0

I'm trying to get latitude and longtitude from google map by giving address. But it's giving error. When I directly copy url to browser url bar. it's giving correct result. If anybody know the answer.

Here is my code.

<?php 
$key = '[AIzaSyAoPgQlfKsBKQcBGB01cl8KmiPee3SmpU0]';

$opt = array (
'address'   => urlencode('Kolkata,India, ON') ,
'output'    => 'xml' 
);

$url = 'http://maps.google.com/maps/geo?q='.$opt['address'].'&output='.$opt['output'].'&oe=utf8&key='.$key;

$dom = new DOMDocument();
$dom->load($url);

$xpath = new DomXPath($dom);
$xpath->registerNamespace('ge', 'http://earth.google.com/kml/2.0');

$statusCode = $xpath->query('//ge:Status/ge:code');

if ($statusCode->item(0)->nodeValue == '200') {

$pointStr = $xpath->query('//ge:coordinates');
$point = explode(",", $pointStr->item(0)->nodeValue);

$lat = $point[1];
$lon = $point[0];

echo '<pre>';
echo 'Lat: '.$lat.', Lon: '.$lon;
echo '</pre>';
}

?>

Following error has occured:

Warning: DOMDocument::load(http://maps.google.com/maps/geo?      q=Kolkata%252CIndia%252C%2BON&output=xml&oe=utf8&key=%5BAIzaSyAoPgQlfKsBKQcBGB01cl8KmiPee3SmpU0%5D) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\xampp\htdocs\draw\draw.php on line 19

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "http://maps.google.com/maps/geo?q=Kolkata%252CIndia%252C%2BON&output=xml&oe=utf8&key=%5BAIzaSyAoPgQlfKsBKQcBGB01cl8KmiPee3SmpU0%5D" in C:\xampp\htdocs\draw\draw.php on line 19

Notice: Trying to get property of non-object in C:\xampp\htdocs\draw\draw.php on line 26
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Lakhan
  • 1
  • 1
  • 1

1 Answers1

2

DOMDocument::load() requires allow_url_fopen to be true.

Check your php.ini-settings and switch it to 1.

See also:

http://php.net/filesystem.configuration.php#ini.allow-url-fopen

What would cause DOMDocument.load to fail loading XML from a URL that is accessible?

Community
  • 1
  • 1
feeela
  • 29,399
  • 7
  • 59
  • 71