In PHP, Curl loops to request multiple URLs and multithreading to request multiple URLs

Source: Internet
Author: User
PHP in a loop to request multiple URLs and multithreading to request multiple URLs method
The first type: Circular request
$SR =array (Url_1,url_2,url_3), foreach ($sr as $k = = $v) {$curlPost = $v. '? f= incoming parameters '; $ch = Curl_init ($curlPost); curl_ Setopt ($ch, Curlopt_returntransfer, true); Get Data return   curl_setopt ($ch, Curlopt_binarytransfer, true);//Get Data back when Curlopt_returntransfer is enabled   $data = Curl_ EXEC ($ch); Echo $k. ' # #: '. $data. '
';
}
Curl_close ($ch);

Second multithreaded 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, we found that at the same time request 4 different URLs, the processing speed of the loop seems faster than multi-threaded, the problem remains 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.