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.

Leave a Reply

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

Other Useful Resources

Form Cloning jQuery Plugin illustration

Building dynamic forms is a task that a lot of web developers will have to do at some point. By dynamic I mean forms that change based on what the user inputs.

A basic example of dynamic forms, are those that allow the user to add the same group of information several times. For example, an attendance form, where you can add several persons, a booking form where you can add several tickets, a membership with several users, a media page with several pictures or videos and so on, the possibilities are countless.

In order to facilitate the development of such forms, I created a little plugin, that allows you to dynamically clone a specific set of information that will be repeated. So if you ever searched a way of cloning forms, fieldsets or groups of input, this could really be helpful.

Read more
How to integrate your instagram pictures with php illustration

Ever looked for a straightforward way to integrate you instagram pictures on your website? Without a plugin nor an iframe? Well this article featured on the NOE interactive's blog presents how you can do so with PHP.

It's a neat and simple solution that uses the instagram's API to get a particular users' latest pictures, and then display them in a ul list.

It is accessible to most developers as it is a "personalize, copy and paste" kind of solution. Just two steps are required, configuration, and implementation.

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

Read more
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
@Afup_Mtp @DuaelFr Ha ha cool Edouard. Tu seras en terrain connu en plus 😉.15 Nov / 17:39