PHP + Google Geocode API

5

Follow the method code:

function GetGeoCode($address) {
    $geo= array();

    $geocode = file_get_html('http://maps.google.com/maps/api/geocode/json?address=' . $address . '&sensor=false');
    $output = json_decode($geocode);
    echo 'http://maps.google.com/maps/api/geocode/json?address=' . $address . '&sensor=false';
    $lat = $output->results[0]->geometry->location->lat;
    $long = $output->results[0]->geometry->location->lng;

    $geo['lat']=$lat;
    $geo['long']=$long;

    return $geo;
}

The purpose of this method is to return latitude and longitude according to a given address entered as a parameter. However, after performing the test with some addresses the error of not returning the data. I ran the test to copy the created url and paste it into the browser, which worked correctly.

Can anyone tell me why this happens?

    
asked by anonymous 01.11.2015 / 06:34

1 answer

2

I made some tests, first, instead of file_get_html I used the code below file_get_contents , however what I noticed in the test at least using this method, is that you need to send the parameter with the spaces

  

Suggested reading: The Google More Geocoding API

    
01.11.2015 / 11:57