我目前正在尝试解析MapQuest Traffic API,但是当我尝试显示事件时,没有任何内容出现,如果我在php中执行“if empty”,则返回空。
以下是代码:
<?php
$mysongs = simplexml_load_file("http://www.mapquestapi.com/traffic/v1/incidents?key=Fmjtd%7Cluuan1u2nh%2C2a%3Do5-96rw5u&callback=handleIncidentsResponse&boundingBox=$_GET[a], $_GET[b], $_GET[c], $_GET[d]&filters=construction,incidents&inFormat=kvp&outFormat=xml");
echo $mysongs->Incidents[0]->Incident[0]->fullDesc;
?>
我传递的参数:?a = 33.352532499999995&amp; b = -118.2324383&amp; c = 34.352532499999995&amp; d = -117.2324383。
提前致谢!
答案 0 :(得分:0)
这里simplexml_load_file没有加载你所有的xml数据所以,我创建了一个名为test.xml的xml文件,然后从test.xml加载了数据。现在,您可以打印所需的数据。
<?php
$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];
$d = $_GET['d'];
$xml_feed_url = 'http://www.mapquestapi.com/traffic/v1/incidents?key=Fmjtd|luuan1u2nh%2C2a%3Do5-96rw5u&callback=handleIncidentsResponse&boundingBox='.$a.','.$b.','.$c.','.$d.'&filters=construction,incidents&inFormat=kvp&outFormat=xml';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
$xml2 = new SimpleXMLElement($xml);
$xml2->asXML("test.xml");
$xml2->asXML();
$mysongs = simplexml_load_file("test.xml");
print_r($mysongs);
?>