Php Curl Multithreading method

Source: Internet
Author: User

<?PHP/*Curl Multi-threaded crawl*/ /** * Curl Multithreading * * @param array $array parallel URL * @param int $timeout Time Out * @return Array */  functionCurl_http ($array,$timeout){  $res=Array(); $MH= Curl_multi_init ();//Create multiple Curl handles $startime=Getmicrotime (); foreach($array  as $k=$url){   $conn[$k]=curl_init ($url); curl_setopt ($conn[$k], Curlopt_timeout,$timeout);//setting the time-out periodcurl_setopt ($conn[$k], Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) '); curl_setopt ($conn[$k], Curlopt_maxredirs, 7);//HTTP Orientation Levelcurl_setopt ($conn[$k], Curlopt_header, 0);//no header, no more efficiency.curl_setopt ($conn[$k], curlopt_followlocation, 1);//302 redirectcurl_setopt ($conn[$k],curlopt_returntransfer,1); Curl_multi_add_handle ($MH,$conn[$k]); }  //to prevent the death cycle from killing the CPU. This is based on the online wording   Do {   $MRC= Curl_multi_exec ($MH,$active);//when there is no data, active=true} while($MRC= = Curlm_call_multi_perform);//When data is being accepted   while($activeand$MRC= = CURLM_OK) {//when there is no data or a request is paused, the Active=true   if(Curl_multi_select ($MH)! =-1) {     Do {     $MRC= Curl_multi_exec ($MH,$active); }  while($MRC==curlm_call_multi_perform); }  }  foreach($array  as $k=$url) {Curl_error ($conn[$k]); $res[$k]=curl_multi_getcontent ($conn[$k]);//Get return information       $header[$k]=curl_getinfo ($conn[$k]);//Return header informationCurl_close ($conn[$k]);//Closing a handleCurl_multi_remove_handle ($MH,$conn[$k]);//Freeing Resources} curl_multi_close ($MH); $endtime=Getmicrotime (); $diff _time=$endtime-$startime; return Array(' diff_time ' =$diff _time, ' return ' =$res, ' header ' =$header       ); } //Calculate Current Time functionGetmicrotime () {List($usec,$sec) =Explode(" ",Microtime()); return((float)$usec+ (float)$sec); } //test it, curl. Three URLs $array=Array(    "http://www.weibo.com/", "http://www.renren.com/", "http://www.qq.com/"    ); $data= Curl_http ($array, ' 10 ');//called Var_dump($data);//Output?>

The explanation of the does while:

Because $active to wait for all the URL data to be completed before it becomes false, so here to use the return value of curl_multi_exec to determine whether there is data,
When there is data on the call curl_multi_exec, temporarily no data to enter the Select phase, the new data can be awakened to continue execution.
The advantage here is that the CPU is useless.

This multi-threaded writing step:
First step: Call Curl_multi_init
Step two: Loop call Curl_multi_add_handle
It is important to note that the second parameter of Curl_multi_add_handle is a sub-handle from Curl_init.
Step three: Continue calling Curl_multi_exec
Fourth step: Loop call Curl_multi_getcontent as needed to get results
Fifth step: Call Curl_multi_remove_handle, and call curl_close for each word handle
Sixth step: Call Curl_multi_close

Php Curl Multithreading method

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.