0

I have a simple function that converts address string into longitude and latitude using simple XML from Google. I get this weird warning error once in a while:

Warning: simplexml_load_file(http://maps.googleapis.com/maps/api/geocode/xml?address=&sensor=true): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in /search_results.php on line 193

Warning: simplexml_load_file(): I/O warning : failed to load external entity "http://maps.googleapis.com/maps/api/geocode/xml?address=&sensor=true" in /search_results.php on line 193 url not loading

Is there a better way to do this to avoid this error?

My code is below:

$geo_address = urlencode($address);

$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$geo_address."&sensor=true";

  $xml =  simplexml_load_file($request_url) or die ("url not loading");
  $status = $xml->status;
  if ($status=="OK") {
    $lat = $xml->result->geometry->location->lat;
    $lon = $xml->result->geometry->location->lng;
  }
kjhughes
  • 106,133
  • 27
  • 181
  • 240
Edward
  • 77
  • 6
  • It seems your internal php file `(search_results.php)` has the problem `HTTP/1.0 500 Internal Server Error` , and you can try [this](http://stackoverflow.com/questions/10524748/php-file-get-contents-500-internal-server-error) out – bjiang Jan 27 '15 at 17:48

1 Answers1

1

It seems as if you're trying to geocode an empty address. You should verify the $geo_address length before querying google

$geo_address = urlencode($address);

if(strlen($geo_address)>0) {

    $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$geo_address."&sensor=true";

    $xml =  simplexml_load_file($request_url) or die ("url not loading");
    $status = $xml->status;
    if ($status=="OK") {
      $lat = $xml->result->geometry->location->lat;
      $lon = $xml->result->geometry->location->lng;
    }
} else {
  die('Invalid address');
}

you should replace the die() with some other error management logic that suits your app.

ffflabs
  • 17,166
  • 5
  • 51
  • 77