Home >Backend Development >PHP Tutorial >Analysis of how PHP uses curl concurrency to reduce backend access time, curl backend_PHP tutorial

Analysis of how PHP uses curl concurrency to reduce backend access time, curl backend_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:52:54890browse

Analysis of how PHP uses curl concurrency to reduce backend access time, curl backend

This article describes how PHP uses curl concurrency to reduce backend access time. Share it with everyone for your reference, the details are as follows:

In our daily programs, it is inevitable to access several interfaces at the same time. When we usually use curl to access, it is usually a single and sequential access. If there are 3 interfaces, each interface takes 500 milliseconds. Then Our three interfaces will take 1500 milliseconds. This problem is too troublesome and seriously affects the page access speed. Is it possible to increase the speed by concurrent access? Today I will briefly talk about using curl concurrency to improve page access speed,

1. Old curl access method and time-consuming statistics

<&#63;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}";
&#63;>

Time taken: 0.614 seconds

2. Curl concurrent access methods and time-consuming statistics

<&#63;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}";
&#63;>

Time taken: 0.316 seconds

The time for accessing the back-end interface of the entire page of Handsome Bar is reduced by half

3. curl related parameters

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

Readers who are interested in more PHP related content can check out the special topics of this site: "php curl usage summary", "PHP array (Array) operation skills", "PHP data structure and algorithm tutorial", "PHP mathematical operations" Skills Summary", "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary" 》

I hope this article will be helpful to everyone in PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1125866.htmlTechArticleAnalysis on how PHP uses curl concurrency to reduce back-end access time, curl back-end This article explains how PHP uses curl concurrency Ways to reduce backend access time. Share it with everyone for your reference, with...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn