使用SimpleXML解析XML不会返回任何内容

2022-04-13 00:00:00 php simplexml

我当前正在尝试分析MapQuest流量API,但当我尝试显示事件时,什么也不显示,如果我在php中显示"If Empty",它将返回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&;b=-118.2324383&;c=34.352532499999995&;d=-117.2324383.

提前谢谢!

test.xml

此处推荐答案_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);      
?>

相关文章