-1

Google Geocode use to work for me but does not anymore. Everything I've tried returns false. It doesn't even give a clue as to why. I've tried changing the API key, I've tried different methods to connect (I prefer using simplexml_load_file). It does work if I type the URL directly into the a browser window but not from my website or even from my localhost. Does anyone have suggestions on what to try next or an alternative to Googles Geocode service?

Here is some code that I've been using that Json / XML results.

//URL SAVE ADDRESS
date_default_timezone_set("America/Chicago");
$address = "1600 Amphitheatre Parkway, Mountain View, CA 94043";
$address = urlencode($address);

// GOOGLE URL
$url = "https://maps.googleapis.com/maps/api/geocode/xml?address=". $address ."&sensor=false&key=AIzaSyDrGe_qr4ofea8WhZFhnPGsXNQtTiQwGhw";
echo $url; //TEMP
$xml = simplexml_load_file($url) OR $ex['tblx'] = "Unable to load XML from Googleapis.";
if(empty($xml)) {
    $ex['tblx'] = "Googleapis return no values.";
} elseif("" == $xml) {
    $ex['tblx'] =  "Geocode problem with address, revise and retry.";
} elseif("OK" != $xml->status) {
    $ex['tblx'] = "Geocode: " . ("ZERO_RESULTS" == $xml->status? "Not Found": substr($xml->status,0,30));
} else{ 
        $dvtby0 = $uca['y0'] = (double)$xml->result->geometry->location->lat;  //x
        $dvtbx0 = $uca['x0'] = (double)$xml->result->geometry->location->lng;
        $dvtby1 = $uca['y1'] = (double)$xml->result->geometry->viewport->southwest->lat;  //a
        $dvtbx2 = $uca['x1'] = (double)$xml->result->geometry->viewport->southwest->lng;
        $dvtbx1 = $uca['x2'] = (double)$xml->result->geometry->viewport->northeast->lng;  //b       
        $dvtby2 = $uca['y2'] = (double)$xml->result->geometry->viewport->northeast->lat;        
}//endif

if($ex) {
    var_dump($ex);
}
die("did it work?");
Brian
  • 3,850
  • 3
  • 21
  • 37
  • It doesn't need braces there and it doesn't make code more or less readable. It just takes up space. Why the down vote? What is wrong with my question? What is this website for it not to ask and collaborate on problems? – David in Topeka Kansas Jan 31 '18 at 18:20
  • Why put braces around one statement? Why take up an extra line? With just your changes you added 4 lines of code. It adds up. I don't have a problem reading left to right until I reach the semicolon. Reads fine to me. I do this all the time in my code – David in Topeka Kansas Jan 31 '18 at 20:34

2 Answers2

2

geocoder.ca for north america opencage or geocode.xyz for the world pelias as standalone

there are lots of options in 2018

Ervin Ruci
  • 829
  • 6
  • 10
0

The problem here turned out to be the version of PHP. It's really weird and hard to troubleshoot. simplexml_load_file($url) was not returning an object. It wasn't even returning false. The only clue that it was something other that code problem. Upgrade to version 5.6 and it works ok.

  • As far as I know Google migrated to new root certificates. Probably your issue was that the old version of PHP didn't have required certificates to establish secure connection. Please have a look at the following issue that explains this subject: https://issuetracker.google.com/issues/67842936 – xomena Apr 01 '18 at 11:20