Question

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

Was it helpful?

Solution

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');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top