Search code examples
phpxmlgeocodegoogle-geocoding-api

Url not loading error on geocoding requests


I previously had a Google geocoding script working to extract longitude and latitude using local addresses in a database.

In the last 6 months I've switched hosts, and apparently Google has implemented a new forward geocoder. Now it just returns the url not loading error from the xml script call.

I've tried everything to get my code working. Even sample coding from other websites won't work on my server. What am I missing? Is there possibly a server side setting that is blocking this from executing properly?

Attempt # 1:

$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?new_forward_geocoder=true&address=1600+Amphitheatre+Parkway,+Mountain+View,+CA";
echo $request_url;
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
return $status;

Simply returns url not loading. I have tried with and without the new_forwad_geocoder. I have also tried with and without https.

The $request_url string DOES return proper results if you simply copy and paste it to a browser.

Also tried this just to see if I could get a file to return. Attempt 2:

$request_url = "http://maps.googleapis.com/maps/api/geocode/json?new_forward_geocoder=true&address=1600+Amphitheatre+Parkway,+Mountain+View,+CA";//&sensor=true
echo $request_url."<br>";
$tmp = file_get_contents($request_url);
echo $tmp;

Any idea what could be causing the connection failure?


Solution

  • I wasn't ever able to get this working with XML again and the file_get_contents call was the culprit I'm almost positive.

    I've posted what I did get to work with JSON/Curl (below) in case anyone has similar issues.

    Ultimately I think the problems I ran into had to do with an upgrade to our Apache version on the server; and some of the default settings related to file_get_contents and fopen being more restrictive. I haven't confirmed this though.

    This code does work though:

    class geocoder{
        static private $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=";
    
        static public function getLocation($address){
            $url = self::$url.$address;
    
            $resp_json = self::curl_file_get_contents($url);
            $resp = json_decode($resp_json, true);
            //var_dump($resp);
            if($resp['status']='OK'){
              //var_dump($resp['results'][0]['geometry']['location']);
                //echo "<br>";
                //var_dump($resp['results'][0]['geometry']['location_type']);
                //echo "<br>";
                //var_dump($resp['results'][0]['place_id']);
    
                return array ($resp['results'][0]['geometry']['location'], $resp['results'][0]['geometry']['location_type'], $resp['results'][0]['place_id']);
            }else{
                return false;
            }
        }
    
        static private function curl_file_get_contents($URL){
            $c = curl_init();
            curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($c, CURLOPT_URL, $URL);
            $contents = curl_exec($c);
            curl_close($c);
    
            if ($contents) return $contents;
                else return FALSE;
        }
    }
    
    $Address = "1600 Amphitheatre Parkway, Mountain View, CA";
    $Address = urlencode(trim($Address));
    
    list ($loc, $type, $place_id) = geocoder::getLocation($Address);
    //var_dump($loc);
    $lat = $loc["lat"];
    $lng = $loc["lng"];
    echo "<br><br> Address: ".$Address;
    echo "<br>Lat: ".$lat;
    echo "<br>Lon: ".$lng;
    echo "<br>Location: ".$type;
    echo "<br>Place ID: ".$place_id;