本文實例講述了php使用curl並發減少後端存取時間的方法。分享給大家供大家參考,具體如下:
在我們平時的程序中難免出現同時訪問幾個接口的情況,平時我們用curl進行訪問的時候,一般都是單個、順序訪問,假如有3個接口,每個介面耗時500毫秒那麼我們三個介面就要花費1500毫秒了,這個問題太頭疼了嚴重影響了頁面訪問速度,有沒有可能並發訪問來提高速度呢?今天就簡單的說一下,利用curl並發來提高頁面訪問速度,
1、老的curl訪問方式以及耗時統計
<?php function curl_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; } function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } $url_arr=array( "taobao"=>"http://www.taobao.com", "sohu"=>"http://www.sohu.com", "sina"=>"http://www.sina.com.cn", ); $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}"; ?>
耗時:0.614秒
2、curl並發訪問方式以及耗時統計
<?php function curl_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"=>"http://www.sohu.com", "sina"=>"http://www.sina.com.cn", ); function microtime_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}"; ?>
耗時:0.316秒
帥氣吧整個頁面訪問後端介面的時間節省了一半
3、curl相關參數
curl_close —pyg; cURL handle along with all of its preferencescurl_errno — Return the last error number
curl_error — Return a string containing the last error for the current session
curl_exeificat — Performav — Initialize a cURL session
curl_multi_add_handle — Add a normal cURL handle to a cURL multi handle
curl_multi_close — Close a set of cURL handles
curl_multi_execd Run the curconn. content of a cURL handle if CURLOPT_RETURNTRANSFER is set
curl_multi_info_read — Get information about the current transfers
curl_multi_init — Returns a new cURL multi handle
curl_multi_remove_handleh — Remove a multial perpovewhiwhiw swhilwh照 - mem週 - mem週 - mR週 - mR印 號on any curl_multi connection
curl_setopt_array — Set multiple options for a cURL transfer
curl_setopt — Set an option for a cURL transfer
curl_version — Gets cURL version information
更多關於PHP相關內容有興趣的讀者可查看本站專題:《php curl )操作技巧大全》、《PHP資料結構與演算法教學》、《PHP數學運算技巧總結》、《php日期與時間用法總結》、《php物件導向程式設計入門教學》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教學》及《php常見資料庫操作技巧總表》
希望本文所述對大家PHP程式設計有所幫助。
以上就介紹了php使用curl並發減少後端訪問時間的方法分析,包括了curl,的方法方面的內容,希望對PHP教程有興趣的朋友有所幫助。