0

我试图通过提供地址从谷歌地图获取纬度和经度。但它给出了错误。当我直接将 url 复制到浏览器 url 栏时。它给出了正确的结果。如果有人知道答案。

这是我的代码。

<?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>';
}

?>

出现以下错误:

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
4

1 回答 1

2

DOMDocument::load()要求allow_url_fopen为真。

检查您的php.ini-settings 并将其切换到1.

也可以看看:

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

什么会导致 DOMDocument.load 从可访问的 URL 加载 XML 失败?

于 2012-10-10T10:29:16.163 回答