国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

利用curl并發(fā)來提高頁面訪問速度

2019-11-15 01:31:10
字體:
供稿:網(wǎng)友
利用curl并發(fā)來提高頁面訪問速度

在我們平時的程序中難免出現(xiàn)同時訪問幾個接口的情況,平時我們用curl進行訪問的時候,一般都是單個、順序訪問,假如有3個接口,每個接口耗時500毫秒那么我們?nèi)齻€接口就要花費1500毫秒了,這個問題太頭疼了嚴重影響了頁面訪問速度,有沒有可能并發(fā)訪問來提高速度呢?今天就簡單的說一下,利用curl并發(fā)來提高頁面訪問速度,希望大家多指導(dǎo)。1、老的curl訪問方式以及耗時統(tǒng)計

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并發(fā)訪問方式以及耗時統(tǒng)計

<?phpfunction 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}";?>

輸出結(jié)果array('taobao'=>result,'sina'=>result,'baidu'=>result)  ;

耗時:0.316秒帥氣吧整個頁面訪問后端接口的時間節(jié)省了一半3、curl相關(guān)參數(shù)來自:http://cn2.php.net/manual/en/ref.curl.phpcurl_close — Close a cURL sessioncurl_copy_handle — Copy a cURL handle along with all of its PReferencescurl_errno — Return the last error numbercurl_error — Return a string containing the last error for the current sessioncurl_exec — Perform a cURL sessioncurl_getinfo — Get information regarding a specific transfercurl_init — Initialize a cURL sessioncurl_multi_add_handle — Add a normal cURL handle to a cURL multi handlecurl_multi_close — Close a set of cURL handlescurl_multi_exec — Run the sub-connections of the current cURL handlecurl_multi_getcontent — Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is setcurl_multi_info_read — Get information about the current transferscurl_multi_init — Returns a new cURL multi handlecurl_multi_remove_handle — Remove a multi handle from a set of cURL handlescurl_multi_select — Wait for activity on any curl_multi connectioncurl_setopt_array — Set multiple options for a cURL transfercurl_setopt — Set an option for a cURL transfercurl_version — Gets cURL version information原文地址:http://www.searchtb.com/2010/12/using-multicurl-to-improve-performance.html?spm=0.0.0.0.onjnnY


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 措美县| 武穴市| 那曲县| 崇左市| 通州市| 昭平县| 黔东| 句容市| 石阡县| 托克托县| 中江县| 兴海县| 永修县| 淳安县| 河曲县| 揭阳市| 赣州市| 武强县| 鄯善县| 阳朔县| 噶尔县| 饶河县| 龙州县| 且末县| 徐州市| 班玛县| 延川县| 大连市| 平定县| 大同市| 江油市| 静安区| 北辰区| 嘉峪关市| 东光县| 资阳市| 乌兰浩特市| 峡江县| 洪江市| 惠东县| 盐边县|