【发布时间】:2017-11-14 06:21:19
【问题描述】:
我有一个使用 CURL 和 Google Maps API 返回地址坐标的函数。
代码如下:
function get_coordinates($address_string) {
$address = urlencode($address_string);
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=" . $address . "&key=" . $api_key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
$status = $response_a->status;
return $response;
}
该代码适用于我和我使用它的 99% 的网络服务器 - 但对于大约 1% 的服务器,Google 会返回错误消息:
Your client has issued a malformed or illegal request. That’s all we know.
我已经检查过,Google API 密钥是正确的,PHP CURL 已启用并且 PHP 版本与它正在使用的 PHP 版本匹配。
谁能想到任何其他可能导致 Google 返回此消息的原因?
【问题讨论】:
-
对我来说,问题在于添加 CURLOPT_TIMEOUT 值。由于某种原因,添加超时将返回操作错误,而在没有超时值的情况下发出请求,将返回成功的响应。
标签: php google-maps curl google-api