Feeds 101

Feeds 101

Oct 27th, 2009 in Other by Siddharth

Feeds. RSS. Atom. Syndication. Subscribers. These are some of the keywords floating around the web and have gained notorious prominence over the years. In this guide, we'll take a look at a number of things including what feeds are, why you need to have a feed for your site, how to set up one and then publish it.

PG

Author: Siddharth

Hi! I am Siddharth, a web developer/ designer based in Chennai with expertise in C#, Python, CSS and JavaScript . I resort to using jQuery when I don't feel like writing raw JS. I am passionate about web technologies and video games and I really think code *is* art. You can follow me if you want.

What are Feeds?

Feed image

In this digital age, users no longer have the luxury of time to check for new content manually each time or more importantly remember each site they want to get information from. Web feeds, news feeds or feeds helps the user simplify this process drastically.

Feeds, to put it simply, are a way to publish frequently updated content. Your feed is a XML formatted document which lets you share content with other users on the web. Users, subscribers in this lingo, can use your feed to read updated information on your site if and when it is posted.

Why you Should Publish Feeds

From a web developer's perspective, one of the main reason for publishing a feed is user convenience. With a feed for users to subscribe to, they don't have to check for new content manually each time. They can just subscribe to your feed and get notified new content is posted. No hassles! If you fear you'll lose your advertisement revenues in this process, you can just as easily include ads in the feed.

Publishing a feed also means that it is easier for third party content providers to syndicate your content thus gaining more exposure and traffic in the process.

Feed Formats

As with any hot technology, there are a few well established, competing protocols for creating web feeds.

RSS

Feed image

RSS is the dominant format for publishing web feeds and stands for Really Simple Syndication. RSS has a number of variants each branching out from RSS 1.x and RSS 2.x versions. A lot of services, including WordPress use RSS for creating its feeds.

Despite it's massive user base, RSS does suffer from some drawbacks, some significant, the most important one being its inability to handle HTML. Nevertheless, we'll be creating our feed today in the RSS format.

Atom

Atom logo

Atom was created in order to mitigate a lot of RSS' drawbacks including the ability to include properly formatted XML or XHTML in your feeds. But since RSS has almost become synonymous with feeds, Atom has always been the much more feature rich and flexible little brother.

RSS's Format

In the interest of keeping it simple, we'll just stick with RSS today instead of trying out each format out there.

Each and every RSS feed out there follows this general format:

Defining the version and encoding

RSS is a subset of XML which means we need to make sure it is marked so appropriately.

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"> 
.. 
</rss>

The first line is the XML declaration. We define the version so that it validates correctly as XML. The encoding part is purely optional.

The second line defines the version of RSS we are going to use today. We are going to use RSS 2 today.

Each feed need to be inside a channel so that goes inside the markup. Thus far our feed looks like so.

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"> 
<channel>
.. 
</channel>
</rss>

Filling in the feed's source information

This is where you fill in all the important details like the name of the feed, the URL and a description of the site.

<title>My feed</title>
 <link>http://www.somesite.com</link>
 <description>Random ravings :)</description>

You aren't limited to these fields alone. There are a number of other optional fields including the language of your feed, an image for the logo, when the feed was updated last and many more.

Adding the content

Each item in the feed has to be enclosed by an <item> element. An item can be anything: a news post, a status update, new products: anything. Each item requires a title and a corresponding link. As with before, you can make use of a number of optional elements including description and author fields.

A sample item would look like so:

<item>
<title>Feeds 101</title>
 <link>http://www.net.tutsplus.com</link>
 <description>Let's create an RSS feed from scratch!</description>
 <author>Siddharth</author>
</item>

Building a Static RSS Feed

Now that we know all the individual parts of a RSS file and how they all gel together, it's time to see a complete RSS file.

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"> 
<channel>
	<title>My feed</title>
   <link>http://www.somesite.com</link>
   <description>Random ravings :)</description>
   <item>
		<title>Feeds 101</title>
 		<link>http://www.net.tutsplus.com</link>
 		<description>Let's create an RSS feed from scratch!</description>
 		<author>sid@ssiddharth.com</author>
	</item>
</channel>
</rss>

It may not look like much but gents, this is a working RSS feed. We've defined everything that needs to be defined and if you are inclined to do so, you can put this on the web.

Building a Dynamic RSS Feed

Happy about building your first RSS feed? You should be! But the problem with this is that the feed is completely static: something which is completely counter intuitive as compared to the concept of feeds. We'll rectify this now by building a simple PHP script that mooches off data from a database and updates the RSS feed when needed.

Since I like having pretty URLs, I am going to name this file index.php and place it in a folder called feed so my feed can be accessed at www.mysite.com/feed

For the sake of simplicity, I am going to assume you already have a database containing your articles. I am also assuming the database has columns named title>, link, description and date in a table called posts.

Building the base

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"> 
<channel>
	<title>My feed</title>
   <link>http://www.somesite.com</link>
   <description>Random ravings :)</description>

 <?php
    // Code here
 ?>

</channel>
</rss>

Since the XML declarations and feed information are going to be pretty static, we'll keep them static. You'd want to keep them dynamic if you were writing a PHP class for generating RSS feeds but for our purposes, this should do.

Defining database information and connecting

DEFINE ('DB_USER', 'some_username');  
DEFINE ('DB_PASSWORD', 'some_unusually_weak_password');  
DEFINE ('DB_HOST', 'localhost');  
DEFINE ('DB_NAME', 'database'); 

Simple as it looks. We just note down a bunch of information for use later.

$connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or 
die('Connection to the specified database couldn't be established');  
mysql_select_db(DB_NAME)  or 
die ('Specified database couldn't be selected');  

Pretty generic connection code. We try to connect using the credentials noted earlier. If nothing hitches up, we select the relevant database for use later.

Querying the database

$query = "SELECT * FROM posts ORDER BY date DESC";  
$result = mysql_query($query) or die ("Query couldn't be executed");  

This isn't really a SQL oriented tutorial and so I'll skim over it. We just grab all the posts from the table so that we can add it to the feed. Nothing else fancy going on over there.

Populating the items list

while ($row = mysql_fetch_array($result, MYSQL_ASSOC) {
echo '<item>
		 <title>'.$row['title'].'</title>
		 <link>'.$row['link'].'</link>
		 <description>'.$row['description'].'</description>
	   </item>';
}

We grab each individual record and then print it inside the relevant element to create the items list. Note that since I wanted a hash to work with I set the result type to MYSQL_ASSOC.

And with that the PHP part is done. The complete code should look like below.

 <?php
     header("Content-Type: application/rss+xml; charset=utf-8");  
 ?>
 
 <?xml version="1.0" encoding="utf-8"?>
 <rss version="2.0"> 
 <channel>
	<title>My feed</title>
   <link>http://www.somesite.com</link>
   <description>Random ravings :)</description>

 <?php
    DEFINE ('DB_USER', 'some_username');  
	 DEFINE ('DB_PASSWORD', 'some_unusually_weak_password');  
	 DEFINE ('DB_HOST', 'localhost');  
	 DEFINE ('DB_NAME', 'database'); 

	 $connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or 
	 die('Connection to the specified database couldn't be established');  
	 mysql_select_db(DB_NAME)  or 
	 die ('Specified database couldn't be selected');  

	 $query = "SELECT * FROM posts ORDER BY date DESC";  
	 $result = mysql_query($query) or die ("Query couldn't be executed");  

	 while ($row = mysql_fetch_array($result, MYSQL_ASSOC) {
		echo '<item>
			 	<title>'.$row['title'].'</title>
		 		<link>'.$row['link'].'</link>
		 		<description>'.$row['description'].'</description>
	  	 	  </item>';
	 }
 ?>

 </channel>
 </rss>

You should now be able to access your feed at www.yoursite.com/feed.

Validate your Feed

Feed image

Just like with xHTML, RSS/XML needs to be well-formed and without errors. There are a number of validators to help you with this. Here are some of my often used ones.

Since RSS can only handle escaped HTML, make sure you use &lt; lt; for < and &lt; gt; for > respectively. Also make sure you replace special characters to their respective HTML codes. Forgetting to do so will probably result in invalid markup and break the feed.

All Done! Publish that Feed

Feed image

Now that we've created the feed and made sure it validates, we can now go publish it. You can use a service like Feedburner to manage your feeds. This lets you glean a lot of information including how many subscribers you have. Or you can take the easy way out and just link to your feed on your site.

Have you ever noticed the feed icon on your browser lighting up for certain pages alone? This means the browser has been notified that a feed of the current page is available for subscription. In order for the user's browser to automatically detect the feed's presence you need to add this small snippet to the head section of your page:

<link rel="alternate" type="application/rss+xml" title="Article RSS Feed" 
href="http://www.yoursite.com/feed" />

You need not limit yourself to one feed. You may have a feed for each author or a feed for each category of the products you sell. Feel free to add as many feeds you want to the head section.

Conclusion

And that brings us to an end to this joy ride. We've gone over what feeds are, what purpose they serve and the different formats available. Next we looked at RSS, its skeleton structure and then learned how to create a simple dynamic RSS feed. Hopefully you've found this tutorial interesting and this has been useful to you.

Questions? Nice things to say? Criticisms? Hit the comments section and leave me a comment. Happy coding!


Related Posts

Check out some more great tutorials and articles that you might like

Enjoy this Post?

Your vote will help us grow this site and provide even more awesomeness

Plus Members

Source Files, Bonus Tutorials and
More for $9 a month for all TUTS+
sites in one subscription.

Join Now

User Comments

( ADD YOURS )
  1. PG

    Stoian Kirov October 27th

    Great tutorial!!!
    Exactly what i need! :) ;)

    ( Reply )
  2. PG

    NetChaos October 27th

    Not the usual stuff (tutorial) but really useful one. Thanks a lot.

    ( Reply )
  3. PG

    Myfacefriends October 27th

    thanks… really useful!

    ( Reply )
  4. PG

    Ethan October 27th

    I would really like going over how to structure Atom–it is truly a better feed format than RSS.

    ( Reply )
  5. PG

    Juan C Rois October 27th

    Thanks for the tutorial, it is very useful and it helped refresh my memory because it’s been a while since the last time I set up an RSS feed.

    I particularly like the idea of receiving my feeds as soon as they are ready.
    Unfortunately I find myself not wanting to deal with the RSS reader (I use gmail), since it takes just as much effort to log into it as it does to go to the source site directly and check for new content myself.

    Thanks for the tutorial.

    ( Reply )
    1. PG

      Siddharth October 27th

      You said thank you twice. Thank you for that. :)

      ( Reply )
      1. PG

        Juan C Rois October 27th

        I just wanted you to know that I do appreciate your effort, as well as all the other contributions from the authors posting their articles and tutorials to NetTuts.

      2. PG

        Siddharth October 27th

        Just to clarify, I wasn’t making fun: I was appreciating the gesture. Kind words always lifts one’s spirits.

  6. PG

    Aziz Light October 27th

    Thanks a lot. Nice Tutorial.

    What would be awesome is to now have a tutorial on how to create RSS and Atom feeds in CodeIgniter :)

    ( Reply )
    1. PG

      Siddharth October 27th

      Look for it in the future. :)

      ( Reply )
    2. PG

      Andi October 28th

      Here is an old but good tutorial about creating rss with codeigniter.
      derekallard.com/blog/post/building-an-rss-feed-in-code-igniter/

      ( Reply )
  7. PG

    Hamza Oza October 27th

    Thanks. Always had problem understanding it.

    ( Reply )
    1. PG

      Siddharth October 27th

      I’m really glad you liked it.

      ( Reply )
  8. PG

    Daniel Whyte October 27th

    Awesome, i need to use these more.

    ( Reply )
  9. PG

    Aaron October 27th

    Thanks for posting this, very helpful.

    ( Reply )
  10. PG

    Mark Kadlec October 27th

    Very nice, it was interesting from the PHP perspective (versus the .net way I have done it in the past).

    Thanks!

    ( Reply )
  11. PG

    William Rouse October 27th

    I learned much from the tutorial so thank you for that. I don’t see how to use this as a script however. It can’t be run as a PHP script. There are some errors with the use of the single quote (‘) in the error message and a missing parenthesis “)” in the while loop, but aside from that where does the output go. Are you expecting this to be printed to the browser or a file, I am confused.
    Thanks!

    ( Reply )
    1. PG

      Siddharth October 27th

      Eeeks. I did miss a ‘)’ in the while loop. It’s meant to read: while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

      ( Reply )
    2. PG

      Alec Gorge October 27th

      Yeah, there are several parse errors in the samples he provides. I noticed 2 just scanning:

      die(‘Connection to the specified database couldn’t be established’);
      should be
      die(‘Connection to the specified database couldn\’t be established’);

      and

      while ($row = mysql_fetch_array($result, MYSQL_ASSOC) {
      should be
      while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

      Even the syntax highlighter doesn’t show this right! Jeffrey! You need to fix this! (Since Siddharth can’t fix this himself because us writers don’t have the permissions)

      ( Reply )
      1. PG

        William Rouse October 27th

        Yeah those are the ones I found, but how do you run this thing? Is this suppose to go to a browser, save as a file? I don’t see how that is done in this example? Am I off base?
        WBR

      2. PG

        Siddharth October 28th

        Alex, thank you for pointing those out. I’ll ask Jeffrey to fix them. :)

  12. PG

    akosipau October 27th

    i didn’t realize how easy it is to make a feed script. thank you for this. very useful

    ( Reply )
  13. PG

    alan October 27th

    This is a great tutorial.
    Your staff is always interesting!

    ( Reply )
  14. PG

    Jon Desrosiers October 27th

    I received a weird parse error from PHP, unexpected T_STRING at the first < character of the doc.
    Not sure why this was happening, however fixed it by outputting the <?xml tag with an echo statement under the header().

    ( Reply )
  15. PG

    Alex October 27th

    A simple idea for a tutorial but very interesting and well done! Thanks! May just join the pro again as I want to see the iPhone tut one lol

    ( Reply )
  16. PG

    Shiro October 27th

    this is a good tutorial to understand how RSS work, great work!

    ( Reply )
  17. PG

    Tomáš Fejfar October 27th

    Good to understand how it works. Awfult if used like a howto :) You should definetly use appropriate classes (at least SimpleXML, better Zend_Feed – see http://framework.zend.com/manual/en/zend.feed.importing.html#zend.feed.importing.custom.dump)

    ( Reply )
  18. PG

    Peter October 27th

    A poor article by Nettuts+ standards both in terms of the content and the writing itself. Did an editor even look over this article before it was published; I’d assume so, meaning this quality of article is ok? Maybe I am just wearing my critical hat.

    Siddharth, thank you for taking the time to contribute an article to Nettuts+ and for introducing or refreshing the concept of feeds and generating RSS documents. Your writing style is very informal (not such a bad thing) and full of little errors both in how sentences or paragraphs are structured and the overall flow of the article. I certainly do not wish to be “the internet troll” but would like to ask you to take perhaps a more structured and considered approach to your future articles.

    On a technical note, the PHP code offered will not execute with the desired result. It will immediately fail with a “Parse error” due to the XML processing instruction on line 5. After fixing that, there are a number of other errors (some have already been mentioned in the comments) making the script a not particularly good example of generating an RSS feed.

    I am guessing that this comment appears quite negative but please be assured that all points made are simply an observation unintended to cause offense. Thank you again, Siddharth, for submitting the Feeds 101 overview—a fine read, just not quite what I expect from here.

    ( Reply )
    1. PG

      Siddharth October 28th

      No offense taken. I am still working on my writing skills and I’ll keep your feedback in mind whilst writing my next one.

      Thanks for reading.

      ( Reply )
      1. PG

        Siddharth October 28th

        About the XML processing instruction, it seems to work fine on my end. It’s probably PHP shorthand declaration messing with the XML declaration.

        You can either turn shorthand declaration off or take the easier route and just hold the entire static XML part within a string.

    2. PG

      Eye October 28th

      Maybe you should make an effort to write an tutorial yourself instead of comming down with stupid comments.

      Just a thought. It´s easy to give criticism but to create something is more difficult.

      ( Reply )
  19. PG

    Ignas October 28th

    simple, but good enough to read for newbies, so I think you done well :)

    ( Reply )
  20. PG

    Powler October 28th

    Nice tutorial …

    But escaping the HTML caracters is a really bad idea : How can you put images with this technique?

    You should read about the CDATA section :
    http://www.w3schools.com/xmL/xml_cdata.asp

    Thank you again :)

    ( Reply )
  21. PG

    Benjamin Reid October 28th

    Great tutorial, I didn’t even know you could add PHP to XML, doh!

    ( Reply )
    1. PG

      Siddharth October 28th

      Glad you found it useful.

      ( Reply )
  22. PG

    Siddharth October 28th

    The parse errors were completely my fault and I apologize.

    If you are trying to get the script to run, add the extra ) to the while loop and escape the single quotes.

    If I find anything else, I’ll post here and get Jeffrey to fix it.

    Thanks for reading.

    ( Reply )
  23. PG

    Siddharth October 28th

    I forgot to add this to the tut so here goes: the XML declaration will conflict with the shorthand PHP declaration.

    You can either turn shorthand declaration off or take the easier route and just hold the entire static XML part within a string to use later.

    ( Reply )
  24. PG

    Akino October 28th

    Excelent Article…

    ( Reply )
  25. PG

    ron October 28th

    Great tutorial, I am a newbie so i will have to read it again and again.

    ( Reply )
  26. PG

    mattvot October 28th

    Great Tut!

    A few errors in the PHP but I won’t hold it against you :)

    Thanks

    ( Reply )
  27. PG

    Tom Mayers October 28th

    On an unrelated note I wanted to say how distracting I find the lazyload script you are employing of late.

    Whilst I am trying to read I am knocked about the page like a drunken sailor as I scroll down. It’s incredibly annoying. Especially as the images in this article were more or less unnecessary the whole thing added up to a less than optimum experience.

    Thanks for the content though, as ever.

    ( Reply )
  28. PG

    w1sh October 28th

    Thanks Siddharth. I always wanted to know how to legitimately incorporate feeds.

    Btw, I like how you describe RSS as having a few drawbacks and give it a low-res image, and talk about how Atom is awesome and give it a high res pic. It’s the little, subliminal touches that really set an article ap-All glory to the Hypnotoad.

    ( Reply )
    1. PG

      Siddharth October 28th

      What can I say? My preference for Atom seems to have crept in sub-consciously. :)

      ( Reply )
  29. PG

    esranull October 28th

    thanks, very usefull

    ( Reply )
  30. PG

    DemoGeek October 28th

    Make sure to HTML Encode (figure that out in the programming language of your choice) the title and the description to make sure the feed functions properly.

    ( Reply )
  31. PG

    Cameron Baney October 28th

    Excellent information and tutorial. I always saw Atom, but never really knew what it was.

    ( Reply )
  32. PG

    Dileep K Sharma October 28th

    I disagree with other comments. Although its a good article but its a redundant one. You can easily find many resources over the web talking about the same thing. Nettut is about something new and exciting. I am very much disappointed with this one.

    ( Reply )
  33. PG

    LivelyWebTuts October 28th

    Very Useful!! :)

    ( Reply )
  34. PG

    Dillon October 28th

    Great article. Very informative.

    ( Reply )
  35. PG

    designfollow October 28th

    thanks for this great info

    ( Reply )
  36. PG

    Siku October 29th

    Thanks for the great post :) Very informative.

    ( Reply )
  37. PG

    Sathish October 29th

    Wonderful Siddharth! Keep pumping!

    ( Reply )
  38. PG

    Marcio Toledo October 29th

    Siddharth, this is your best tutorioal! very useful ^^

    ( Reply )
  39. PG

    sreeram October 30th

    Thanks siddharth for this info.

    ( Reply )
  40. PG

    Sophia Knight October 30th

    This is great and very useful. I just uploaded a static feed 1 minute ago and didnt think Nettuts would have a tut on this. But you have everything. Thanks so much.

    ( Reply )
  41. PG

    Vasu October 31st

    die(‘Connection to the specified database couldn’t be established’);

    Shouldn’t this be
    die(‘Connection to the specified database couldn\’t be established’);

    ( Reply )
    1. PG

      Dillon October 31st

      Well, yeah, it should. I forgot to escape those characters as I mentioned above. :)

      ( Reply )
      1. PG

        Siddharth October 31st

        Err. You? :)

        As he said you’ll need to escape the single quote as I mentioned in a comment above.

  42. PG

    kosaidpo November 1st

    tnx for the nice tuto
    ;]

    ( Reply )
  43. PG

    Linux And Friends November 3rd

    Thanks for the tutorial. When I try to subscribe to certain websites, I find they provide multiple RSS feeds for the same content namely RSS 2.0, RSS 1.0 and so on.

    So what is the difference between RSS 2.0 and RSS 1.0 ? Does subscribing to RSS 2.0 provide additional features not provided by RSS 1.0 or Atom ?

    Really informative article.

    ( Reply )
  44. PG

    Alex November 12th

    Cool very nice !

    And very usefull ;-)

    ( Reply )
  45. PG

    Swenflea December 3rd

    I can’t get it to work… Can you please create a file I can download thanks

    ( Reply )
  1. Arrow
    Gravatar

    Your Name
    December 3rd