How to Easily Make your own RSS feed using PHP + SQL
RSS Feed is an important part of a website/blog to attract and bring back users and make them permanent readers. Blogs like wordpress have built in rss reader, but if your website doesn’t have a rss system this tutorial is just the thing you need. As we know, RSS feeds usually using to transfer some news to readers. Today I will tell you how to create such feeds and fill it with information from database.
Here is a sample:
Live Demo
[sociallocker]
download in package
[/sociallocker]
Ok, download the example files and lets start coding !
Step 1. PHP
Ok, here are all used PHP files:
index.php
This file will generate RSS feed
<?php require_once('inc/db.inc.php'); require_once('inc/rss_factory.inc.php'); $sSiteUrl = 'http://localhost/dolphin7_rep_svn/demos/www.script-tutorials.com/9/'; $sRssIcon = 'https://www.script-tutorials.com/logo-tuts.png'; $aStoriesRSS = array(); $sSQL = "SELECT * FROM `stories` ORDER BY `id` DESC"; $aStories = $GLOBALS['MySQL']->getAll($sSQL); foreach ($aStories as $iID => $aStoryInfo) { $iStoryID = (int)$aStoryInfo['id']; $aStoriesRSS[$iID]['Guid'] = $iStoryID; $aStoriesRSS[$iID]['Title'] = $aStoryInfo['title']; $aStoriesRSS[$iID]['Link'] = $sSiteUrl . 'view.php?id=' . $iStoryID; $aStoriesRSS[$iID]['Desc'] = $aStoryInfo['description']; $aStoriesRSS[$iID]['DateTime'] = $aStoryInfo['when']; } $oRssFactory = new RssFactory(); header('Content-Type: text/xml; charset=utf-8'); echo $oRssFactory->GenRssByData($aStoriesRSS, 'Our stories', $sSiteUrl . 'index.php', $sRssIcon); ?>
view.php
We will draw post page using this page
<?php require_once('inc/db.inc.php'); $iStoryID = (int)$_GET['id']; if ($iStoryID > 0) { $aStoryInfo = $GLOBALS['MySQL']->getRow("SELECT * FROM `stories` WHERE `id`='{$iStoryID}'"); $sStoryTitle = $aStoryInfo['title']; $sStoryDesc = $aStoryInfo['description']; echo <<<EOF <h1>{$sStoryTitle}</h1> <div>{$sStoryDesc}</div> <hr /> <div><a href="index.php">Back to RSS</a></div> EOF; } ?>
inc/db.inc.php
This is our database class. No need to give full code of that file here (it big enough). It always available as a download package.
inc/rss_factory.inc.php
This is our RSS factory class. Universal class which transforming array with information (in necessary format) into RSS code.
<?php class RssFactory { // constructor function RssFactory() {} // rss generator function GenRssByData($aRssData, $sTitle, $sMainLink, $sImage = '') { $sRSSLast = ''; if (isset($aRssData[0])) $sRSSLast = $aRssData[0]['DateTime']; $sUnitRSSFeed = ''; foreach ($aRssData as $iUnitID => $aUnitInfo) { $sUnitUrl = $aUnitInfo['Link']; $sUnitGuid = $aUnitInfo['Guid']; $sUnitTitle = $aUnitInfo['Title']; $sUnitDate = $aUnitInfo['DateTime']; $sUnitDesc = $aUnitInfo['Desc']; $sUnitRSSFeed .= "<item><title><![CDATA[{$sUnitTitle}]]></title><link><![CDATA[{$sUnitUrl}]]></link><guid><![CDATA[{$sUnitGuid}]]></guid><description><![CDATA[{$sUnitDesc}]]></description><pubDate>{$sUnitDate}</pubDate></item>"; } $sRSSTitle = "{$sTitle} RSS"; $sRSSImage = ($sImage != '') ? "<image><url>{$sImage}</url><title>{$sRSSTitle}</title><link>{$sMainLink}</link></image>" : ''; return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rss version=\"2.0\"><channel><title>{$sRSSTitle}</title><link>{$sMainLink}</link><description>{$sRSSTitle}</description><lastBuildDate>{$sRSSLast}</lastBuildDate>{$sRSSImage}{$sUnitRSSFeed}</channel></rss>"; } } ?>
Step 3. SQL
We will need to execute next SQL in our database. We will create table with demo stories which going to show in RSS
CREATE TABLE `stories` ( `id` int(11) NOT NULL auto_increment, `title` varchar(255) NOT NULL default '', `description` text NOT NULL, `when` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; INSERT INTO `stories` (`id`, `title`, `description`, `when`) VALUES (1, 'First story', 'First story description here', '2010-06-01 00:00:00'), (2, 'Second story', 'Second story description here', '2010-06-02 00:00:00'), (3, 'Third story', 'Third story description here', '2010-06-03 00:00:00'), (4, 'Fourth story', 'Fourth story description here', '2010-06-04 00:00:00'), (5, 'Fifth story', 'Fifth story description here', '2010-06-05 00:00:00'), (6, 'Sixth story', 'Sixth story description here', '2010-06-06 00:00:00'), (7, 'Seventh story', 'Seventh story description here', '2010-06-07 00:00:00'), (8, 'Eighth story', 'Eighth story description here', '2010-06-08 00:00:00'), (9, 'Ninth story', 'Ninth story description here', '2010-06-09 00:00:00'), (10, 'Tenth story', 'Tenth story description here', '2010-06-10 00:00:00');
Live Demo
Conclusion
Today I told you how to create own RSS feed. Hope all my code easy to understand and comfortable to use. You can use this material to create own scripts into your startups. Good luck!
a good working tutorial
Hi! Thanks for mentioning the best RSS feed creation method, Its really helpful for us and thank you for posting useful content!
It does not work :(
What exactly now work? Our demo? I just fixed it (due transferring to another hosting – DB details was changed)
It does not work. It is looking for a password …
Warning: mysql_connect() [function.mysql-connect]: Access denied for user ‘istiatv’@’localhost’ (using password: YES) in /home/istiatv/public_html/superbasura/MakeRssFeed/inc/db.inc.php on line 35
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/istiatv/public_html/superbasura/MakeRssFeed/inc/db.inc.php on line 38
Warning: mysql_query() [function.mysql-query]: Access denied for user ‘istiatv’@’localhost’ (using password: NO) in /home/istiatv/public_html/superbasura/MakeRssFeed/inc/db.inc.php on line 40
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/istiatv/public_html/superbasura/MakeRssFeed/inc/db.inc.php on line 40
What am I missing?
It works well, but it seems that you have problem with database, – why it is access denied? It seems that you haven’t assigned proper db user to your database.
thanks a lot for this tutor
can u suggest me? where i setup this script? my web root directory or sub-directory? if setup sub-directory, so what i instead { $sSiteUrl = ‘http://localhost/dolphin7_rep_svn/demos/www.script-tutorials.com/9/} for this link , my root link or sub-directory link???
Hello Ibp,
You need to use URL of your own website. Pay attention, that this variable ($sSiteUrl) is used only to generate links to view RSS elements, so you need to use the proper link here (root link in case if your view.php is in the root folder, and sub-directory url in case if everything is in subfolder).