Read many versions of the PHP asynchronous request method, here a simple summary of several common methods to share
1. Implement one step request with Curl
Curl extension is one of the most common methods we use in the development process, and he is a powerful HTTP command-line tool that simulates HTTP requests such as Post/get, and then gets and extracts the data, which is displayed on the "standard output" (stdout).
Example:
Copy Code code as follows:
<?php
$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);
?>
Because the Curopt_timeout property has a minimum value of 1, this means that the client must wait 1 seconds, which is also a disadvantage of using the Curl method
2. Implement asynchronous request with Popen () function
Syntax format: popen (Command,mode)
Example:
Copy Code code as follows:
<?php
$file = Popen ("/bin/ls", "R");
Here is the code to execute
//...
Pclose ($file);
?>
The Popen () function directly opens a pipeline that points to the process, fast, and immediately appropriate. But this function is a single item, either Read or write, and if the number of concurrent numbers is large, a large number of processes will result in a burden on the server.
Also, as in the example, be sure to close with Pclose () after the program finishes.
3. Implement asynchronous request with Fscokopen () function
We usually develop mail to send the function such as socket programming, will use this function, before using this function, we have to open the Allow_url_fopen option in the php.ini, in addition, we have to manually stitching out the header part.
Example:
Copy Code code as follows:
$fp = Fsockopen ("www.uncletoo.com/demo.php", $errno, $errstr, 30);
if (! $fp) {
echo "$errstr ($errno) <br/>\n";
} else {
$out = "get/index.php/http/1.1\r\n";
$out. = "host:www.uncletoo.com\r\n";
$out. = "connection:close\r\n\r\n";
Fwrite ($fp, $out);
/* Ignore execution results here
* Can be opened when testing
while (!feof ($fp)) {
Echo fgets ($FP, 128);
}*/
Fclose ($FP);
}
PHP itself does not have multiple threads, but we can use other ways to achieve the effect of multithreading, the above listed three ways have their own advantages and disadvantages, you can use the program according to the needs of the preferred choice.
Uncletoo experience is shallow, here is summed up so much, if there are other better ways to implement PHP multithreading can be discussed together!