为什么可以';我是否发送了太多请求';s到谷歌地图API,使用PHP curl?

为什么可以';我是否发送了太多请求';s到谷歌地图API,使用PHP curl?,php,curl,Php,Curl,我对curl-php脚本有一个问题,如果我发送的请求超过5个,脚本就会中断 我试图从服务器端执行该脚本,因为我认为这是一个internet连接问题,但它不起作用 该脚本从我的数据库中提取信息,并在谷歌地图上搜索确切的地址 有时需要2500个请求有时需要100个,这是随机的。 如果我使用5个请求和1个作为数组块的限制,就像一个符咒,没有错误 代码如下: <?php //includes include_once("inc_dbcon.php"); //get data from db $

我对curl-php脚本有一个问题,如果我发送的请求超过5个,脚本就会中断

我试图从服务器端执行该脚本,因为我认为这是一个internet连接问题,但它不起作用

该脚本从我的数据库中提取信息,并在谷歌地图上搜索确切的地址

有时需要2500个请求有时需要100个,这是随机的。 如果我使用5个请求和1个作为数组块的限制,就像一个符咒,没有错误

代码如下:

<?php

//includes
include_once("inc_dbcon.php");

//get data from db
$result = mysql_query('SELECT * FROM `geo` where `state` = "FL" ORDER BY rand() limit 0,5000');
while($row = mysql_fetch_array($result)) {

//simplified request just to test if it works...
$competeRequests[] = "http://maps.googleapis.com/maps/api/geocode/json?latlng=34.0519,-118.243" ;

}


$curlRequest = array();

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

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

}

$j = 0;
foreach ($curlRequest as $json){
$j++;
$json_output = json_decode($json, TRUE);

$newlat = $json_output['results'][0]['geometry']['location']['lat'];
$newlong = $json_output['results'][0]['geometry']['location']['lng'];
$address=$json_output['results'][0]['formatted_address'];
$zip = explode(",", $address);
$state_zip = explode(" ", trim($zip[2]));
$the_zip =$state_zip[1];
if($newlat) {

    $out = "$the_zip, $newlat, $newlong ";
    //show output
    echo $out . "<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;

}

?>

你似乎正在打破谷歌地图的使用限制。实际数字似乎是每秒10次

以下是一个可能的解决方案:

<?php

//includes
include_once("inc_dbcon.php");

//get data from db
$result = mysql_query('SELECT * FROM `geo` where `state` = "FL" ORDER BY rand() limit 0,20');
while($row = mysql_fetch_array($result)) {

//simplified request just to test if it works...
$competeRequests[] = "http://maps.googleapis.com/maps/api/geocode/json?latlng=34.0519,-118.243" ;

}


$curlRequest = array();

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

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

$j = 0;
foreach ($curlRequest as $json){
$j++;
$json_output = json_decode($json, TRUE);

$newlat = $json_output['results'][0]['geometry']['location']['lat'];
$newlong = $json_output['results'][0]['geometry']['location']['lng'];
$address=$json_output['results'][0]['formatted_address'];
$zip = explode(",", $address);
$state_zip = explode(" ", trim($zip[2]));
$the_zip =$state_zip[1];
if($newlat) {

    $out = "$the_zip, $newlat, $newlong ";
    //show output
    echo $out . "<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;

}

?>

我看到了,如果我的代码没有问题,这似乎就是问题所在。如何在每10个请求后暂停脚本?PHP为您提供了sleep函数(),您可能想将数组分成10个部分,然后将其粘贴到一个数组中,我正在考虑如何立即执行。@john您可以尝试此实现:)无法使其工作“array_merge():参数#2不是test.PHP第21行的数组”和“为test.php中的foreach()在第26行提供的参数无效”。ps:将mysql查询限制降低到20,将array_chunk降低到10,否则我认为完成循环需要花费很多时间……我认为解决方案太复杂了,如果您已经在使用array chuck,那么就结束睡眠