Objective:
In our usual program will inevitably appear at the same time access to several interfaces, usually we use curl to access the time, generally is a single, sequential access, if there are 3 interfaces, each interface takes 500 milliseconds then we three interface will cost 1500 milliseconds, This problem is too headache to seriously affect the page access speed, there is no possibility of concurrent access to improve speed? Today simply say, the use of curl concurrency to improve page access speed, I hope you have more guidance.
1. Old Curl access methods and time-consuming statistics
<?PHPfunctionCurl_fetch ($url,$timeout=3){ $ch=Curl_init (); curl_setopt ($ch, Curlopt_url,$url); curl_setopt ($ch, Curlopt_timeout,$timeout); curl_setopt ($ch, Curlopt_returntransfer, 1); $data= Curl_exec ($ch); $errno= Curl_errno ($ch); if($errno>0) { $data=false; } curl_close ($ch); return $data;}functionmicrotime_float () {List($usec,$sec) =Explode(" ",Microtime()); return((float)$usec+ (float)$sec);}$url _arr=Array( "Taobao" = "http://www.taobao.com", "Sohu" and "http://www.sohu.com", "lai18" and "http://www.lai18.com",);$time _start=microtime_float ();$data=Array();foreach($url _arr as $key=$val){ $data[$key]=curl_fetch ($val);}$time _end=microtime_float ();$time=$time _end-$time _start;Echo"Time consuming: {$time}";?>
Time: 0.614 seconds
2, Curl concurrent access mode and time-consuming statistics
<?PHPfunctionCurl_multi_fetch ($urlarr=Array()){ $result=$res=$ch=Array(); $nch= 0; $MH=Curl_multi_init (); foreach($urlarr as $nk=$url) { $timeout=2; $ch[$nch] =Curl_init (); Curl_setopt_array ($ch[$nch],Array(Curlopt_url=$url,Curlopt_header=false,Curlopt_returntransfer=true,Curlopt_timeout=$timeout, )); Curl_multi_add_handle ($MH,$ch[$nch]); ++$nch; } /*wait for performing request*/ Do { $MRC= Curl_multi_exec ($MH,$running); } while(Curlm_call_multi_perform = =$MRC); while($running&&$MRC==CURLM_OK) { //Wait for network if(Curl_multi_select ($MH, 0.5) >-1) { //Pull in new data; Do { $MRC= Curl_multi_exec ($MH,$running); } while(Curlm_call_multi_perform = =$MRC); } } if($MRC!=CURLM_OK) { Error_log("CURL Data Error"); } /*Get Data*/ $nch= 0; foreach($urlarr as $moudle=$node) { if(($err= Curl_error ($ch[$nch]) = = ") { $res[$nch]=curl_multi_getcontent ($ch[$nch]); $result[$moudle]=$res[$nch]; } Else { Error_log("Curl Error"); } curl_multi_remove_handle ($MH,$ch[$nch]); Curl_close ($ch[$nch]); ++$nch; } curl_multi_close ($MH); return $result;}$url _arr=Array( "Taobao" = "http://www.taobao.com", "Sohu" and "http://www.sohu.com", "Sina" and "http://www.sina.com.cn", );functionmicrotime_float () {List($usec,$sec) =Explode(" ",Microtime()); return((float)$usec+ (float)$sec);}$time _start=microtime_float ();$data=curl_multi_fetch ($url _arr);$time _end=microtime_float ();$time=$time _end-$time _start; Echo"Time consuming: {$time}";?>
Time-consuming: 0.316 seconds handsome. The entire page accesses the backend interface for half the time saved
Curl Technical Knowledge Tutorial series of technical Articles to organize the collection
Http://www.cnblogs.com/ruthon/p/4491800.html
Use curl in PHP to reduce access to third-party Web content time