Three methods to implement PHP multi-thread asynchronous requests

Source: Internet
Author: User
I have seen many versions of PHP asynchronous request methods on the Internet, here are a few common methods to share with you. 1. using CURL to implement one-step request CURL extension is the most common method in our development process. it is a powerful HTTP command line tool, it can simulate POSTGET and other HTTP requests. However, I have seen many versions of PHP asynchronous request methods on the Internet, here are a few common methods to share with you. 1. using CURL to implement one-step request CURL extension is the most common method in our development process. it is a powerful HTTP command line tool, simulate POST/GET and other HTTP requests, and then obtain and extract data, which is displayed on stdout. Example: [php] Because the minimum value of the CUROPT_TIMEOUT attribute is 1, this means that the client must wait for 1 second. this is also the disadvantage of using the CURL method. 2. use the popen () function to implement the asynchronous request syntax format: popen (command, mode) example: [php] The popen () function directly opens a pipeline pointing to a process, which is fast and instantly appropriate. However, this function is a single function, either read or write, and if the number of concurrency is large, a large number of processes will be generated, causing a burden on the server. In addition, as in the example, the program must be closed with pclose. 3. use the fscokopen () function to implement asynchronous requests. we usually use this function when developing socket programming such as the Mail sending function. before using this function, we need to use it in PHP. enable the allow_url_fopen option in ini. In addition, we also need to manually splice the header part. Example: [php] $ fp = fsockopen ("www.uncletoo.com/demo.php", 80, $ 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"; fwrite ($ fp, $ out);/* ignore the execution result here * you can open while (! Feof ($ fp) {echo fgets ($ fp, 128) ;}*/fclose ($ fp);} PHP itself has no multithreading, however, we can use other methods to achieve the effect of multithreading. The three methods listed above have their own advantages and disadvantages. you can choose the best option based on the needs of the program during use. UncleToo experience is not very simple. here is a summary of so much. if there are other better methods to implement PHP multithreading, we can discuss it 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.