使用cURL在PHP中同时发出HTTP请求

时间:2012-09-12 00:40:17

标签: php loops curl

我正在尝试使用相当大的域列表查询每个域的等级,使用如下所示的compete.com API - > https://www.compete.com/developer/documentation

我编写的脚本采用了我填充的域的数据库,并启动了一个cURL请求来竞争网站的排名。我很快意识到这很慢,因为每次请求都是一次发送的。我做了一些搜索,发现了这个帖子> http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/解释了如何使用cURL在PHP中同时执行HTTP请求。

不幸的是,该脚本将采用25,000个域的数组并尝试一次处理它们。我发现1,000个批次的工作非常好。

任何想法如何向compete.com发送1,000个查询然后等待完成并发送下一个1,000直到数组为空? 这就是我正在使用的内容到目前为止:

<?php

//includes
include('includes/mysql.php');
include('includes/config.php');

//get domains
$result = mysql_query("SELECT * FROM $tableName");
while($row = mysql_fetch_array($result)) {
    $competeRequests[] = "http://apps.compete.com/sites/" . $row['Domain'] . "/trended/rank/?apikey=xxx&start_date=201207&end_date=201208&jsonp=";
}

//first batch
$curlRequest = multiRequest($competeRequests);
$j = 0;
foreach ($curlRequest as $json){
    $j++;
    $json_output = json_decode($json, TRUE);
    $rank = $json_output[data][trends][rank][0][value];

    if($rank) {
        //Create mysql query
        $query = "Update $tableName SET Rank = '$rank' WHERE ID  = '$j'";

        //Execute the query
        mysql_query($query);
        echo $query . "<br/>";
    }
}


function multiRequest($data) {
  // 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();

    $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
    curl_setopt($curly[$id], CURLOPT_URL,            $url);
    curl_setopt($curly[$id], CURLOPT_HEADER,         0);
    curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);

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

    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;

}
?>

2 个答案:

答案 0 :(得分:5)

而不是

//first batch
$curlRequest = multiRequest($competeRequests);

$j = 0;
foreach ($curlRequest as $json){

你可以这样做:

$curlRequest = array();

foreach (array_chunk($competeRequests, 1000) as $requests) {
    $results = multiRequest($requests);

    $curlRequest = array_merge($curlRequest, $results);
}

$j = 0;
foreach ($curlRequest as $json){
    $j++;
    // ...

这会将大数组拆分为1,000个块,并将这1,000个值传递给multiRequest函数,该函数使用cURL执行这些请求。

答案 1 :(得分:0)

https://github.com/webdevelopers-eu/ShadowHostCloak

这正是您想要的。只需将空参数传递给new Proxy()即可绕过代理并提出直接请求。

您可以在其中填充1000个请求并调用$proxy->execWait(),它将同时处理所有请求并在完成所有操作后退出该方法...然后可以重复。