3 ways to implement a PHP multithreaded asynchronous request

Source: Internet
Author: User
Tags curl php multithreading

  Implementation of PHP multithreaded asynchronous request method There are many, in this article to organize a few 3 kinds of common methods, you can refer to the following

Read many versions of the PHP asynchronous request method, here a brief summary of several common methods to share     1, with curl to implement a step request     Curl expansion is the most common way we use in the development process, 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:    code is as follows: <?php  $CL = Curl_init ();  $curl _opt = Array (curlopt_url, ' http://www.un cletoo.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 the disadvantage of using the Curl method     2, asynchronous request with Popen () function     syntax format: popen (command,mode)     Example:    code is as follows: <?php  $file = Popen ("/bin/ls", "R"); //Here is the code to execute  //...  pclose ($file); nbsp The?>    Popen () function directly opens a pipeline 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, using Fscokopen () function to implement asynchronous request     We usually develop mail to send the function such as socket programming, will use this function, before using this function, we have to open a in the php.iniLlow_url_fopen option, and in turn, we also have to manually stitching out the header part.     Example:    code is as follows: $fp = Fsockopen ("www.uncletoo.com/demo.php", $errno, $errstr,);  if (! $fp) {  echo "$errstr ($errno) <br/>n"; } else {  $out = "Get/index.php/http/1.1rn";  $out. = "H Ost:www.uncletoo.comrn ";  $out. =" Connection:closernrn ";    fwrite ($fp, $out); /* Ignore execution results   * When testing, you can open   while (!feof ($FP)) {  Echo fgets ($fp, 128); }*/ , fclose ($fp); }    PHP itself is not Multithreading, 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!    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.