【发布时间】:2013-02-11 04:38:00
【问题描述】:
尝试使用SimpleXMLElement 获取 RSS 提要,但我不完全了解如何执行此操作。我的代码如下:
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"]=> object(SimpleXMLElement)#2 (1) { ["TITLE"]=> string(17) "永久移动" } ["BODY"]=> 对象(SimpleXMLElement)#3(3){[“@attributes”]=>数组(2){ ["BGCOLOR"]=> 字符串(7) "#FFFFFF" ["TEXT"]=> 字符串(7) "#000000" } ["H1"]=> string(17) "永久移动" ["A"]=> string(4) "这里" } }
我已尝试将上面的代码更改为此,但它没有输出任何内容,并且我得到一个 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");
谁能告诉我我到底做错了什么?
【问题讨论】: