【发布时间】:2017-08-14 22:32:23
【问题描述】:
我有点问题,
我需要从道路名称(我提供的)中获取一个地方的坐标,我尝试使用 file_get_contents 和 json_decode 来完成,但其中一些不起作用(返回一个 stdClass ZERO_RESULTS)!
如果我在浏览器中复制粘贴网址(完全相同),它会向我发送正确的数据,不知道为什么。
如果我尝试使用另一个道路名称,它会起作用。
显然我已经尝试清除缓存,但没有结果,更改导航器也不起作用,也在多个服务器上尝试过,但仍然没有结果,尝试使用 CURL (as provided as an answer in a post relating the same problem),但仍然,没有结果。
我很迷茫,希望你能帮我解决这个问题。
这里是代码。
提供的数据来自法国路名,想看的可以在$a中加“法国”
数据
// Not Working
$a = "17 bis Les Chaises";
$b = "77510";
// Working
$a = "2 rue Jean Moulin";
$b = "70300";
生成的链接
// Not Working
https://maps.google.com/maps/api/geocode/json?address=17+bis+Les+Chaises+77510
// Working
https://maps.google.com/maps/api/geocode/json?address=2+rue+Jean+Moulin+70300
卷曲
$url = "https://maps.google.com/maps/api/geocode/json?address=".str_replace(" ","+",$a)."+".$b;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
print_r($response_a);
file_get_contents
$j = json_decode(file_get_contents("https://maps.google.com/maps/api/geocode/json?address=".str_replace(" ","+",$a)."+".$b),true);
print_r("https://maps.google.com/maps/api/geocode/json?address=".str_replace(" ","+",$a)."+".$b);echo "\n\n\n\n\n\n\n";
print_r($j["results"][0]["geometry"]["location"]["lat"].",".$j["results"][0]["geometry"]["location"]["lng"]);
var_dump(file_get_contents("https://maps.google.com/maps/api/geocode/json?address=".str_replace(" ","+",$a)."+".$b));
感谢阅读。
【问题讨论】:
标签: php google-maps curl google-maps-api-3