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.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...