尝试使用SimpleXMLElement
抓取RSS Feed,但我不完全了解如何执行此操作。我的代码如下:
function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
var_dump($x);
}
getFeed("http://feedproxy.google.com/themeforest");
输出以下内容:
object(SimpleXMLElement)#1(2){[“HEAD”] =>对象(的SimpleXMLElement)#2 (1){[“TITLE”] => string(17)“Moved Permanently”} [“BODY”] => object(SimpleXMLElement)#3(3){[“@attributes”] =>数组(2){ [ “BGCOLOR”] => string(7)“#FFFFFF”[“TEXT”] => string(7)“#000000”} [ “H1”] => string(17)“永久移动”[“A”] => string(4)“here”}}
我已经尝试将上面的代码更改为此,但是输出NOTHING并且我得到了一个foreach错误:
function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo '
<ul>';
foreach($x->channel->item as $entry) {
echo '
<li>
<a href="' . $entry->link . '" title="' . $entry->title . '">' . $entry->title . '</a>
</li>';
}
echo '
</ul>';
}
getFeed("http://feedproxy.google.com/themeforest");
有人可以告诉我,我在这里做错了什么?
答案 0 :(得分:0)
无法像对象一样访问它。请阅读这里的第一个例子: http://www.php.net/manual/en/simplexmlelement.children.php