使用PHP从rss feed获取最后的URL

时间:2014-07-24 11:27:31

标签: php rss feed news-feed

成像我有来自网站的RSS源,其中包含该网站的最新消息,

像这样:

http://example.com/feed

现在,我想从此Feed地址获取最后一条新闻网址。

像这样:

http://example.com/post/555.php
http://example.com/post/554.php
http://example.com/post/553.php
http://example.com/post/552.php
http://example.com/post/551.php
http://example.com/post/550.php

如何获取限制为(例如)25个网址的网址列表在PHP中?

1 个答案:

答案 0 :(得分:0)

可以使用以下代码完成:

$rss = new DOMDocument();
$rss->load('http://wordpress.org/news/feed/');
$feed = array();

foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array (
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
    );
    array_push($feed, $item);
}

$limit = 5;

for($x=0;$x<$limit;$x++) {
    echo $link = $feed[$x]['link'];
}