Php uses curl for multi-thread submission

Source: Internet
Author: User
Php does not want java to inherit the Thread interface directly or implement the Runnable interface for multi-Thread development. However, php provides a powerful curl tool that supports multithreading. You can use curl_multi_init () in php () to simulate parallel processing and multi-threaded program functions. You can also use curl_multi_init () to simulate multi-thread submission. As shown in the following program

Php does not want java to inherit the Thread interface directly or implement the Runnable interface for multi-Thread development. However, php provides a powerful curl tool that supports multithreading. You can use curl_multi_init () in php () to simulate parallel processing and multi-threaded program functions. You can also use curl_multi_init () to simulate multi-thread submission. As shown in the following program

Php does not want java to inherit the Thread interface directly or implement the Runnable interface for multi-Thread development. However, php provides a powerful curl tool that supports multithreading,

In php, you can use curl_multi_init () to simulate parallel processing and multi-threaded program functions. You can also use curl_multi_init () to simulate multi-thread submission. As shown in the following program:

 $ Url) {$ conn [$ I] = curl_init ($ url); curl_setopt ($ conn [$ I], CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ conn [$ I], CURLOPT_POST, 1); curl_setopt ($ conn [$ I], CURLOPT_POSTFIELDS, array ('offset' => $ I); // submit data curl_multi_add_handle ($ mh, $ conn [$ I]);} do {$ status = curl_multi_exec ($ mh, $ active); $ info = curl_multi_info_read ($ mh); if (false! ==$ Info) {// var_dump ($ info) ;}} while ($ status === CURLM_CALL_MULTI_PERFORM | $ active ); foreach ($ urls as $ I =>$ url) {$ res [$ I] = curl_multi_getcontent ($ conn [$ I]); // obtain the returned content curl_close ($ conn [$ I]);} echo"
"; Print_r ($ res); $ etime = microtime (true); // get the execution end time of the program $ total = $ etime-$ stime; // calculate the difference value $ str_total = var_export ($ total, TRUE); echo $ str_total;
The program starts seven curls respectively to index. the php page sends a request in index. php can process the request based on the sent parameters, and simulate parallel multi-process submission ..... it can also be applied

Test data access!

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.