Google 地理编码服务返回错误 400 错误请求

发布于 2024-10-25 13:07:45 字数 801 浏览 4 评论 0原文

我一直在尝试从谷歌的地理编码服务获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静被遗忘 2024-11-01 13:07:45

好吧,我想通了。

我的 $cityName 变量是这样的:

$cityName = "Monterrey, NL";

逗号和“NL”之间的空格。我使用 str_replace 将“”更改为“+”并获取有效的网址,如文档中所示:

http://code.google.com/intl/es/apis/maps/documentation/geocoding/

您好,非常感谢您的帮助!

Well, I figured it out.

My $cityName variable was this:

$cityName = "Monterrey, NL";

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!

街角卖回忆 2024-11-01 13:07:45

我认为你错过了 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.

节枝 2024-11-01 13:07:45

仅供参考,我刚刚遇到了这个问题,我的问题是我错误地输入了“地址”查询字符串参数(我将其输入为“地址”而不是“地址”)。

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").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文