php使用curl并發(fā)減少后端訪(fǎng)問(wèn)時(shí)間的方法分析
來(lái)源:易賢網(wǎng) 閱讀:665 次 日期:2016-08-23 15:42:26
溫馨提示:易賢網(wǎng)小編為您整理了“php使用curl并發(fā)減少后端訪(fǎng)問(wèn)時(shí)間的方法分析”,方便廣大網(wǎng)友查閱!

本文實(shí)例講述了php使用curl并發(fā)減少后端訪(fǎng)問(wèn)時(shí)間的方法。分享給大家供大家參考,具體如下:

在我們平時(shí)的程序中難免出現(xiàn)同時(shí)訪(fǎng)問(wèn)幾個(gè)接口的情況,平時(shí)我們用curl進(jìn)行訪(fǎng)問(wèn)的時(shí)候,一般都是單個(gè)、順序訪(fǎng)問(wèn),假如有3個(gè)接口,每個(gè)接口耗時(shí)500毫 秒那么我們?nèi)齻€(gè)接口就要花費(fèi)1500毫秒了,這個(gè)問(wèn)題太頭疼了嚴(yán)重影響了頁(yè)面訪(fǎng)問(wèn)速度,有沒(méi)有可能并發(fā)訪(fǎng)問(wèn)來(lái)提高速度呢?今天就簡(jiǎn)單的說(shuō)一下,利用 curl并發(fā)來(lái)提高頁(yè)面訪(fǎng)問(wèn)速度,

1、老的curl訪(fǎng)問(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ā)訪(fǎng)問(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è)面訪(fǎng)問(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

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話(huà):0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
咨詢(xún)QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)