1

我使用了人们建议尝试解析 RSS 的几种不同脚本,包括 Magpie 和 PHP 中的 SimpleXML 功能。但似乎没有人能很好地处理 RSS 2.0,因为他们不会把完整的内容块还给我。有没有人建议阅读http://chacha102.com/feed/上的提要,并获取完整内容而不仅仅是描述?

4

2 回答 2

2

无需阅读任何有关 rss“内容”名称空间以及如何使用它的文档,这里是一个有效的 SimpleXML 脚本。诀窍是在检索内容时使用命名空间。

/* the namespace of rss "content" */
$content_ns = "http://purl.org/rss/1.0/modules/content/";

/* load the file */
$rss = file_get_contents("http://chacha102.com/feed/");
/* create SimpleXML object */
$xml = new SimpleXMLElement($rss);
$root=$xml->channel; /* our root element */

foreach($root->item as $item) { /* loop over every item in the channel */
    print "Description: <br>".$item->description."<br><br>";
    print "Full content: <div>";
    foreach($item->children($content_ns) as $content_node) {
        /* loop over all children in the "content" namespace */
        print $content_node."\n";
    }
    print "</div>";
}
于 2009-01-23T21:36:11.280 回答
0

你有什么现在不工作的?解析 RSS 应该是一个简单的过程。尝试从过多的库中退后一步,只使用一些简单的 XPath 查询或访问 PHP 中的 DOMDocument 对象。

参见:PHP DOMDocument

于 2009-01-23T21:36:56.020 回答