5

i am trying to read the yahoo rss (http://news.yahoo.com/rss/us) in php using the xml function

this is myvery simple code:

 $xml = simplexml_load_file('xml.xml');
 var_dump($xml['channel']);

but i shows NULL:

adam@cka: php test.php
NULL

is my XML broken? or there's a better function in php to read xml file?

i can see the elment exists in the XML file and i downloaded the file correctly in my computer.

Adam
  • 1,205
  • 10
  • 20
  • [simplexml_load_file](http://php.net/manual/en/function.simplexml-load-file.php) return an object not an array: `Returns an object of class SimpleXMLElement with properties containing the data held within the XML document, or FALSE on failure.` – Book Of Zeus Dec 05 '11 at 00:48

2 Answers2

6

SimpleXML returns an object, not an array. Try this:

<?php
 $xml = simplexml_load_file('http://news.yahoo.com/rss/us');
 var_dump($xml->channel);
?>
Tak
  • 11,428
  • 5
  • 29
  • 48
0

Something like this:

$rss = simplexml_load_file('http://news.yahoo.com/rss/us');

echo $rss->channel->title;

foreach ($rss->channel->item as $item) {
   echo $item->link. " -- " .$item->title;
   echo $item->pubDate;
   echo $item->description;
} 
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162