Google Maps API会返回“无效请求”在一个有效的请求 [英] Google Maps API returns "Invalid Request" in a valid request

查看:168
本文介绍了Google Maps API会返回“无效请求”在一个有效的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为我的数据库反转地理编码一堆地址。我有大约200个地址,其中一半工作像一个魅力..我没有把谷歌地图请求在一个循环,它的工作。有大约100个地址返回了无效请求消息。我回应请求url并将其放入浏览器中,并返回Status:OK和所有数据。为什么我的脚本返回一个无效的请求,另一个浏览器 - 标签返回OK?



这就是我使用的函数:

  function getLatLon($ strasse,$ plz){
$ inhalt = simplexml_load_file('http://maps.googleapis.com/maps/api/geocode/xml?address = $大街。 '+安培' $ PLZ。 ';传感器=真');

$ geo ['lat'] = $ inhalt-> result->> geometry-> location-> lat;
$ geo ['lon'] = $ inhalt-> result->> geometry-> location-> lng;

返回$ geo;



$ b $ p
$ b

请求url清晰如下:
http:/ /maps.googleapis.com/maps/api/geocode/xml?address=Dreik%C3%B6nigstra%C3%9Fe+12,+Erlangen&sensor=true


$ b $如果有人可以帮助我或检测到错误,那么这真是太棒了:)
问候,
Philipp

解决方案

我很确定这个错误是由你未发送的德语字符造成的。当你使用浏览器的时候,URL会被自动编码,但不会自动被PHP脚本编码。



使用 rawurlencode
$ b

  $ inhalt = simplexml_load_file('http://maps.googleapis.com/maps/api/geocode/xml?'。
'address ='。rawurlencode($ strasse)。', +'。
rawurlencode($ plz)。'& sensor = true');


i am trying to reverse geocode a bunch of addresses for my database. I have about 200 addresses, and half of them worked like a charm.. i did put the google maps request in a loop and it worked. There were about 100 adresses that returned a "INVALID REQUEST" message. I echoed the request url and put it in my browser and it returned a "Status: OK" and all the data. Why does my script return a invalid request and another browser-tab returns OK?

Thats the function i used:

function getLatLon($strasse, $plz) {
    $inhalt = simplexml_load_file('http://maps.googleapis.com/maps/api/geocode/xml?address='.$strasse.',+'.$plz.'&sensor=true');

    $geo['lat'] = $inhalt->result->geometry->location->lat;
    $geo['lon'] = $inhalt->result->geometry->location->lng;

    return $geo;
}

The request url in clear looks like this: http://maps.googleapis.com/maps/api/geocode/xml?address=Dreik%C3%B6nigstra%C3%9Fe+12,+Erlangen&sensor=true

Would be awesome if someone could help me or detect an error :) Regards, Philipp

解决方案

I am pretty sure the error is caused by the german characters you are sending unencoded. When you do it by browser the URL will be encoded automatically, but not automatically by your PHP-script.

Use rawurlencode for this :

$inhalt = simplexml_load_file('http://maps.googleapis.com/maps/api/geocode/xml?'.
                              'address='.rawurlencode($strasse).',+'.
                              rawurlencode($plz).'&sensor=true');

这篇关于Google Maps API会返回“无效请求”在一个有效的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆