PHP巧用curl 並發減少後端訪問時間

來源:互聯網
上載者:User
在我們平時的程式中難免出現同時訪問幾個介面的情況,平時我們用curl進行訪問的時候,一般都是單個、順序訪問,假如有3個介面,每個介面耗時500毫 秒那麼我們三個介面就要花費1500毫秒了,這個問題太頭疼了嚴重影響了頁面訪問速度,有沒有可能並發訪問來提高速度呢?今天就簡單的說一下,利用 curl並發來提高頁面訪問速度

1、curl訪問方式以及耗時統計

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

2、php中curl並發訪問方式以及耗時統計

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}";
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.