I have seen many versions of PHP asynchronous request methods on the Internet. Here is a brief summary of a few common methods to share with you
1. Use CURL to implement one-step requests
CURL extension is One of the most commonly used methods during our development process is a powerful HTTP command line tool that can simulate HTTP requests such as POST/GET, then obtain and extract data, and display it on "standard output" (stdout).
Example:
Copy code The code is as follows:
$cl = curl_init();
$curl_opt = array(CURLOPT_URL, 'http://www.uncletoo.com/demo.php',
CURLOPT_RETURNTRANSFER, 1,
CURLOPT_TIMEOUT, 1,);
curl_setopt_array ($cl, $curl_opt);
curl_exec($ch);
curl_close($ch);
?>
Since the minimum value of the CUROPT_TIMEOUT attribute is 1, this It means that the client must wait for 1 second, which is also the disadvantage of using the CURL method
2. Use the popen() function to implement asynchronous requests Syntax format: popen (command,mode)
Example:
Copy code The code is as follows:
$file = popen("/bin/ls","r");
//Here is the code to be executed
//...
pclose($file);
?>
The popen() function directly opens a pipe pointing to the process, which is fast and responds immediately. However, this function is single, either reading or writing, and if the number of concurrency is large, a large number of processes will be generated, which will burden the server.
In addition, as in the example, be sure to use pclose() to close the program after it ends.
3. Use the fscokopen() function to implement asynchronous requests We usually use this function when developing socket programming such as email sending functions. When using this function Previously, we had to enable the allow_url_fopen option in PHP.ini, and when changing, we had to manually splice out the header part ourselves.
Example:
Copy code The code is as follows:
$fp = fsockopen("www.uncletoo. com/demo.php", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)
n";
} else {
$out = "GET /index.php / HTTP/1.1rn";
$out .= "Host: www.uncletoo.comrn";
$out .= "Connection: Closernrn";
fwrite($fp, $out);
/*Ignore the execution results here
*You can open it during testing
while (!feof($fp)) {
echo fgets($fp, 128);
}*/
fclose($fp);
}
PHP itself does not have multi-threading, but we can use other methods To achieve multi-threading effect, the three methods listed above have their own advantages and disadvantages. When using them, you can choose the best according to the needs of the program.
UncleToo has little experience, so this is a brief summary here. If there are other better ways to implement PHP multi-threading, we can discuss them together!
http://www.bkjia.com/PHPjc/710596.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/710596.htmlTechArticleI have seen many versions of PHP asynchronous request methods on the Internet. Here is a brief summary of several common methods to share with you 1. Using CURL to implement one-step requests CURL extension is the most commonly used one in our development process...