从BING Routes访问行程距离API XML响应

时间:2017-03-25 16:55:00

标签: php xml routes bing bing-api

- 回答,见下文 -

如何从Bing Routes API响应中访问并回显行程距离金额?

有很多关于XML的教程:http://www.php.net/manual/en/simplexml.examples-basic.php

以下是响应示例(行程距离靠近底部): https://msdn.microsoft.com/en-us/library/gg636957.aspx

以下是调用响应并检索行程距离的代码示例。

$calculateURL = $drivingRoutesBaseURL."?wayPoint.1=".$waypoint1."&wayPoint.2=".$waypoint2."&output=xml&key=".$bingKey;

//发送请求   //从Locations API获取响应并将其存储在字符串

$output = file_get_contents($calculateURL);

//基于XML字符串

创建XML元素
$response = new SimpleXMLElement($output);

//从响应中获取行程距离元素

$travelDistance = $response->ResourceSets->ResourceSet->Resources->Route->TravelDistance;

echo $travelDistance;

1 个答案:

答案 0 :(得分:0)

此行返回具有行进距离的对象。

$travelDistance = $response->ResourceSets->ResourceSet->Resources->Route->TravelDistance;

echo $travelDistance;