Google 地理编码服务返回错误 400 错误请求
我一直在尝试从谷歌的地理编码服务获取 json 响应。我正在使用 PHP。我尝试使用 fopen,然后我在另一个 stackoverflow 问题中读到我应该使用 file_get_contents,但也不起作用。然后我继续搜索,发现另一个论坛上有人说如果我使用 CURL,我会得到更好的解决方案,所以我更改了代码但不起作用。在所有情况下,我都收到“错误 400:错误请求。您的客户端发出了格式错误或非法的请求。”
我的代码是这样的:
$jsonUrl = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityName . "&sensor=false";
$geocurl = curl_init();
curl_setopt($geocurl, CURLOPT_URL, $jsonUrl);
curl_setopt($geocurl, CURLOPT_HEADER,0); //Change this to a 1 to return headers
curl_setopt($geocurl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($geocurl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($geocurl, CURLOPT_RETURNTRANSFER, 1);
$geofile = curl_exec($geocurl);
然后我打印内容并收到错误消息。
有什么想法吗?
非常感谢。
I've been trying to get a json response from google's geocoding service. I'm using PHP. I was trying with fopen, then I read in another stackoverflow question that I should use file_get_contents, but didn't worked too. Then I keep searching and found someone in another forum who said I would a better solution if I use CURL so I changed my code and is not working. In all cases I received an "Error 400: Bad Request. Your client has issued a malformed or illegal request."
My code is this:
$jsonUrl = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityName . "&sensor=false";
$geocurl = curl_init();
curl_setopt($geocurl, CURLOPT_URL, $jsonUrl);
curl_setopt($geocurl, CURLOPT_HEADER,0); //Change this to a 1 to return headers
curl_setopt($geocurl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($geocurl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($geocurl, CURLOPT_RETURNTRANSFER, 1);
$geofile = curl_exec($geocurl);
Then I print the content and got the error message.
Any ideas?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我想通了。
我的 $cityName 变量是这样的:
逗号和“NL”之间的空格。我使用 str_replace 将“”更改为“+”并获取有效的网址,如文档中所示:
http://code.google.com/intl/es/apis/maps/documentation/geocoding/
您好,非常感谢您的帮助!
Well, I figured it out.
My $cityName variable was this:
The white space between the comma and "NL". I used str_replace to change " " for "+" and get a valid url like in the documentation:
http://code.google.com/intl/es/apis/maps/documentation/geocoding/
Greetings and thanks a lot for your help!
我认为你错过了 API-Key
顺便说一句,我只是使用 file_get_contents() 作为 Google 的地理编码器,因为没有你必须设置的特殊标头,或者你必须遵循的 http 重定向等。
I think you're missing the API-Key
Btw, I simply use file_get_contents() for Google's geocoder, since there are no special headers you have to set, or http-redirects you have to follow, etc.
仅供参考,我刚刚遇到了这个问题,我的问题是我错误地输入了“地址”查询字符串参数(我将其输入为“地址”而不是“地址”)。
FYI, I just ran into this and my issue was that I had incorrectly typed the "address" query string parameter (I typed it as "adress" rather than "address").