Displaying Multiple RSS Feeds with PHP

I would like to make a webpage that displays the titles of the most recent posts from multiple blogs in a single list, ordered by the date from newest to oldest. There exists a nice tutorial about reading the information from an RSS feed and displaying it using PHP.[1]c.bavota: An Easy Way to Display an RSS Feed with PHP. bavotasan.com. 15 Jul 2010. However, that only works with a single RSS feed. It is possible to display multiple feeds by just copying the code from the tutorial multiple times, then combine the content of those lists into a single list and sort the list items by date. Luckily, it’s easy to do this with PHP’s foreach loop.

$rss = new DOMDocument(); 
$feed = array();
$urlArray = array(array('name' => 'WordPress', 'url' => 'http://wordpress.org/news/feed/'),
                  array('name' => 'Duolingo',  'url' => 'http://blog.duolingo.com/rss'),
                  array('name' => 'Twitter',   'url' => 'https://blog.twitter.com/api/blog.rss?name=company'),
                  array('name' => 'Wikimedia', 'url' => 'http://blog.wikimedia.org/c/global/feed/')

foreach ($urlArray as $url) {

    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array ( 
            'site'  => $url['name'],
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc'  => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link'  => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date'  => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        array_push($feed, $item);

Now that we have all the information from the feeds in a single array, we can sort the items by date (newest first) using the usort function.

usort($feed, function($a, $b) {
    return strtotime($b['date']) - strtotime($a['date']);

You can do the printing however you want. For example, the following prints an html list of 30 most recent post titles with their links and date information. It’s also possible to print every item in $feed by using a foreach loop instead of the for loop and $limit.

$limit = 30;
echo '<ul>';
for ($x = 0; $x < $limit; $x++) {
    $site = $feed[$x]['site'];
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
    echo '<li>';
    echo '<strong>'.$site.': <a href="'.$link.'" title="'.$title.'" target="_blank">'.$title.'</a></strong> ('.$date.')';
    echo '</li>';
echo '</ul>';

At the time of writing this post, the above code produces the following output:

References   [ + ]

1. c.bavota: An Easy Way to Display an RSS Feed with PHP. bavotasan.com. 15 Jul 2010.
Tagged with: , ,

6 Comments on “Displaying Multiple RSS Feeds with PHP

  1. Hello.
    Great entry. That is what I was looking for. I am just thinking is there any why to limit showing posts, say to just last seven days?

    • Hello Peter!

      One possibility is to use PHP’s handy strtotime function. In the last for loop, check if the time of the post is greater than seven days before today and if it’s not, break out of the for loop:

      if (strtotime($feed[$x]['date']) > strtotime('-7 days', strtotime('today'))) {
          // do the printing here
      } else {

  2. Hello,

    Thanks for the instruction; this is very useful. I was wondering how to change the “name” to “logo”. In other word, I would like to show the logo of the website (WordPress) nest to the link; similar to http://www.Fark.com


  3. Is it possible to display the RSS feeds in multiple columns (e.g., 2)

    I tried adding the $blocks = array_slice(); and the n the loop:
    foreach ($blocks as $block) {

    But this doesn’t work.
    Can you help?


Leave a Reply

Your email address will not be published. Required fields are marked *