How to Easily Make Your own RSS Feed
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
download in package
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/script-tutorials.com/9/';
$sRssIcon = 'http://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
download in package
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!
|
About: Aramis: Web Developer and Module Developer for online web applications. Have great experience with Dolphin CMS. |
Related posts:
Tags: MySQL, PHP, rss feed tutorial, rss feeds, rss generator, tutorial
Wow! This can be a single of the top blogs We have actually arrive throughout on this subject. Merely Amazing
This is a good,Typical Feeling article.Very Beneficial to one who is just finding the resouces about this Component.It will certainly help
Admirable weblog! I’ll possibly be referencing some of this details in my next speech. I would appreciate it in case you visited my blog at
Thank you for any guide. This method content likes and dislikes others greatly together with due to people, As i acquired brand new issues. It’s very worthwhile. Appreciate it. Htc bravo. Your house is.
Great Blog I love the lay out and the color scheme is it possible to get a copy of your theme?
Remerciment, félicitation et respect.
Cool post thanks, where are your contact details hmm?
Great post. It was well written. Keep up the good work.
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon
Your blog article is very intersting and fanstic,at the same time the blog theme is Specific and Good,great job.To your
Maintain up the excellent work mate. This weblog publish shows how well you comprehend and know this subject.
Thank you for your main piece. This specific content fascinates all of us without doubt and also on account of you will, I come to understand brand-new important things. It had been quite interesting. Thanks a lot. Well done. You live.
I’m grateful for you because of this excellent content material. You actually did make my day :
Thats some interesting information there; never really examined it carefully like that; i think more comments on topics like this make the article more credible.
Merci pour ce courrier, ce contenu m’a réellement beaucoup plu. Grace à ce blog j’ai énormément appris des nouveaux élements que je ne savais pas.
Maintain up the excellent work mate. This weblog publish shows how well you comprehend and know this subject.
This is 4 times now that i’ve happened upon on your website in the last two days while searching google for absolutely unrelated things. Kinda funny. Keep up the good articles!
I am not much of a guy who thinks in so deeply about web design but I think your post had some valid points in it. Like designers are forced to design stuff within the limited code available and not go beyond it, their innovation is somewhat limited but still I think Web Design won’t die! I agree that Amazon and other some big sites won’t have a blog but now a days it’s very important to have some sort of option available so people can quickly communicate their thoughts. I think Amazon if wants to shift it to that, they can get a customized CMS for themselves.
Many thanks for your excellent blog post! I found your post very interesting, you are a great author. I just bookmarked your blog and will come back in the future. I want to encourage you to continue the exciting job, have a great daytime!
Groß danke bezüglich Diese Post,Das Subjekt hat mich wirklich Sehr viel Aufgehangen. Grace in Diese Säule habe ich Äußerst viel gelernt Neue Sachen die ich ignorierte nicht.
Hi there! I really love reading your blog today! Keep making great posts and I will come back every day to keep reading!
Hello there! I really enjoy reading your blog! If you keep making amazing posts like this I will come back every day to keep reading.
I appreciate you for your main piece of writing. It question passions us truly and also caused by an individual, My partner and i figured out unique details. It turned out very worthwhile. Answers. Well done. Yourr home is.
I thought it was going to be some boring old site, but I’m glad I visited. I will post a link to this page on my blog. I believe my visitors will find that very useful.
This is my first time I have visited your site. I found a lot of interesting stuff in your blog. From the tons of comments on your posts, I guess I am not the only one! keep up the impressive work.
I appreciate you for your post. This particular area passions everyone truly and also on account of you will, My partner and i perfected modern points. Previously quite interesting. Appreciate it. Well done. Yourr home is.
Merci à propos de cette colonne, le contenu m’a réellement beaucoup accroché. Grace à ce papier j’ai extrêmement beaucoup appris des nouveaux élements que je ne savais pas.
Hello everyone.. enjoy reading the blog and posting for the first time. Keep up the good work.
Mes remerciements à propos de cette colonne, le contenu m’a réellement immensément accroché. Grace à ce site j’ai bigrement bien appris de nouveaux élements que j’ignorais.
I read a lot of blogs recently and yours is one of the best. I enjoy reading your posts – clear and well written. Your page goes straight to my bookmarks. I got some nice inspirational thoughts after reading it.
Good share, great article, very usefull for us…thanks.
Hello there! I really enjoy reading your blog! If you keep making such great posts I will come back every day to keep reading!
Amazing post, saved the website with interest to see more information!
Amazing story, saved the blog with interest to see more information!
The formal article helped me very much! Saved the site, extremely excellent topics just about everywhere that I read here! I really like the info, thank you.
Good read. I found your site from a google search, and was glad i did. The data has helped me immensely.
Hello there! I really enjoy reading your blog! If you keep making great posts I will come back every day to keep reading!
Hello, I just wanted to take some time today to make a comment and say I have really enjoyed reading your blog. Thanks for all your work!
I really loved this post. You write about this topic very well. I really like your blog and I will definetly bookmark it! Keep up the super posts!
Dear Admin, thanks for your info sharing!
Nice, I am happy to find this great words
Such a well written post.. Thnkx for sharing this post!
Would you mind if I reference a paragraph or two from your post? I’m writing project for highschool. Thanks a lot!