使用FETCH_FEED组合RSS提要和排序

时间:2012-11-21 作者:Tim Bowen

我正在尝试创建一个来自5个不同feed的最近10篇文章的feed,所有这些文章都合并到一个列表中。我找到了一种合并提要的方法,但无法轻松地将其限制为10个,并按日期进行排序。

有人能给我指出正确的方向吗?谢谢

<ul>
<?php $rsslist = array(
\'http://www.nrel.gov/news/press/rss/rss.xml\',
\'http://www.pewenvironment.org/rss/campaigns-8589935316\',
\'http://www.ashrae.org/RssFeeds/ashrae.xml\',
\'http://www.districtenergy.org/blog/category/idea-activity/feed/\',
\'http://www.districtenergy.org/blog/category/industry-news/feed/\'
 );

$feedlist = array();
foreach ( $rsslist as $rssurl ) $feedlist []= fetch_feed( $rssurl ); /* store in feed array for later access */

$current_item_cycle = 0;
while ( sizeof($feedlist) ) { /* while feedlist is not empty */ 

    foreach ( $feedlist as $index => $feed ) { /* cycle through each feed */

        if (!$item = $feed->get_item($current_item_cycle++)) unset($feedlist[$index]); /* if feed has not more items unset */

        else /* echo it out */ { ?>
            <li>
            <?php echo $item->get_date(\'j F Y | g:i a\') . $item->get_permalink(); ?>
            </li>
        <?php }
    }
} ?>
</ul>

1 个回复
最合适的回答,由SO网友:Jake 整理而成

此代码应该有效。我注释掉了你的一个提要。当我取消注释它时,它会停止正确排序。似乎有什么问题。它在浏览器中的格式也不像其他文件那样正确。

<ul>
<?php 
$rsslist = array(
    #\'http://www.pewenvironment.org/rss/campaigns-8589935316\',
    \'http://www.ashrae.org/RssFeeds/ashrae.xml\',
    \'http://www.districtenergy.org/blog/category/idea-activity/feed/\',
    \'http://www.districtenergy.org/blog/category/industry-news/feed/\',
    \'http://www.nrel.gov/news/press/rss/rss.xml\'
);

// Fetch all of the feeds into a simplePie mashup
$rss = fetch_feed($rsslist);
// Set the max items to either 10 or all items if there are less than 10
$maxitems = $rss->get_item_quantity(10);

// Get the items (0-10)
$rss_items = $rss->get_items(0, $maxitems); 

// If there are no items, output that
if ($maxitems == 0) {
    echo \'<li>No items.</li>\';
// Otherwise loop over the items
} else {
    foreach ( $rss_items as $item ) { ?>
        <li>
        <?php echo $item->get_date() . $item->get_permalink(); ?>
        </li>
    <?php 
    }
}
?>
</ul>

结束