Welcome to TheCredence.com - You may like to subscribe to our RSS feed to stay updated.
RSS feed is one of the essential component of web 2.0 and It has now become a must to have RSS feeds on a site. RSS provides the user a mode of looking at the content whenever it get published in there readers, saving there time. By default, when you use blogger, wordpress or any blogging software, it automatically creates an rss feed and shows an image on the address bar. A person just to click on it and he can subscribe it to. You should be able to see the same in my address bar
If you are finding this blog interesting than I will advice you to subscribe to it
as I am trying to put atleast 2-3 informative post daily in it.
Anyway Today we will learn how to create RSS feeds using PHP but let me explain
its structure a little for those who are very new to this.
What is an RSS feed
RSS stands for Real simple syndication. It syndicates contents such as news, blogs, podcasts or videos. Almost anything can be syndicated with RSS. The possibility is immense, so does the outcome. Most search engines use RSS feeds to aggregate news and blogs. It is also a sign of professionalism now days.
Technically RSS feed is nothing but xml documents with some common tags. A typical RSS feed can look like this,
<rss version="2.0"> <channel> <description>If it's a gadget, we review it. Learn what gadgets are hot and what's not!</description> <link />http://allgadgetsreviewed.com <item> <description>I've been playing with the new Nokia 3650. Finally, someone has got the combination of a cell phone with digital camera capabilities right!</description> <link />http://allgadgetsreviewed.com/nokia3650.html </item> </channel> </rss>
As you can see, It's just a simple xml document. It starts with
<rss version="2.0"></rss>
After that is the <channel> tag. Channel tag holds information about the feed. It has three tags <title>, <description> and <link>. There are other optional tags like an image or a category. For full reference visit RSS specification.
The real content is in the <item> tag. An RSS feed can have many item tag. It is through this item tags that RSS aggregators update content. An item tag has following elements,
- Title: The title of the item.
- Link: The URL of the item.
- Description: The item synopsis.
- Author: Email address of the author of the item.
- Category: Includes the item in one or more categories.
- Comments: URL of a page for comments relating to the item
- pubDate: Indicates when the item was published.
Create an RSS Feed
It may seem a bit hassle to create RSS feeds. So, We will use an RSS creator class. The class can be found here . Here is an example of using the class,
include("feedcreator.class.php"); $rss = new UniversalFeedCreator(); $rss->useCached(); $rss->title = "Test news"; $rss->description = "daily news from the PHP scripting world"; $rss->link = "http://test.com/news"; $rss->syndicationURL = "http://www.dailyphp.net/".$PHP_SELF;
This initializes the class and also sets the channel info. Now it is time for items, which can be done easily. We pull info from DB and set each item attributes like this,
mysql_select_db($dbHost, $dbUser, $dbPass); $res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC"); while ($data = mysql_fetch_object($res)) { $item = new FeedItem(); $item->title = $data->title; $item->link = $data->url; $item->description = $data->short; $item->date = $data->newsdate; $rss->addItem($item); } $xml=$rss->createFeed(“2.0”)
Here we get some news from an imaginary database and put individual item attributes. Finally we add the Item using the addItem method. To get the xml doc we use the createFeed method with the RSS version as the parameter. Now we can either save this into a file or show it to the browser.
A site without RSS feed is considered out of class now days. Its usefulness is also undeniable. So, lets create these feeds and open ourselves to the big worlds.
Related Articles -
Creating a Pay to Read RSS Feed | Using Yahoo’s User Interface Library (YUI) with PHP | Creating A Simple Install Script for PHP/MySQL Applications
Links you may find interesting -


(3 votes, average: 3.67 out of 5)