Google地理编码服务会返回错误400错误请求 [英] Google Geocoding service returns Error 400 Bad Request
问题描述
我一直在试图从google的地理编码服务获取json响应。我使用PHP。我试图与fopen,然后我阅读在另一个stackoverflow问题,我应该使用file_get_contents,但没有工作。然后我继续搜索,发现有人在另一个论坛谁说,如果我使用CURL我会更好的解决方案,所以我改变了我的代码,不工作。在所有情况下,我收到了错误400:错误的请求,您的客户发出了错误或非法的请求。
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."
我的代码是:
$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.
任何想法?
非常感谢。
推荐答案
好吧,我想出来了。
我的$ cityName变量是:
My $cityName variable was this:
$cityName = "Monterrey, NL";
逗号和NL之间的空格。我使用str_replace为+更改,并获得有效的网址,如文档中所示:
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!
这篇关于Google地理编码服务会返回错误400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!