Cannot read yahoo xml feed in php

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

this is my simple code:

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

but I show NULL :

adam@cka: php test.php
NULL

is my xml broken? or is there a better function in php for reading an xml file?

I see that elment exists in the XML file, and I downloaded the file correctly on my computer.

+5
source share
2 answers

SimpleXML returns an object, not an array. Try the following:

<?php
 $xml = simplexml_load_file('http://news.yahoo.com/rss/us');
 var_dump($xml->channel);
?>
+6
source

Something like that:

$ 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;
} 
0

All Articles