[PHP] How to join / combine / merge different RSS feeds into one using your own server

In this case study we will explain how to merge different RSS feeds into one. We needed to program some automatic newsletters that uses RSS feeds, and as it was a bit complicated to join the pieces, we bring you a step-by-step tutorial on how to do it.

It is true that there are platforms that allow you to do this online, but depending on those platforms implies either having difficulties when adding feeds to the mix, or that the number of feeds to be added is limited. You avoid this by having your own script on your server, with which you can do whatever you want, however you want, whenever you want. Do we start?

Prepare the enviroment

For this project we will install an open source PHP library called “SimplePie”. This is what is going to allow us to handle RSS feeds. Next, we will create a PHP script that, calling this library, will allow us to join the various RSS feeds into one.

Conseguir SimplePie

To obtain SimplePie, so that you can install it on your server, just go to http://simplepie.org/ and click on “download”. This will download a compressed file with some files inside, however you will not need them all, now you will understand why.

Install SimplePie

SimplePie installation is quite simple, and you will only need FTP access. A program like Filezilla can do the job for you if you know how to use it. Assuming you want to call the library from yourdomain/php, let’s do the following:

  1. In the root folder of your domain create two folders, one called “php” and the other called “cache”
  2. Give the “cache” folder permission 777 just in case
  3. Inside the “php” folder, upload the “library” folder and the “autoloader.php” file found in the compressed file you downloaded before. The rest of files can be thrown away

At this point, you already have the SimplePie library functional, and we can work on the script that combines the various RSS feeds.

Create the script

Create a blank file wherever you want with the name you want, and put the PHP extension. For example, you can create a file called “feedcombinado.php” in the root of your domain.

Inside that file we are going to paste a code based on the work of hhje22 over 8 years ago, but we will have to update a bit because it is obsolete. Still it is very good code, and the changes will be minimal. We leave you the code as we have adapted it in the environment mentioned before. You will simply have to change the necessary data to suit your needs. However, with the data it has you can test if it really works.

<?php 
/* Merge multiple RSS feeds with SimplePie
 *
 * Just modify the path to SimplePie and 
 * modify the $feeds array with the feeds you want
 *
 * You should probably also change the channel title, link and description,
 * plus I added a CC license you may not want
 *
 * Help from: http://www.webmaster-source.com/2007/08/06/merging-rss-feeds-with-simplepie/ and https://digitalfreelancing.eu/?p=87313
 * 
 *
*/
header('Content-Type: application/rss+xml; charset=UTF-8');
// Your path to simplepie
include_once('./php/autoloader.php');
//
$feedlink = "https://oliverdelarosa.com/feedcombinado.php"; // URL for this feed, <atom:link>
$feedtitle = "Post de Oliver de la Rosa"; // <title>
$feedhome = "https://OliverdelaRosa.com"; // <link>
$feeddesc = "Feed de todos los post de Oliver de la Rosa en Internet"; // <link>
// Feeds you want to aggregate
$feeds = array(
    'https://oliverdelarosa.com/feed/',
    'https://digitalfreelancing.eu/feed/?lang=es',
	'https://digitalfreelancing.eu/feed',
    'https://derobots.online/author/oliverdelarosa/feed',
	'https://micocina.online/author/oliverdelarosa/feed',
    'https://thefinancialfreedom.online/author/oliverdelarosa/feed',
    'https://lalibertadfinanciera.online/author/oliverdelarosa/feed',
    'https://electrycan.com/author/oliverdelarosa/feed',
    'https://vivirabordo.com/author/OliverdelaRosa/feed',
    'https://demariposas.online/author/oliverdelarosa/feed',
    'https://cursosbaratos.online/author/oliverdelarosa/feed',
    'https://generadores.online/author/OliverdelaRosa/feed/',
    'https://proveedorindustrial.online/author/OliverdelaRosa/feed/',
    'https://fundasbaratas.online/author/OliverdelaRosa/feed',
    'https://playasdecanarias.com/author/OliverdelaRosa/feed/',
    'https://www.youtube.com/feeds/videos.xml?channel_id=UCWFRESFCQjtDv0egKR3j40Q'
    
    
    
    
);
echo '<?xml version="1.0" encoding="UTF-8"?>'; 
?>
<rss version="2.0" 
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:content="http://purl.org/rss/1.0/modules/content/" 
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>
<channel>
<title><?php echo $feedtitle; ?></title>
<atom:link href="<?php echo $feedlink; ?>" rel="self" type="application/rss+xml" />
<link><?php echo $feedhome; ?></link>
<description><?php echo $feeddesc; ?></description>
<language>en-us</language>
<copyright>Copyright <?php echo '2007-'.date("Y");  ?></copyright>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>

<?php
date_default_timezone_set('America/Chicago');
$feed = new SimplePie(); // Create a new instance of SimplePie
// Load the feeds
$feed->set_feed_url($feeds);
$feed->set_cache_duration (600); // Set the cache time
$feed->enable_xml_dump(isset($_GET['xmldump']) ? true : false);
$success = $feed->init(); // Initialize SimplePie
$feed->handle_content_type(); // Take care of the character encoding
?>


<?php if ($success) {
$itemlimit=0;
foreach($feed->get_items() as $item) {
if ($itemlimit==40) { break; }
?>

    <item>
        <title><?php echo $item->get_title(); ?></title>
        <link><?php echo $item->get_permalink(); ?></link>
        <guid><?php echo $item->get_permalink(); ?></guid>
        <pubDate><?php echo $item->get_date('D, d M Y H:i:s T'); ?></pubDate>
        <dc:creator><?php if ($author = $item->get_author()) { echo $author->get_name()." at "; }; ?><?php if ($feed_title = $item->get_feed()->get_title()) {echo $feed_title;}?></dc:creator>
        <description>
        <?php echo htmlspecialchars(strip_tags($item->get_description())); ?>
        </description>
        <content:encoded><![CDATA[<?php echo $item->get_content(); ?>]]></content:encoded>
		<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
    </item>
<?
$itemlimit = $itemlimit + 1;
}
}
?>
</channel>
</rss>

Troubleshooting of possible problems

If the script does not work, verify that the path to the autoloader.php file on line 16 is correct, and that SimplePie is correctly installed in the folders we have mentioned.


From Digital Freelancing OÜ we hope this tutorial has served you, if so, share it on your social networks and leave us a comment with your impressions. You can also write us for any doubt or contribution you have on this matter, we will be happy to help you.