duanqiao1961 2015-12-12 15:45
浏览 61
已采纳

调用google指令api时,PHP在mysql循环中崩溃

I am running the following function within a MySQL search query which generally find around 100 results. the function retrieves distance between two location using google api.

the problem looks like the loop is crashing the script anything over 10 results.

is there any way to fix this issue?

pause for the google call back?

any suggestion helpful.

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');
    }
}
  • 写回答

1条回答 默认 最新

  • douchuituo3032 2015-12-12 15:50
    关注

    there is a limit 10 requests per second, check more about limits here: https://developers.google.com/maps/documentation/directions/usage-limits

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?