cURL地理编码OVER_QUERY_LIMIT

时间:2013-10-23 21:36:59

标签: php google-maps curl

谷歌的地理编码API有问题:

我有一个地址应该通过谷歌地理编码api使用cURL转换为纬度和经度。您将在下面看到我的代码。它工作正常,但突然它停止工作,我有一个“OVER_QUERY_LIMIT”的答案。我查了一下,如果每天要求api超过2500次,通常会发生这种情况。这是不可能的,因为我的网站刚刚完成,每天有大约20-40个地理编码请求。

那么“OVER_QUERY_LIMIT”出现的问题究竟是什么?我的代码出了什么问题,以某种方式谷歌阻止了它?!

ini_set('allow_url_fopen', true);

$CookiePath = 'mycookiepath/geocookie.txt';
$userAgent = "mysite.com";
$ListURLRoot = "http://maps.googleapis.com/maps/api/geocode/json";
$ListURLSuffix = '&sensor=false';
$Curl_Obj = curl_init(); // Setup cURL
curl_setopt($Curl_Obj, CURLOPT_COOKIEJAR, $CookiePath);
curl_setopt($Curl_Obj, CURLOPT_USERAGENT, $userAgent);
curl_setopt($Curl_Obj, CURLOPT_HEADER, 0);
curl_setopt($Curl_Obj, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($Curl_Obj, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($Curl_Obj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($Curl_Obj, CURLOPT_TIMEOUT, 30);
curl_setopt($Curl_Obj, CURLOPT_POST, 0); 



$stAddr = str_replace(' ','+', $ast);
$City = str_replace(' ','+', $ac);
$address = "$stAddr,+$City,+{$aco},+{$ap}";
$address = str_replace(' ', '+', $address);
$ListURL = "{$ListURLRoot}?address=$address$ListURLSuffix";
curl_setopt ($Curl_Obj, CURLOPT_URL, $ListURL);
$output = curl_exec ($Curl_Obj);
GetLocation($output);

function GetLocation($output)  {
    global $db_connect;
    $Loc = json_decode($output, true);
    if(isset($Loc))  {
        $i = 1;
        while ($Loc['status']=="OVER_QUERY_LIMIT") {
            if ($i > 14) {
                echo "Geocode failed!";
                exit();
            }
            sleep(1);
            $i++;

        }
        if(isset($Loc['status']) && stristr($Loc['status'], 'OK'))  {
            if(isset($Loc['results'][0]['geometry']['location']))  {
                $Lat = $Loc['results'][0]['geometry']['location']['lat'];
                $Lng = $Loc['results'][0]['geometry']['location']['lng'];
            }
        }
        else {
            error_log($Loc['status'], 0);
            echo "Unknown error occured!";
            exit();
        }
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

主要是谷歌问题。尽量避免服务器端地理编码。而是使用客户端javascript解算器。

相关问题