加速卷曲多请求

时间:2016-02-22 23:18:38

标签: php curl

我正在运行以下curl multi请求,但它运行正常但是我必须针对4,000多个查询执行此操作,尽管使用异步调用仍需要一段时间才能完成(6分钟以上!)I'我们研究了各种选项以加快速度,并在stackoverflow上添加了一些curl_seopts,这显然有助于提高速度,但我没有注意到任何真正的差异。

我确定我可能错过了一个优化速度的技巧,有没有人有任何提示让它更快?

function multiRequest($data, $accesstoken, $options = array()) {

  // array of curl handles
  $curly = array();
  // data to be returned
  $result = array();

  // multi handle
  $mh = curl_multi_init();

  // loop through $data and create curl handles
  // then add them to the multi-handle

foreach ($data as $id => $d) {

    $curly[$id] = curl_init();

    $headers = array();
    $headers[] =  $d;
    $headers[] = 'Authorization: Bearer '.$accesstoken;

    $url = 'xxxyyyzzz.json';
    curl_setopt($curly[$id], CURLOPT_URL,            $url);
    curl_setopt($curly[$id], CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
    curl_setopt($curly[$id], CURLOPT_ENCODING,'gzip');
    curl_setopt($curly[$id], CURLOPT_HEADER,         0);
    curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $headers);

    if (is_array($d)) {
      if (!empty($d['post'])) {
        curl_setopt($curly[$id], CURLOPT_POST,       1);
        curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
      }
    }

    // extra options?
    if (!empty($options)) {
      curl_setopt_array($curly[$id], $options);
    }

    curl_multi_add_handle($mh, $curly[$id]);
  }

  // execute the handles
  $running = null;
  do {
    curl_multi_exec($mh, $running);
  } while($running > 0);

  // get content and remove handles
  foreach($curly as $id => $c) {

    $result[$id] = curl_multi_getcontent($c);
    curl_multi_remove_handle($mh, $c);
  }

  // all done
  curl_multi_close($mh);
  return $result;
    print_r($result);
}

0 个答案:

没有答案
相关问题