Read many versions of PHP asynchronous request method on the Internet, here is a brief summary of a few common methods to share to everyone
1. Make one step request with Curl
Curl extension is one of the most common methods we use in the development process, which is a powerful HTTP command-line tool that simulates HTTP requests such as Post/get, and then obtains and extracts data that is displayed on the "standard output" (stdout).
Example:
Copy CodeThe 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 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. Implementing asynchronous requests with the Popen () function
Syntax format: popen (Command,mode)
Example:
Copy CodeThe code is as follows:
$file = Popen ("/bin/ls", "R");
Here is the code to execute
//...
Pclose ($file);
?>
The Popen () function directly opens a pipeline to a process that is fast and responsive. However, this function is a single item, either Read or write, and if the number of concurrent numbers is large, there will be a lot of process and burden on the server.
In addition, as in the example, it must be closed with pclose () after the end of the program.
3. Implementing asynchronous requests with the Fscokopen () function
We usually develop the mail sending function, such as socket programming, will use this function, before using this function, we have to open the Allow_url_fopen option in php.ini, in addition, we also have to manually splicing the header section.
Example:
Copy CodeThe code is as follows:
$fp = Fsockopen ("www.uncletoo.com/demo.php", $errno, $errstr, 30);
if (! $fp) {
echo "$errstr ($errno)
\ 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 is not multi-threading, but we can use other ways to achieve multi-threaded effect, 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 simply summed up so much, if there are other better implementations of PHP multi-threading method can be discussed together!
http://www.bkjia.com/PHPjc/710596.html www.bkjia.com true http://www.bkjia.com/PHPjc/710596.html techarticle read a lot of PHP asynchronous request method on the Internet, here briefly summarize a few common methods to share to everyone 1, with curl to achieve a step request curl extension is our most commonly used in the development process ...