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. ?>
  4.  
  5. <rss version="0.91">
  6.  
  7. <!--Start the channel definition-->
  8. <channel>
  9.  
  10. <title >jdmweb</title ><!--Channel Title-->
  11. <link>http://www.jdmweb.com</link><!--Channel Link-->
  12. <description><!--Channel Description-->
  13. jdmweb.com 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-->
  18.  
  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. ?>
  4.  
  5. <rss version="0.91">
  6.  
  7. <!--Start the channel definition-->
  8. <channel>
  9.  
  10. <title >jdmweb</title ><!--Channel Title-->
  11. <link>http://www.jdmweb.com</link><!--Channel Link-->
  12. <description><!--Channel Description-->
  13. jdmweb.com 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-->
  18.  
  19. <image> //The code to add your image goes here
  20. <title >jdmweb</title>
  21. <url>http://www.jdmweb.com/Images/style/apple-touch-icon.png</url>
  22. <link>http://www.jdmweb.com</link>
  23. <width>57</width>
  24. <height>57</height>
  25. </image>
  26.  
  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: http://nytimes.com/2004/12/07FEST.html
  • 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 http://www.jdmweb.com jdmweb.com is a website about web development, website creations and handy web tools for developpers by Jeremy Desvaux de Marigny. en-us // Illustrating Image jdmweb http://www.jdmweb.com/Images/style/apple-touch-icon.png http://www.jdmweb.com 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, http://mysite.com/myrss.php, 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: http://www.feedvalidator.org/. 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
  2.  
  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. }
  19.  
  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.

Conclusion

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.

2 Responses to How to create your own rss feed

  1. Can I just say what a relief to discover someone who really understands what they are discussing over the internet.
    You certainly realize how to bring an issue to light and make it important.
    More people must check this out and understand this side of the story.
    I was surprised that you are not more popular
    given that you definitely possess the gift.

  2. I've recently started a web site, the info you offer on this website has helped me
    greatly. Thank you for all of your time & work.

Leave a Reply

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

Other Useful Resources

Creating a simple image gallery with PHP & jQuery illustration

An image gallery is a very common feature for a website to have. It is an efficient way to nicely present a set of pictures. With the increasing popularity of JavaScript plugin, image galleries are nowadays implemented with rich features, effects, and transitions, in order to emphasize the given content.

When you wish to create a picture gallery, there are several steps involved in the process. In this tutorial, we will see how you can set everything up, and then add, remove, or edit pictures in your gallery, in a simple and dynamic manner. Finally, we will explain how we can setup the jQuery lightbox plugin in order to work with our pictures.

What are the benefits of this solution? This methods is sooo easy to setup, but still achieves quite a lot. Little efforts, great rewards. You only have to put your images in a folder, and give that folder path to the class. You also tell the class how big you want your pictures to be, and it handles all the hard work for you. Just by calling a method, you can see your gallery on the page. So if you are looking for a fast and convenient way to put an image gallery on a site, read on.

Read more
How to make SEO friendly urls illustration

It's now quite common to swap the old url format containing file extensions and non explicit query strings by SEO friendly ones, only made of keywords and slashes, hence their name.

There are several techniques to do so, and I certainly do not have the pretention to say that this one is the best of all, but it is the one I use in personal projects as it works nicely, and it is robust and dynamic.

It makes use of an .htaccess file to rewrite the urls, of a php function to parse the given parameters, and a content manager to serve the appropriate content according to them.

Before we start I'd like to make a personal thanks to Josh Moont for his precious help.

Read more
Scroll to Top jQuery plugin illustration

If you happen to have on your site some pages that are a bit long, and therefore will lead your users to scroll down quite a bit to read your content, they may not have the patience to scroll again to get back up to the top. That's where this little plugin, hand made by NOE interactive comes in handy. Just put it on your website, and it will help your users get back to the top of pages with a single click, and with a small smooth animation (how nice is that :) ).

In this article featured on NOE interactive, you'll learn how you can install the jQuery plugin on your website. There's a complete step by step guide as well as a demonstration. If you wish to download the plugin, it's also hosted on Github.

Read more