Best way to merge multiple RSS feeds with PHP

There are many ways to create a combined RSS feed. After going through a couple of different options, I figured this one is quite frustrating to google. Most of the ways are outdated or too complicated so decided to share the solution I found.

I needed a quick way to show other blogs I’m writing on, all the latest articles combined. You could use this snippet on your WordPress theme or other website powered by PHP.

<?php
/**
 * Merge multiple RSS feeds to one and display them on a web page
 *
 * @package geekylifestyle
 */

$rss = new DOMDocument();
$feed = array();
$urlarray = array(
  array( 'name' => 'Geeky Lifestyle', 'url' => 'https://geekylifestyle.com/feed' ),
  array( 'name' => 'Dude',            'url' => 'https://www.dude.fi/feed' ),
  array( 'name' => 'Huurteinen',      'url' => 'https://www.huurteinen.fi/feed/' ),
  array( 'name' => 'Leffat',          'url' => 'https://rollemaa.org/leffat-rss.php' ),
);

foreach ( $urlarray as $url ) {
  $rss->load( $url['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 );
  }
}

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

$limit = 30;
echo '<ul>';
for ( $x = 0; $x < $limit; $x++ ) {
    $site = $feed[ $x ]['site'];
    $title = str_replace( ' & ', ' & ', $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>';

Originally posted on Mikko’s Project Corner.

Feel free to suggest other options in the comments!

Want to write with me?

Do you know something about tech, nerdy stuff, movies, code, AI, Internet phenomena, social media or anything related to geeky subjects? Join and write with me! I'm currently doing this alone and my time is not sufficient for upkeeping the blog by myself, so all pieces of articles are welcome. Contact me and get your user account.

Roni Laukkarinen

Roni Laukkarinen

Editor-in-chief and owner of Geeky Lifestyle blog. Truly a jack of all trades, a Swiss knife; an avid tech and multimedia geek, coder, owner of a digital agency company, sysadmin, music enthusiast, artist and film critic.

2 comments

  1. Pete

    Oh it seems comma is allowed at the last item of multidimensional array? That’s a new thing. 🙂 Anyway, I’d do some micro optimization to that code by combining as many *echo* calls as possible and would just remove that *$limit* variable and write it directly to the loop. There are almost as many styles of coding as there are those who code. 🙂

    Reply to this comment
  2. Isaac

    Thanks for your wonderful piece of code.. I’ve been trying to aggregate multiple sites rss in one place as headlines.. Hope this works for me.

    But plz I am not good with codes. Can u plz write a code for me to aggregate multiple news feeds and display them in one place with only titles, dates, and source with the source favicon.
    Thank you in anticipation.

    Reply to this comment

Leave a Reply

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

Follow The Geek

Geeky Lifestyle is a huge tweeter! Follow @thatgeekyblog to get real-time updates about what's happening in the scene.