Parsing XML with SimpleXML returns nothing

只谈情不闲聊 提交于 2019-12-13 05:36:04

问题


I'm currently trying to parse the MapQuest Traffic API, but when I try to display an incident, nothing appears, and if I do "if empty" in php, it returns empty.

Here's the code:

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

The parameters I'm passing: ?a=33.352532499999995&b=-118.2324383&c=34.352532499999995&d=-117.2324383.

Thanks in advance!


回答1:


Here simplexml_load_file not loading all your xml data so, i created a xml file with name test.xml and then loaded data from test.xml. Now you can print data what you need.

<?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);      
?>


来源:https://stackoverflow.com/questions/13945105/parsing-xml-with-simplexml-returns-nothing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!