Can't Parse RSS XML with PHP

北城以北 提交于 2020-01-07 03:12:05

问题


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


回答1:


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;



回答2:


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.



来源:https://stackoverflow.com/questions/5101231/cant-parse-rss-xml-with-php

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