加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用SimpleXML解析多个RSS源

发布时间:2020-12-13 21:39:54 所属栏目:PHP教程 来源:网络整理
导读:如何将Simple XML中的多个RSS源放入按pubDate排序的数组中? 例: feed[0] = 'http://www.example.org/feed1.rss';feed[1] = 'http://www.thing.org/feed.rss';...feed[n] = '..';#Fetch feeds#Sort by pubDateforeach ($feeds as $row) { //Do something pr
如何将Simple XML中的多个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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读