Recently i generated a rss feed from business listings for one of my client’s website. Also, i merged this listings feed into client’s existing blog rss feed. Here’s how i did it.
First of all i initiated a $rss string variable with necessary rss header tags in it. (code is written in php)
<?php $rss = '<?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> <channel> <title><![CDATA[ClientsWebsite.com - Somethings Directory]]></title> <link>http://www.ClientsWebsite.com</link> <description><![CDATA[ClientsWebsite is the online source for doing something!]]></description> <lastBuildDate>'.date('D, d M Y h:i:s O').'</lastBuildDate> <docs>http://www.devarticles.in</docs> <language>en</language> '; //Then i wrote a (sql) query to fetch latest 20 entries $query = "select `id`, `title`, `description` from `listings` where `status`='Active' order by `id` desc limit 20"; $reso = mysql_query($query); //then i iterated through each records to create individual rss feed items while($row=mysql_fetch_assoc($reso)) { $rss .= '<item><title><![CDATA['.$row['title'].']]></title><link>http:www.ClientsWebsite.com/listing.php?id="'.($row['id']).'</link><description><![CDATA['.$row['description'].']]></description></item>'; } // i put <![CDATA[]]> there so that any special characters or html tags do not break the XML structure of my feed $rss .= '</channel></rss>'; header ("content-type: text/xml"); echo $rss; //finally outputting the feed to web browser ?>
It was a quite simple process to create rss feed dynamically on fetching records from database table.
Following the additional bit of code i put there in order to merge the above generated rss feed into existing blog rss feed. This is how it came. When i had finished iterating through the listings from database table i read my blog feed and extracted feed items and merged them into the listings feed items. Here’s the complete code once again.
<?php $rss = '<?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> <channel> <title><![CDATA[ClientsWebsite.com - Somethings Directory]]></title> <link>http://www.ClientsWebsite.com</link> <description><![CDATA[ClientsWebsite is the online source for doing something!]]></description> <lastBuildDate>'.date('D, d M Y h:i:s O').'</lastBuildDate> <docs>http://www.devarticles.in</docs> <language>en</language> '; $query = "select `id`, `title`, `description` from `listings` where `status`='Active' order by `id` desc limit 20"; $reso = mysql_query($query); while($row=mysql_fetch_assoc($reso)) { $rss .= '<item><title><![CDATA['.$row['title'].']]></title><link>http:www.ClientsWebsite.com/listing.php?id="'.($row['id']).'</link><description><![CDATA['.$row['description'].']]></description></item>'; } $source = file_get_contents('http://www.mysiteaddress.com/blog/feed/rss/'); preg_match_all("'<item>(.*?)</item>'si", $source, $match, PREG_SET_ORDER); if(sizeof($match[0])>0) { foreach($match[0] as $item) { $rss .= $item; } } $rss .= '</channel></rss>'; header ("content-type: text/xml"); echo $rss; ?>
I hope it helps someone.