Feeds 101

Feeds 101

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.

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!


Siddharth is Siddharth on Codecanyon
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Stoian Kirov

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

  • http://dealrobo.com NetChaos

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

  • http://myfacefriends.com Myfacefriends

    thanks… really useful!

  • http://ethan.luffle.com Ethan

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

  • http://www.fatlizardmedia.com Juan C Rois

    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.

    • http://www.ssiddharth.com Siddharth
      Author

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

      • http://www.fatlizardmedia.com Juan C Rois

        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.

      • http://www.ssiddharth.com Siddharth
        Author

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

  • http://twitter.com/aziz_light Aziz Light

    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 :)

    • http://www.ssiddharth.com Siddharth
      Author

      Look for it in the future. :)

    • http://creative-webdesign.info Andi

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

  • http://www.000webhost.com/205252.html Hamza Oza

    Thanks. Always had problem understanding it.

    • http://www.ssiddharth.com Siddharth
      Author

      I’m really glad you liked it.

  • http://www.danielwhyte.com Daniel Whyte

    Awesome, i need to use these more.

  • http://www.awmcreative.com Aaron

    Thanks for posting this, very helpful.

  • Mark Kadlec

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

    Thanks!

  • William Rouse

    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!

    • http://www.ssiddharth.com Siddharth
      Author

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

    • http://ramblingwood.com Alec Gorge

      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)

      • William Rouse

        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

      • http://www.ssiddharth.com Siddharth
        Author

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

  • http://wpcrunchy.com akosipau

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

  • http://www.urbanvideos.tv alan

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

  • http://www.jonathandesrosiers.com Jon Desrosiers

    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().

  • Alex

    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

  • Shiro

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

  • http://blog.red-pill.cz Tomáš Fejfar

    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)

  • Peter

    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.

    • http://www.ssiddharth.com Siddharth
      Author

      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.

      • http://www.ssiddharth.com Siddharth
        Author

        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.

    • Eye

      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.

  • http://butenas.com Ignas

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

  • Powler

    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 :)

  • http://www.nouveller.com/ Benjamin Reid

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

    • http://www.ssiddharth.com Siddharth
      Author

      Glad you found it useful.

  • http://www.ssiddharth.com Siddharth
    Author

    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.

  • http://www.ssiddharth.com Siddharth
    Author

    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.

  • Akino

    Excelent Article…

  • http://www.arcaderush.net ron

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

  • mattvot

    Great Tut!

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

    Thanks

  • Tom Mayers

    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.

  • w1sh

    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.

    • http://www.ssiddharth.com Siddharth
      Author

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

  • http://os.laroouse.com esranull

    thanks, very usefull

  • http://www.demogeek.com DemoGeek

    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.

  • http://blog.cameronbaney.com Cameron Baney

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

  • http://www.dileepsharma.com Dileep K Sharma

    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.

  • http://web.livelytuts.com LivelyWebTuts

    Very Useful!! :)

  • Dillon

    Great article. Very informative.

  • http://www.designfollow.com designfollow

    thanks for this great info

  • http://twitter.com/sik00 Siku

    Thanks for the great post :) Very informative.

  • http://bardo.in Sathish

    Wonderful Siddharth! Keep pumping!

  • Marcio Toledo

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

  • sreeram

    Thanks siddharth for this info.

  • http://set24.org Sophia Knight

    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.

  • Vasu

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

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

    • Dillon

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

      • http://www.ssiddharth.com Siddharth
        Author

        Err. You? :)

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

  • http://xsaiddx.0fees.net/ kosaidpo

    tnx for the nice tuto
    ;]

  • http://linuxandfriends.com Linux And Friends

    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.

  • http://www.actarus.ch Alex

    Cool very nice !

    And very usefull ;-)

  • http://swenflea.com Swenflea

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

  • http://www.forgenepal.com Forge nepal

    Really Great Tutorial .. Thanks

  • http://www.forgenepal.com Forge nepal

    I am using RSS and atom in my website too ..

  • http://www.playsite247.com/ Mario

    tnx for the nice tuto

  • http://www.monstertruckgames1.com Monster Truck Games

    Someday I’ll be drowned in encodings:)

  • http://www.briskgames.com Parking Games

    Really Great Tutorial, thanks a lot