In php, curl cyclically requests multiple URLs and multiple threads to request multiple URLs-php Tutorial

Source: Internet
Author: User
In php, curl cyclically requests multiple URLs and multiple threads to request multiple URLs. The first method is loop request $ srarray (url_1, url_2, url_3 ); foreach ($ sras $ k & gt; $ v) {$ curlPost $ v .? F input parameters; $ chcurl_init ($ curlPost); curl_setopt ($ ch, CURLOP php curl loop to request multiple URLs and multiple threads to request multiple URLs
Type 1: cyclic requests
$ Sr = array (url_1, url_2, url_3); foreach ($ sr as $ k => $ v) {$ curlPost = $ v .'? F = input parameter '; $ ch = curl_init ($ curlPost); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // get data and return curl_setopt ($ ch, CURLOPT_BINARYTRANSFER, true ); // when CURLOPT_RETURNTRANSFER is enabled, $ data = curl_exec ($ ch); echo $ k is returned. '##:'. $ data.'
';
}
curl_close($ch);

Second multi-thread request

$ Sr = array (url_1, url_2, url_3 );

$mh = curl_multi_init();     foreach ($sr as $i => $url) {      $curlPost=$url.'?f=%2Fpipefile%2Fskincss%2Fskincss_1356490012_1.zip%40%2Fskincss%2Fimages%2Fskincss_1356490012_1.jpg';      $conn[$i]=curl_init($curlPost);      curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1);      curl_multi_add_handle ($mh,$conn[$i]);     }      do {      $mrc = curl_multi_exec($mh,$active);     } while ($mrc == CURLM_CALL_MULTI_PERFORM);     while ($active and $mrc == CURLM_OK) {      if (curl_multi_select($mh) != -1) {       do {        $mrc = curl_multi_exec($mh, $active);       } while ($mrc == CURLM_CALL_MULTI_PERFORM);      }     }      foreach ($sr as $i => $url) {      $res[$i]=curl_multi_getcontent($conn[$i]);      curl_close($conn[$i]);     }  var_dump($res);

After a simple test, it is found that when four different URLs are requested at the same time, the processing speed of the loop seems to be faster than that of multithreading. This problem needs to be verified.


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.