Genadinik
Genadinik

Reputation: 18639

Can't Parse RSS XML with PHP

I have some RSS that I am trying to parse from this URL: http://weather.yahooapis.com/forecastrss?w=12797541

But when I try to parse it using PHP doing this:

$yahoo_response = new SimpleXMLElement($yahoo_url , 0, true);

echo $yahoo_response->rss->channel->item->title;
echo $yahoo_response->rss->channel->item->description;

Nothing gets outputed. Does anyone know what I am doing wrong here? I just need the current forecast bit.

Thanks, Alex

Upvotes: 0

Views: 362

Answers (2)

Gordon
Gordon

Reputation: 316969

The root element is <rss>, which is represented by the SimpleXmlElement you loaded into $yahoo_response.

echo $yahoo_response->getName();  // rss

You are trying to do <rss><rss> when you should do:

echo $yahoo_response->channel->item->title;
echo $yahoo_response->channel->item->description;

Upvotes: 2

Byron Whitlock
Byron Whitlock

Reputation: 53861

I've found that RSS feeds are best parsed using a library.

I've had much success with magpieRSS.

But your immediate problem is that you are passing a URL to simplexml, instead of the xml itself.

try $xml = file_get_contents($yahoo_url); first.

Upvotes: 1

Related Questions