php使用curl并發(fā)減少后端訪問(wèn)時(shí)間的方法分析
本文實(shí)例講述了php使用curl并發(fā)減少后端訪問(wèn)時(shí)間的方法。分享給大家供大家參考,具體如下:
在我們平時(shí)的程序中難免出現(xiàn)同時(shí)訪問(wèn)幾個(gè)接口的情況,平時(shí)我們用curl進(jìn)行訪問(wèn)的時(shí)候,一般都是單個(gè)、順序訪問(wèn),假如有3個(gè)接口,每個(gè)接口耗時(shí)500毫 秒那么我們?nèi)齻€(gè)接口就要花費(fèi)1500毫秒了,這個(gè)問(wèn)題太頭疼了嚴(yán)重影響了頁(yè)面訪問(wèn)速度,有沒(méi)有可能并發(fā)訪問(wèn)來(lái)提高速度呢?今天就簡(jiǎn)單的說(shuō)一下,利用 curl并發(fā)來(lái)提高頁(yè)面訪問(wèn)速度,
1、老的curl訪問(wèn)方式以及耗時(shí)統(tǒng)計(jì)
<?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 "耗時(shí):{$time}"; ?>
耗時(shí):0.614秒
2、curl并發(fā)訪問(wèn)方式以及耗時(shí)統(tǒng)計(jì)
<?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 "耗時(shí):{$time}"; ?>
耗時(shí):0.316秒
帥氣吧整個(gè)頁(yè)面訪問(wèn)后端接口的時(shí)間節(jié)省了一半
3、curl相關(guān)參數(shù)
curl_close — Close a cURL session
curl_copy_handle — Copy a cURL handle along with all of its preferences
curl_errno — Return the last error number
curl_error — Return a string containing the last error for the current session
curl_exec — Perform a cURL session
curl_getinfo — Get information regarding a specific transfer
curl_init — 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_exec — Run the sub-connections of the current cURL handle
curl_multi_getcontent — Return the 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_handle — Remove a multi handle from a set of cURL handles
curl_multi_select — Wait for activity 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
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php curl用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- 使用PHP curl模擬瀏覽器抓取網(wǎng)站信息
- PHP curl模擬瀏覽器采集阿里巴巴的實(shí)現(xiàn)代碼
- php使用curl模擬瀏覽器表單上傳文件或者圖片的方法
- PHP基于curl實(shí)現(xiàn)模擬微信瀏覽器打開(kāi)微信鏈接的方法示例
- php使用curl訪問(wèn)https示例分享
- php采用curl訪問(wèn)域名返回405 method not allowed提示的解決方法
- php 使用curl模擬ip和來(lái)源進(jìn)行訪問(wèn)的實(shí)現(xiàn)方法
- PHP基于curl模擬post提交json數(shù)據(jù)示例
- PHP使用Curl實(shí)現(xiàn)模擬登錄及抓取數(shù)據(jù)功能示例
- PHP curl模擬登錄帶驗(yàn)證碼的網(wǎng)站
- php使用curl偽造瀏覽器訪問(wèn)操作示例
相關(guān)文章
PHP簡(jiǎn)單實(shí)現(xiàn)歐拉函數(shù)Euler功能示例
這篇文章主要介紹了PHP簡(jiǎn)單實(shí)現(xiàn)歐拉函數(shù)Euler功能,簡(jiǎn)單說(shuō)明了歐拉函數(shù)的概念、原理,并結(jié)合實(shí)例形式分析了php實(shí)現(xiàn)歐拉函數(shù)的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11php 獲取今日、昨日、上周、本月的起始時(shí)間戳和結(jié)束時(shí)間戳的方法
php 獲取今日、昨日、上周、本月的起始時(shí)間戳和結(jié)束時(shí)間戳的方法,主要使用到了 php 的時(shí)間函數(shù) mktime,下面首先還是直奔主題以示例說(shuō)明如何使用 mktime 獲取今日、昨日、上周、本月的起始時(shí)間戳和結(jié)束時(shí)間戳,然后在介紹一下 mktime 函數(shù)作用和用法2013-09-09PHP面向?qū)ο蠓治鲈O(shè)計(jì)的經(jīng)驗(yàn)原則
你不必嚴(yán)格遵守這些原則,違背它們也不會(huì)被處以宗教刑罰。但你應(yīng)當(dāng)把這些原則看成警鈴,若違背了其中的一條,那么警鈴就會(huì)響起 。 ----- Arthur J.Riel2008-09-09php中的標(biāo)量數(shù)據(jù)類(lèi)型總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于php中的標(biāo)量數(shù)據(jù)類(lèi)型總結(jié)內(nèi)容,對(duì)此有興趣的朋友們可以跟著學(xué)習(xí)下。2022-01-01php實(shí)現(xiàn)session共享的實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于php如何實(shí)現(xiàn)session共享知識(shí)點(diǎn)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)參考下。2019-09-09PHP實(shí)現(xiàn)的oracle分頁(yè)函數(shù)實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)的oracle分頁(yè)函數(shù),結(jié)合實(shí)例形式分析了PHP針對(duì)oracle數(shù)據(jù)庫(kù)使用rownum代替MySQL中l(wèi)imit實(shí)現(xiàn)的分頁(yè)操作相關(guān)技巧,需要的朋友可以參考下2016-01-01