开发者

Use SimpleXML to parse multiple RSS feeds

How can I put multiple RSS feeds from SimpleXML into an array sorted by pubDate?

Example:

feed[0] = 'http://www.example.org/feed1.rss';
feed[1] = 'http://www.thing.org/feed.rss';
...
fe开发者_JS百科ed[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);

Works in PHP 5.3.


You put the item values into a multi-dimensional array, then sort using usort(), then print. Do you have more specific questions?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜