Home  >  Article  >  Backend Development  >  Detailed explanation of common methods of asynchronous execution based on PHP_PHP tutorial

Detailed explanation of common methods of asynchronous execution based on PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:09:42832browse

1. The client page uses AJAX technology to request the server
Advantages:
The simplest and fastest is to embed the AJAX call in the HTML code returned to the client, or, Embed an img tag, src points to the time-consuming script to be executed.
Disadvantages: Generally speaking, Ajax should be triggered after onLoad. That is to say, if the user clicks on the page and closes it, then our background script will not be triggered.
If you use the img tag, this method cannot be called asynchronous execution in the strict sense. The user's browser will wait for a long time for the execution of the php script to be completed, that is, the status bar of the user's browser always shows that it is still loading.
Of course, other methods with similar principles can also be used, such as script tags, etc.
2.popen() function
This function opens a pipe pointing to the process resulting from the execution of the given command command. Opens a pipe to the process spawned by execution of the command that spawned the given command.
So you can call it but ignore its output. Use the following code:

Copy the code The code is as follows:

pclose(popen("/home/xinchen/backend.php & ", 'r'));

Advantages: Avoids the shortcomings of the first method and is also fast.
Disadvantages: This method cannot request another WebService through the HTTP protocol and can only execute local script files. And it can only be opened in one direction, and cannot pass a large number of parameters to the called script. And if the number of visits is high, a large number of processes will be generated. If you use external resources, you have to consider the competition yourself.
3.CURL extension
CURL is a powerful HTTP command line tool that can simulate HTTP requests such as POST/GET, and then get and extract data, displayed in Standard Output" (stdout) above. The code is as follows:
Copy code The code is as follows:

$ch = curl_init();
$curl_opt = array( CURLOPT_URL, 'http://www.example.com/backend.php',
CURLOPT_RETURNTRANSFER, 1,
CURLOPT_TIMEOUT, 1,);
curl_setopt_array($ch, $curl_opt);
curl_exec ($ch);
curl_close($ch);

Disadvantages: As described in your question, due to using CURL you need to set CUROPT_TIMEOUT to 1 (minimum is 1, depressed). That is, the client must wait at least 1 second.
4.fscokopen() function
fsockopen supports socket programming. You can use fsockopen to implement socket programs such as email sending, etc. To use fcockopen, you need to manually splice out the header part.
Usage examples are as follows:
Copy codeThe code is as follows:

$fp = fsockopen( "www.34ways.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)
n";
} else {
$out = "GET /index.php / HTTP/1.1rn";
$out .= "Host: www.34ways.comrn";
$out .= "Connection : Closernrn";
fwrite($fp, $out);
/*Ignore the execution result
while (!feof($fp)) {
echo fgets($fp, 128);
}*/
 fclose($fp);
}

So in summary, the fscokopen() function should meet your requirements. You can try it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327228.htmlTechArticle1. The client page uses AJAX technology to request the server. Advantages: The simplest and fastest, it is returned to the customer In the HTML code of the client, embed AJAX calls, or embed an img tag,...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn