Php 如何正确使用谷歌距离矩阵Web服务

Php 如何正确使用谷歌距离矩阵Web服务,php,google-maps-api-3,Php,Google Maps Api 3,我曾尝试在PHP中使用Google Distance Matrix Web服务(http://code.google.com/apis/maps/documentation/distancematrix/#XML) 我从PHP向该Web服务发送请求,并尝试获取响应xml。但它返回的xml文件不存在。相同的url在浏览器中也可以正常工作 PHP文件: $request_url = "http://maps.googleapis.com/maps/api/distancematrix/xml?ori

我曾尝试在PHP中使用Google Distance Matrix Web服务(http://code.google.com/apis/maps/documentation/distancematrix/#XML)

我从PHP向该Web服务发送请求,并尝试获取响应xml。但它返回的xml文件不存在。相同的url在浏览器中也可以正常工作

PHP文件:

$request_url = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=11.498507+77.245688&destinations=11.497208+77.244656&sensor=false";

if (file_exists($request_url)) {
$xml = simplexml_load_file($request_url);
print_r($xml);
} 
else {
exit('Failed to open request_url.');
}
输出:打开请求url失败

如何正确使用来自距离矩阵Web服务的响应XMl。

对文件_exists()的调用返回false。我认为这是因为file_exists()不适用于远程文件,但我对此不确定。我建议你这样利用:

$request_url = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=11.498507+77.245688&destinations=11.497208+77.244656&sensor=false";

$content = file_get_contents($request_url);
if (false !== $content) {
    echo $content;
} 
else {
    exit('Failed to retrieve contents from request_url.');
}