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!

Thanks for reading! I need your attention for a moment.

Did your problem got solved? Did you enjoy this post? If so, consider thanking me on Patreon. Doing this is not free and I'd love you buy me a beer or coffee. If you do that, I might be able to help you if you didn't get your problem solved with this blog post. I know my shit around areas like website design, coding, blogging, digital marketing and SEO so if you want to do business with me in other ways let me know.

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.

6 comments

  1. Avatar

    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. Avatar

    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
  3. Avatar

    Deepanshu Sonkhla

    Thanx for the feed i was literally looking for these feed.
    But i just want to know one more thing how to make it fast as the script is loading very low causing delay in server response.
    basically i just want to make it fast
    Thanks and reply ASAP if possible

    Reply to this comment
    1. Roni Laukkarinen

      Roni Laukkarinen

      Hello. You need to cache the output in some way (not included in this tutorial) and get a better server. Preferably both. Also, if the site containing RSS is slow, you have even more reasons to cache and fetch the feed once per hour for example.

      Reply to this comment
  4. Avatar

    Deepanshu Sonkhla

    Thanks for the reply!
    Can you tell me how can i do it?
    I mean any reference or anything it will be very helpful for me.
    Its a request
    Reply ASAP
    Thanks

    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.

How to get on the first page of Google? Search Engine Optimization – the easy five tips https://t.co/TBIaFe1esIhttps://t.co/IL1HWeMZ9S View tweet on Twitter →