Kas*_*ai. 5 php curl google-maps google-maps-api-3
我有点问题,
我需要从道路的名称(我提供的)获取一个地方的坐标,我尝试用file_get_contents&来做json_decode,但是其中一些不起作用(返回带有ZERO_RESULTS的stdClass )!
如果我复制粘贴网址(exaclty相同)在浏览器中,它发送给我正确的数据,不知道为什么.
如果我尝试另一个道路名称,它的工作原理.
显然我已经尝试清除缓存,但没有结果,更改导航器也不起作用,尝试在多个服务器上,但仍然没有结果,尝试用CURL做(在一篇文章中作为答案提供相关的同样的问题),但仍然没有结果.
我很失落,我希望你能帮助我.
这里的代码.
提供的数据来自法国道路名称,如果您想查看,可以在$ a中添加"法国"
数据
// Not Working
$a = "17 bis Les Chaises";
$b = "77510";
// Working
$a = "2 rue Jean Moulin";
$b = "70300";
Run Code Online (Sandbox Code Playgroud)
链接生成
// 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
Run Code Online (Sandbox Code Playgroud)
卷曲
$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);
Run Code Online (Sandbox Code Playgroud)
的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));
Run Code Online (Sandbox Code Playgroud)
谢谢阅读.
我发现如果您将 +France 添加到搜索条件中,您的非工作请求将不起作用。
// 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=17+bis+Les+Chaises+77510+France
Run Code Online (Sandbox Code Playgroud)