7Oct/113

How to merge/mix/combine RSS feeds

One problem I faced on my multi-language blog setup - it has different feeds for each language. Multi-language plugin WPML which I use does not have feature to mix feeds into one. I like my subscribers to see one common feed which is merged for all languages and leave possibility to subscribe to one language also. I setup simple solution using pure PHP.

I did research several times about it and found very few information.

RSS merge for WordPress

Very few plugins exists for WordPress and they do not work with recent 3.X versions. Unfortunately.

Yahoo pipes

Yahoo pipes is a very flexible and feature rich tool for managing RSS feeds. It is possible fetch, merge, split, filter for any RSS source. It has nice graphical interface for all this.

But I think it is a little but overkill for my two feeds.

PHP Solution for mixing RSS feeds

I did not want to bother with PHP coding for such a simple task. But if to compare actual time for coding it is far less then trying different plugins and research.

So I found somewhere on the Internet these two nice libraries:

  • magpierss - for fetching feeds
  • feedcreator.class.php - for publishing feeds

With a small PHP glue between them I created simple script for merging several RSS feeds into one. Below is simple script that does the job.

You can download full package with libraries included below in the end of post.

<?php
/**
 * Setup part
 * 
 */
$DOMAIN_NAME = 'http://a32.me/';
$FEED_URL = $DOMAIN_NAME . 'rss/full.php';
$SITE_TITLE = 'a32.blog by Constantin Bosneaga';
$SITE_DESRIPTION = 'Its all about. IT and "stuff" :)';
$SITE_AUTHOR = 'Constantin Bosneaga';
 
$RSS_CACHE = "/tmp/rsscache";
$RSS_CACHE_EXP = 3600;
 
$FEED_LIST = array(
	'http://a32.me/feed/',
	'http://a32.me/feed/?lang=ru'
);
 
 
/**
 * Do not modify below this point
 * 
 */
define('MAGPIE_CACHE_DIR', $RSS_CACHE);
define('MAGPIE_CACHE_AGE', $RSS_CACHE_EXP);
define('MAGPIE_OUTPUT_ENCODING', 'utf-8');
 
 
// include required files 
require_once ('magpierss-0.72/rss_fetch.inc');
include ('feedcreator.class.php');
 
/* Set RSS properties */
$rss = new UniversalFeedCreator();
$rss->useCached();
$rss->title = $SITE_TITLE;
$rss->description = $SITE_DESRIPTION;
$rss->link = $DOMAIN_NAME;
$rss->syndicationURL = $FEED_URL;
$rss->encoding = 'utf8';
 
/* Set Image properties 
$image = new FeedImage();
$image->title = $SITE_TITLE . " Logo";
$image->url = $SITE_LOG_URL;
$image->link = $DOMAIN_NAME;
$image->description = "Feed provided by " . $SITE_TITLE . ". Click to visit.";
$rss->image = $image;
*/
 
function showSummary($url, $num = 10, $showfullfeed = false) {
	global $rss, $DOMAIN_NAME, $SITE_AUTHOR, $SITE_TITLE;
	$num_items = $num;
	@ $rss1 = fetch_rss($url);
	if ($rss1) {
		$items = array_slice($rss1->items, 0, $num_items);
		foreach ($items as $item) {
			$href = $item['link'];
			$title = $item['title'];
			if (!$showfullfeed) {
				$desc = $item['description'];
			} else {
				$desc = $item['content']['encoded'];
			}
			//                $desc .=  '
			//Copyright &copy; <a href="'.$DOMAIN_NAME.'">'.$SITE_TITLE.'</a>.  All Rights Reserved.
			//';
			$pdate = $item['pubdate'];
			$rss_item = new FeedItem();
			$rss_item->title = $item['title'];
			$rss_item->link = $item['link'];
			$rss_item->description = $item['content']['encoded'];
			$rss_item->date = $item['pubdate'];
			$rss_item->source = $DOMAIN_NAME;
			$rss_item->author = $SITE_AUTHOR;
			$rss->addItem($rss_item);
		}
 
	} else {
		echo "Error: Cannot fetch feed url - " . $url;
	}
}
 
// Fetch all feeds
foreach($FEED_LIST as $v) showSummary($v);
 
// Sort items by date
function __usort($ad, $bd) {return strtotime($bd->date) - strtotime($ad->date);}
usort($rss->items, '__usort');
 
// Display items
$rss->saveFeed("RSS1.0", $RSS_CACHE . "/feed.xml");
 

Download RSS feed mixer/combiner

Download RSS feed mixer/combiner. Unpack and change the Setup part of the script.

Vote for wordpress plugin

If you like to see these functionality done as WordPress plugiun, please leave your vote in comments!

Filed under: dev, php Leave a comment
Comments (3) Trackbacks (0)
  1. Thanks for posting this. I got this to work, however for each entry in the new feed only displays the post Title and date. The content/excerpt is missing.

    here is the feed url:
    michaeltchao.com/rss/full.php

    and they are fetching the feeds from the following:

    michaeltchao.tumblr.com/rss/
    blog.michaeltchao.com/feed/

    any suggestions?

  2. Yeah, I just did not bother with other fields, because title and content was quite enough for me.

    You may check and fix the code include other fields as well, I would update this post with your modifications.

    Do you use with WordPress or just standalone ?

  3. I am looking for the same- title and content.

    for the site (michaeltchao.com) with the combined feed, i am using wordpress.

    and blog.michaeltchao.com is with wordpress as well.


Leave a comment

Spam protection by WP Captcha-Free

No trackbacks yet.