php - PHP 中使用 cURL 的同步 HTTP 请求

标签 php loops curl

我正在尝试使用比较大的域列表来查询每个域的排名,如下所示 -> https://www.compete.com/developer/documentation

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

不幸的是,该脚本将采用 25,000 个域的数组并尝试一次处理所有这些域。我发现 1,000 个批处理效果很好。

知道如何向compute.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;

}
?>

最佳答案

而不是

//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 个 block ,并将这 1,000 个值传递给您的 multiRequest 函数,该函数使用 cURL 来执行这些请求。

关于php - PHP 中使用 cURL 的同步 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12379801/

相关文章:

php - 教义2 : Combine Select with Select from another table with identical structure

php - 从文本区域到单行字符串的多行

c++ - rand() 函数不能与文件 I/O 结合使用

python - Python 中列表中的 for 循环有什么作用?

haskell - 在 Windows 7 上的 Haskell 平台中使用 Curl

javascript - PHP Ajax 仅加载新内容

php - 检查重复记录

java - 为什么循环不将 long 作为其条件中的有效类型?

ssl - CURL - 间歇性错误 35 - 连接中出现未知的 SSL 协议(protocol)错误

php - 通过 cURL 发送 JSON 始终返回 "No access"