How to create your own rss feed

RSS is a way of easily keep your site's readers up to date with your latest content. That's a reason why it is widely used by the famous blogs and big sites. Once someone subscribes to an RSS feed, he'll get a notification every time you post some new content to your site. It helps fidelizing your users, therefore improving your traffic. It is definitely worth implementing. This tutorial will guide throught the steps involved in the creation of your own RSS feed from scratch, and based on your site's existing platform.

How to setup

Step 1: Create the Channel

The feeds are not going to deliver themselves to the user, they are wrapped into what is called a channel. So before you provide the user with any actual feed content, you'll need to give him a few standard extra information. This is how you can create your own channel, it's actually just an xml element which you'll give a nice title, a link, and a handy description (you can also specify another language than English inside the language tag):
  1. <?php
  2. header("content-type: text/xml"); //Tells the browser that we are going to write some xml with php
  3. ?>
  5. <rss version="0.91">
  7. <!--Start the channel definition-->
  8. <channel>
  10. <title >jdmweb</title ><!--Channel Title-->
  11. <link></link><!--Channel Link-->
  12. <description><!--Channel Description-->
  13. is a website about web development,
  14. website creations and handy web tools for developpers
  15. by Jeremy Desvaux de Marigny.
  16. </description>
  17. <language>en-us</language><!--And finally the language-->
  19. </channel>
  20. </rss>
That's it for your channel definition! It's pretty straight forward. If you'd like, you can add an illustrative picture alongside your channel definition to make it look better, let's see how you can do that.

Step 2: Add an illustrative picture

There's nothing difficult here either really. Right after the channel description and language, we add an image xml node. This node contains a title node (the title of your image), a url node (which contains the location of your image), a link node (where to go when you click the picture), and finally the height and width nodes to specify the dimensions of your image. The code therefore becomes:
  1. <?php
  2. header("content-type: text/xml"); //Tells the browser that we are going to write some xml with php
  3. ?>
  5. <rss version="0.91">
  7. <!--Start the channel definition-->
  8. <channel>
  10. <title >jdmweb</title ><!--Channel Title-->
  11. <link></link><!--Channel Link-->
  12. <description><!--Channel Description-->
  13. is a website about web development,
  14. website creations and handy web tools for developpers
  15. by Jeremy Desvaux de Marigny.
  16. </description>
  17. <language>en-us</language><!--And finally the language-->
  19. <image> //The code to add your image goes here
  20. <title >jdmweb</title>
  21. <url></url>
  22. <link></link>
  23. <width>57</width>
  24. <height>57</height>
  25. </image>
  27. </channel>
  28. </rss>

Step 3: Add your items

Now that your RSS channel is ready, it's time to feed in your elements. Those elements have to follow a very particular structure, defined by standards. You can find more details about the full RSS 2.0 specification on the RSS Advisory Board's website In a nutshell, every RSS element will be defined as an xml item node, which can have the following child nodes:
RSS Item Required Elements:
  • title - The title of the item, for ex: Venice Film Festival Tries to Quit Sinking
  • link - The url of the item, for ex:
  • description - The item synopsis, for ex: Some of the most heated chatter at the Venice Film Festival this week was about the way that the arrival of the stars at the Palazzo del Cinema was being staged.
RSS Item Additional Optional Elements:
  • 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
  • enclosure - Describes a media object that is attached to the item
  • guid - A string that uniquely identifies the item
  • pubDate - Indicates when the item was published
  • source - The RSS channel that the item came from
Knowing these constraints, we'll be able to dynamically select the latest posts from a database, and convert them in RSS items. For the purpose of this example, we'll assume that we are working with a standard 'post' table that contains the following fields:
The 'post' table structure:
  • post_id - Unique id of the post
  • post_title - The post Title
  • post_text - The post content
  • post_thumb - Url of the post illustrating picture
  • post_category - The post Category
  • post_date - The post Date
We can now query the database to select the latest posts from this table. We'll then loop through the returned rows, and build a corresponding RSS item for each:
  1. <?php
  2. //Items dynamic building
  3. $sql="SELECT * FROM post ORDER BY post_date DESC LIMIT 10"; //SQL Query
  4. $req = mysql_query($sql) or die("Erreur SQL !: ".$sql." - ".mysql_error());
  5. while($post= mysql_fetch_assoc($req)) { //Loop throught the results
  6. echo' //Build the item
  7. <item>
  8. <title >'.htmlentities($post[post_title]).'</title> //With the title
  9. <link>http://www.yourwebsite/post_'.$post[post_id].'</link> //Link
  10. <description> //And the description
  11. '.htmlentities($post[post_text],ENT_QUOTES).'
  12. </description>
  13. </item>';
  14. }
  15. ?>
As you can see on the previous php code, on line 1, we create the SQL query that will select the 10 most recent posts based on their date. The query is sent to the database on line 2. On line 3 we start the loop through the results and from line 5 to 11 the item xml element is built from each post record. I'd just add a little note on the fact that the title and the description go through the htmlentities() function in order to strip any unwanted character prohibited in xml, such as &, ", ', > or < for example. When adding this loop into our existing code, our RSS generator now looks like this: [php] //Channel Definition jdmweb is a website about web development, website creations and handy web tools for developpers by Jeremy Desvaux de Marigny. en-us // Illustrating Image jdmweb 57 57 '; '.htmlentities($post[post_title]).' //With the title http://www.yourwebsite/post_'.$post[post_id].' //Link //And the description '.htmlentities($post[post_text],ENT_QUOTES).' '; } ?> [/php] With this code, you now have a feed generator working in relationship with the posts you save in the database. As a result, as soon as you post a new article on your site or blog, your RSS subscribers will be notified. This is the minimum required for the feed to work. Let's now have a look at a few things you can do to improve it.

How can I be sure that my feed is valid and working?

Now that the code has been written, we need to test it. To do this, save this code as a file, for example, myrss.php, and upload it to your website in a location you can access, for example,, you can find mine here: RSS If you can view it without error, then it's a good start. We'll now have your feed tested by an impartial and intransigent judge: The Feed Validator, which you can find at the following url: You just have to submit your feed url and if you see the message: "Congratulations! This is a valid RSS feed.", then you guessed it, your feed is valid. If you are still not sure, you can finally subscribe to your own feed and monitor its behaviour as time goes by.

How can I tell the browser to locate and recognize my RSS feed?

You've probably noticed that when you browse a site that has its own RSS feed, the browser detects it to allow you to either view it or subscribe to it easily. But it doesn't do it by default though, you'll have to add this simple line of code in your page header to make it work:
  1. <link rel="alternate" type="application/rss+xml" title="My Feed" href="/myrss.php"/>

How Can I add a picture to each of the items?

There is a way to illustrate each item with a thumbnail. Let's go back to our item description to modify it:
  1. <?php
  3. //Items dynamic building
  4. $sql="SELECT * FROM post ORDER BY post_date DESC LIMIT 10"; //SQL Query
  5. $req = mysql_query($sql) or die("Erreur SQL !: ".$sql." - ".mysql_error());
  6. while($post= mysql_fetch_assoc($req)) { //Loop throught the results
  7. echo' //Build the item
  8. <item>
  9. <title >'.htmlentities($post[post_title]).'</title > //With the title
  10. <link>http://www.yourwebsite/post_'.$post[post_id].'</link> //Link
  11. <description> //And the description
  12. <!&#91;CDATA&#91;
  13. <img align="left" hspace="10" src="'.$post&#91;post_thumb&#93;.'" />
  14. '.$post&#91;post_text&#93;.'
  15. &#93;&#93;>
  16. </description>
  17. </item>';
  18. }
  20. ?>
The lines 12 to 15 have been changed here compared to the previous version. We are using a special tag called CDATA, in which we place our image and the description. This will place the image specified in the post_thumb attribute in front of the post description. You can use the align and hspace properties to control the elements alignment, and change the layout.


Once you know the RSS markup specifications, you are one query away from your own custom RSS feed. By following those simple steps, you can use your blog or site existing setup to generate a feed that visitors will be able to use to stay tuned with your work. So there you go, create your channel, illustrate it with a picture, add your items, and spread the word about your site's latest addition.

Leave a Reply

Your email address will not be published. Required fields are marked *

Other Useful Resources

Getting started with Jenkins Blue Ocean Pipelines illustration

Always being on the lookout for tools and methods that would help a development team industrialise its processes, I got interested into Continuous Delivery and Continuous Integration.
On of the leading tools of this field being Jenkins, I setup an instance to get started.
Here's some feedback on my experience.
Disclaimer : I'm not a Jenkins nor CI expert, this is just a retranscription of my wanderings, hoping it would serve a fellow developer out there.

Read more
How to easily integrate a PayPal Checkout with PHP illustration

PayPal is a renowned payment platform that allows you to accept online payments on your site, by taking care of all the money transactions for you. This transparency really is an appreciated quality that allows developers to integrate checkout solutions on merchant sites, by completely outsourcing the banking operations to PayPal.

Another good thing for developers is that the PayPal API is very versatile. It can be very simple if your needs are simple, or it can be very well customized to meet some more advanced needs such as complete shopping carts handling. On the other hand, I sometimes find this API not really user friendly as it works with forms, which fields are not always very intuitive. In other words, depending on the form you are building, you get a different service from PayPal.

In order to get a friendlier and also more generic solution, I wrote a PayPal manager in PHP. This tutorial will show you how you can benefit from this PHP class to integrate PayPal checkouts faster and in a much simpler way.

Read more
A guide to Ajax Interactions with jQuery illustration

The fantastic jQuery library revolutionized the way javascript is being written around the globe. It's been globally acclaimed and adopted, resulting in an award for best open source project in the end of 2009.

In this article, we are going to see how we can use the power of this library to create your own Ajax request effortlessly, to make pages communicate in a user friendly way. Thank you jQuery team, the days of javascript darkness are over!

Read more
RT @lemouvementinfo: #Montpellier « Le Vélo au cœur des élections municipales à Montpellier » Nouvelle « manifestive » ce dimanche 10 novem…08 Nov / 09:28