簡體   English   中英

調用Google Directions API時,PHP在mysql循環上崩潰

[英]PHP crashes on mysql loop when calling google directions api

我正在一個MySQL搜索查詢中運行以下函數,該查詢通常可以找到大約100個結果。 該函數使用Google API檢索兩個位置之間的距離。

問題看起來像是循環使腳本崩潰超過10個結果。

有什么辦法可以解決此問題?

暫停Google回撥?

任何有幫助的建議。

function get_driving_information($start, $finish, $raw = false)
{
    if(strcmp($start, $finish) == 0)
    {
        $time = 0;
        if($raw)
        {
            $time .= ' seconds';
        }

        return array('distance' => 0, 'time' => $time);
    }

    $start  = urlencode($start);
    $finish = urlencode($finish);

    $distance   = 'unknown';
    $time       = 'unknown';

    $url = 'http://maps.googleapis.com/maps/api/directions/xml?origin='.$start.'&destination='.$finish.'&sensor=false';
    if($data = file_get_contents($url))
    {
        $xml = new SimpleXMLElement($data);

        if(isset($xml->route->leg->duration->value) AND (int)$xml->route->leg->duration->value > 0)
        {
            if($raw==true)
            {
                $distance = (string)$xml->route->leg->distance->text;
                $time     = (string)$xml->route->leg->duration->text;
            }
            if($raw=="miles")
            {
                $distance = (string)$xml->route->leg->distance->value / 1000 / 1.609344;
                $distance = number_format($distance,1)." miles";
                $time     = (string)$xml->route->leg->duration->text;
            }
            else
            {
                $distance = (int)$xml->route->leg->distance->value / 1000 / 1.609344; 
                $time     = (int)$xml->route->leg->duration->value;
            }
        }
        else
        {
            throw new Exception('Could not find that route');
        }

        //return array('distance' => $distance, 'time' => $time);
        return $distance;
    }
    else
    {
        throw new Exception('Could not resolve URL');
    }
}

每秒有10個請求,請在此處查看有關限制的更多信息: https : //developers.google.com/maps/documentation/directions/usage-limits

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM