微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 使用SimpleXML解析多个RSS源

如何将SimpleXML中的多个RSS源放入按pubDate排序的数组中?

例:

Feed[0] = 'http://www.example.org/Feed1.RSS';
Feed[1] = 'http://www.thing.org/Feed.RSS';
...
Feed[n] = '..';

#Fetch Feeds
#Sort by pubDate

foreach ($Feeds as $row) {
   //Do something
   print '<item>
          <title>...</title>
          </item>';
}

解决方法:

// Set the Feed URLs here
$Feeds = array(
    'http://www.example.org/Feed1.RSS',
    'http://www.example.org/Feed2.RSS',
    // etc.
);

// Get all Feed entries
$entries = array();
foreach ($Feeds as $Feed) {
    $xml = simplexml_load_file($Feed);
    $entries = array_merge($entries, $xml->xpath('/RSS//item'));
}

// Sort Feed entries by pubDate (ascending)
usort($entries, function ($x, $y) {
    return strtotime($x->pubDate) - strtotime($y->pubDate);
});

print_r($entries);

适用于PHP 5.3.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐