ASP.NET PHP.NET CODEITWELL MASHABLE OPENSOURCEWEBDESIGN BLOGDUMPS FACEBOOK APPS

Home ASP.NET PHP WINDOW Web Programming Web Logic Web Design css Archives
Monday, October 5

How to create RSS Feed and Why RSS Feed explained

0 comments

You've probably seen them on the web, even if you don't know what they're used for; those little orange buttons that look like they're broadcasting something are everywhere. They're links to RSS feeds. In this tutorial, we'll explore some of the uses for RSS feeds, why your website should have them, and you'll also learn how to make static feeds with XML and dynamic feeds using PHP.

In its simplest form, RSS is really just a list of similar links with a title and description. RSS is an acronym for Really Simple Syndication and it is a dialect of XML. RSS comes in three versions: 0.91, 0.92, and 2.0; with version 2.0 as the most current and common version. You can download the sample files for this tutorial here.

How is RSS used?

RSS logo The standard RSS logo.

RSS is used in many different ways by both people and websites. In general, because RSS works as a syndication channel for a list of links, any consumer of the information in that channel is called a subscriber.

One of the largest benefits to RSS is that you can view headlines from multiple sites without having to visit them directly. This is usually done using a program called a feed reader, which gathers all of the different RSS feeds that you instruct it to and displays them in a central location. Although there are many different feed readers out there, Google Reader and My Yahoo! are two of the more popular ones.

Many RSS feeds are updated dynamically whenever new content is added and thus enable subscribers to visit sites only when there are updates. This saves an enormous amount of time for users who wish to keep track of news or information on a variety of websites. Some RSS feeds even contain the entire piece of content and allow subscribers to view it without ever having to leave their feed reader.

Websites can also get the contents of an RSS feed and display them as a section of their website. Because websites aren't limited to just their own RSS feeds, this allows them to display content from all over the internet. Although we won't cover reading RSS feeds in this tutorial, displaying external RSS feeds on your site can be a great way to augement your already existing content.

Why would I want an RSS feed for my site?

Allowing your users to view your content without ever visiting your site or allowing other sites to syndicate your content may seem counter-intuitive, but take a look at the benefits:

  • You are providing a convenience for your users by saving them time and giving them another way to access your fresh content
  • Users who subscribe to your feeds are interested in your content and are probably more loyal than other users
  • Syndication on other sites will give your site more exposure

If you're worried about what an RSS feed might do to your ad revenue, you have a valid concern, but there are ways to mitigate the problem. You aren't required to put the entire body of content in the description of each link. You can place a brief description with a link to read more, or you can eliminate the description altogether- it's up to you. You can also display ads in your feed. Google is also currently beta testing AdSense for Feeds, Yahoo already has it, and there are other ad networks that offer ads for RSS feeds as well. However, RSS ads apparently work a little differently than your normal contextual ads.

A simple, static example of RSS

If you're not comfortable with working with a server-side language like PHP, don't worry, you can create an RSS feed without using PHP. First we'll discuss the different components of RSS by building a static feed that you'll have to edit manually to update. Then, if you're up to it, we'll convert it into the dynamic version using PHP and MySQL that updates automatically whenever new content is added into your database.

As mentioned above, RSS is a dialect of XML. If you're familiar with HTML, you've worked with the concept of tags. The tag defines a link or an anchor, the

tag creates a paragraph, and so on. XML is a neat language because it allows you to define your own tags and attributes. In fact, XHTML is the HTML tags defined in an XML format. RSS is another use for XML. It's simply an agreed upon standard for the purpose of syndication.

Required elements

Let's go over a brief example of an RSS feed with the required components to every feed and then we'll discuss them in a little more depth. After that, we'll look at some of the optional components. We'll be working with RSS version 2.0, but after reading this tutorial, you should be able to use the other versions if you need to without much trouble.

  1. version="2.0">
  2. >
  3. >Virtuosi Media
  4. >http://www.virtuosimedia.com
  5. >A feed for articles on web development
  6. >
  7. >Tutorial: How to Create RSS Feeds
  8. >http://www.virtuosimedia.com/article.php?id=60

First, we need to specify that we are creating an XML document and which version of XML we are using. After that, the rest of the document will be nested inside of the tag, which, in the case of our example, has a version attribute with a value of 2.0 indicating that it follows the RSS 2.0 specification. (Note: If you're familiar with XML, the reason RSS 2.0 doesn't have a namespace declaration is so that it won't invalidate earlier versions of RSS.) In addition to being nested inside the tag, everything is also nested inside the tag. Take care to include the closing tags.

The , <link>, and <description> tags all describe the channel in greater detail.</p> <ul><li><strong><title></strong> - The title of the feed</li><li><strong><link></strong> - A link to the website where the feed may be found</li><li><strong><description></strong> - A description of the feed</li></ul> <p>The <item> tag indicates a list item in the RSS feed. It could refer to an article, a forum post, or almost any other number of things. The <title> tag nested within the <item> tag indicates the title of the page or element being linked to and the <link> tag is that item's unique URL. You can have as many items in an RSS feed as you desire.</p> <p>That's all there is to creating a simple RSS feed. We could stop there, but there are more things that we can do with it, so let's take a look at the optional tags by looking first at more tags that describe the channel and then additional item tags.</p> <h3>Optional channel elements</h3> <p>The following are optional tags that further describe the channel and should be placed before any <item> tags:</p> <ul><li><strong><language></strong> - The language of the feed. The language can be specified by using the language codes specified <a href="http://www.rssboard.org/rss-language-codes" rel="nofollow">here</a>.</li><li><strong><copyright></strong> - The copyright that governs the content.</li><li><strong><managingeditor></strong> - The email address of the managing editor of the content of the feed. The name can follow the address in parentheses.</li><li><strong><webmaster></strong> - The email address of the webmaster responsible for the feed. The name can follow the address in parentheses.</li><li><strong><pubdate></strong> - The date of feed publication, according to the <a href="http://asg.web.cmu.edu/rfc/rfc822.html#sec-5" rel="nofollow">RFC822</a> specified format.</li><li><strong><lastbuilddate></strong> - The last date the feed was updated, according to the <a href="http://asg.web.cmu.edu/rfc/rfc822.html#sec-5" rel="nofollow">RFC822</a> specified format.</li><li><strong><category></strong> - One or more categories that classify the feed. Has an optional attribute of 'domain' which specifies a URL explaining the category. </li><li><strong><generator></strong> - The name of the program used to generate the feed.</li><li><strong><docs></strong> - A URL that contains the technical specifications for the feed syntax.</li><li><strong><cloud></strong> - Specifies how the RSS feed can be accessed by web services. For more information, click <a href="http://www.rssboard.org/rsscloud-interface" rel="nofollow">here</a></li><li><strong><ttl></strong> - Stands for time to live, which determines how long (in minutes) the feed should last in the cache before being refreshed.</li><li><strong><image></strong> - An image for the feed. Images can be in .PNG, .JPEG, or .GIF formats. The <image> tag is a sub element of the <channel> tag and has sub elements of its own. <ul><li><strong><url></strong> - A required sub element of <image>. Specifies the path where the image may be found.</li><li><strong><title></strong> - A required sub element of <image>. Acts as the ALT text and describes the image when the feed is rendered in HTML.</li><li><strong><link></strong> - A required sub element of <image>. A link to the website where the feed may be found.</li><li><strong><height></strong> - An optional sub element of <image> specifying its height in pixels. Its default value is 31 and its maximum value is 400.</li><li><strong><width></strong> - An optional sub element of <image> specifying its width in pixels. The default value is 88 and the maximum value is 144.</li><li><strong><description></strong> - An optional sub element of <image> that serves as the title attribute when the feed is rendered in HTML. </li></ul> </li><li><strong><rating></strong> - Specifies a <a href="http://www.w3.org/PICS/" rel="nofollow">PICS (Platform for Internet Content Selection)</a> rating for the feed.</li><li><strong><textinput></strong> - Allows for a text input to be inserted into the feed, but it is ignored by most feed readers and aggregators. Contains four required sub elements. <ul><li><strong><title></strong> - A required sub element of <textinput>. Specifies the value of the Submit button used in the form.</li><li><strong><description> </strong>- A required sub element of <textinput>. Describes the purpose of the text input.</li><li><strong><name></strong> - A required sub element of <textinput>. The name of the text input element.</li><li><strong><link></strong> - A required sub element of <textinput>. The URL of the page that processes the form element. (The specification doesn't state whether or not the form is sent using GET or POST, so you may want to experiment with it.) </li></ul> </li><li><strong><skiphours></strong> - Hours during which rearchival should be skipped by any aggregator or feed reader that supports the feature. The values are 0 to 23, with 0 equaling midnight GMT. </li><li><strong><skipdays></strong> - Days during which rearchival should be skipped by any aggregator or feed reader that supports the feature. The values are the days of the week, written in full: Monday, Tuesday, etc. </li></ul> <h3>An updated example using the optional channel elements</h3> <p>Having gone through the optional channel elements, let's put some of them to use.</p> <pre style="display: none;" class="html"><?xml version="1.0"?> <rss version="2.0"> <channel> <title>Virtuosi Media http://www.virtuosimedia.com A feed for articles on web development en-us 2008 Virtuosi Media jane.doe@example.com (Jane Doe) john.doe@example.com (John Doe) Tue, 08 Jul 2008 22:31:45 EDT Tue, 08 Jul 2008 22:31:45 EDT Web Development Virtuosi Media RSS Generator http://www.rssboard.org/rss-specification 60 http://www.virtuosimedia.com/images/logo_fancy.png Virtuosi Media http://www.virtuosimedia.com 95 133 A web development resource center Tutorial: How to Create RSS Feeds http://www.virtuosimedia.com/article.php?id=60

  1. version="2.0">
  2. >
  3. >Virtuosi Media
  4. >http://www.virtuosimedia.com
  5. >A feed for articles on web development
  6. >en-us
  7. >2008 Virtuosi Media
  8. >jane.doe@example.com (Jane Doe)
  9. >john.doe@example.com (John Doe)
  10. >Tue, 08 Jul 2008 22:31:45 EDT
  11. >Tue, 08 Jul 2008 22:31:45 EDT
  12. >Web Development
  13. >Virtuosi Media RSS Generator
  14. >http://www.rssboard.org/rss-specification
  15. >60
  16. >
  17. >http://www.virtuosimedia.com/images/logo_fancy.png
  18. >Virtuosi Media
  19. >http://www.virtuosimedia.com
  20. >95
  21. >133
  22. >A web development resource center
  23. >
  24. >Tutorial: How to Create RSS Feeds
  25. >http://www.virtuosimedia.com/article.php?id=60

Optional item elements

All right, so far we've covered how we can modify the channel itself, but the whole point of the RSS format is being able to view the items in each channel. Let's take a look at the optional item elements that will help us describe each item even further. Technically, all sub elements of are optional, but it must contain either a or <description>.</p> <ul><li><strong><title></strong> - The title of the item.</li><li><strong><link></strong> - The URL of the item.</li><li><strong><description></strong> - The description of the item. HTML (including images) can be entered here, but it needs to be <a href="http://www.rssboard.org/rss-encoding-examples" rel="nofollow">entity-encoded.</a></li><li><strong><author></strong> - The email address of the author of the item.</li><li><strong><category></strong> - The category of the item. Follows the same guidelines as the <category> sub element for <channel>.</li><li><strong><comments></strong> - The URL of comments about the item. Often, if the item is an article or blog post, there will be a section following the content that allows for comments.</li><li><strong><enclosure></strong> - A media object related to the item. <enclosure> is a self-closing tag that has three required attributes: <ul><li><strong>url</strong> - The URL of the media object, beginning with http.</li><li><strong>length</strong> - The size of the media object in bytes.</li><li><strong>type</strong> - The <a href="http://www.iana.org/assignments/media-types/" rel="nofollow">MIME type</a> of the media object.</li></ul> </li><li><strong><guid></strong> - A globally unique identifier for the item, in string format.</li><li><strong><pubdate></strong> - The date the item was published, according to the <a href="http://asg.web.cmu.edu/rfc/rfc822.html#sec-5" rel="nofollow">RFC822</a> specified format.</li><li><strong><source></strong> - The RSS channel of the item. There is one required attribute of 'url', which specifies the URL of the feed. The value of the <source> tag should be the same as the <title> sub element of <channel>.</li></ul> <h3>Example using the optional item elements</h3> <p>For our updated example, we're going to omit the rest of the channel data and just display the item data, but in a real example, you'll want to put it all together. Remember, you can have as many items as you want. </p> <pre style="display: none;" class="html"><item> <title>Tutorial: How to Create RSS Feeds http://www.virtuosimedia.com/article.php?id=60 You've probably seen them on the web, even if you don't know what they're used for, those little orange buttons that look like they're broadcasting something. They're links to RSS feeds. In this tutorial, we'll explore some of the uses for RSS feeds, why your website should have them, and you'll also learn how to make them with and without PHP. email@example.com (generic author name) RSS Tue, 08 Jul 2008 22:31:45 EDT Virtuosi Media

  1. >
  2. >Tutorial: How to Create RSS Feeds
  3. >http://www.virtuosimedia.com/article.php?id=60
  4. >You've probably seen them on the web, even if you don't know what they're used for, those little orange buttons that look like they're broadcasting something. They're links to RSS feeds. In this tutorial, we'll explore some of the uses for RSS feeds, why your website should have them, and you'll also learn how to make them with and without PHP.
  5. >email@example.com (generic author name)
  6. >RSS
  7. >Tue, 08 Jul 2008 22:31:45 EDT
  8. url="http://www.virtuosimedia.com/mainfeed.php">Virtuosi Media

Once you've created your feed, you can save your file with an XML or RSS extension and upload it to your web server. We'll talk about feed validation, directories, and a few more related subjects a little further down.

Going dynamic with PHP and MySQL

Now that we've looked at static RSS feeds that you have to edit by yourself every time, we're going to move on dynamic RSS feeds that will update automatically every time new and relevant data is entered in your database. For our purposes, we're going to use PHP as our server scripting language, but you could certainly do the same with any other similar language. We'll also be using MySQL, but again, you're not limited to that flavor of SQL; you can use PostgreSQL, SQLite, Oracle, MS SQL, DB2, or any other DBMS system you desire. (If you have never used PHP or MySQL, head on over to Tizag for some quick tutorials or reference the PHP.net and the MySQL documentation.) Since displaying recent articles is a common use of RSS, we'll use that to illustrate how to create a dynamic feed.

If you already have your articles set up in your database, you'll have to adapt our example here to fit your own case, but it won't be too hard. For those that don't already have their articles in the database, you're going to need to create a database table using the following MySQL:

  1. CREATE TABLE 'articles' (
  2. 'article_id' INT NOT NULL AUTO_INCREMENT ,
  3. 'article_title' VARCHAR( 200 ) NOT NULL ,
  4. 'article_text' TEXT NOT NULL ,
  5. 'article_url' VARCHAR( 300 ) NOT NULL ,
  6. 'article_author' VARCHAR( 70 ) NOT NULL ,
  7. 'article_category' VARCHAR( 40 ) NOT NULL ,
  8. 'article_date' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
  9. PRIMARY KEY ( 'article_id' )
  10. )

At this point, you'll probably want to populate your database with a few dummy articles (you can use the lorem ipsum text for your main content if you can't think of anything to write). If you don't know how to do that, take a look at the MySQL documentation for INSERT query syntax.

Once you have at least three articles in your database, create a new file in your favorite text editor called rss.php. It's important to note that you will need to use the long PHP tags () in order differentiate from the XML tags () and you will probably have to have the short tags turned off. If you don't know how to do that and the example doesn't work, use PHP tags for the entire file and echo the channel data. We're going to start by using our channel data from above:

  1. version="2.0">
  2. >
  3. >Virtuosi Media
  4. >http://www.virtuosimedia.com
  5. >A feed for articles on web development
  6. >en-us
  7. >2008 Virtuosi Media
  8. >jane.doe@example.com (Jane Doe)
  9. >john.doe@example.com (John Doe)
  10. >Tue, 08 Jul 2008 22:31:45 EDT
  11. >Tue, 08 Jul 2008 22:31:45 EDT
  12. >Web Development
  13. >Virtuosi Media RSS Generator
  14. >http://www.rssboard.org/rss-specification
  15. >60
  16. >
  17. >http://www.virtuosimedia.com/images/logo_fancy.png
  18. >Virtuosi Media
  19. >http://www.virtuosimedia.com
  20. >95
  21. >133
  22. >A web development resource center

Although we could write a class that dynamically fills in the channel data, we'll stick to keeping it the channel data static and the PHP programming procedural for this tutorial. Next, we'll begin our PHP code. You'll first need to establish a connection to a database, which you can do by using the mysql_connect function. For this example, we'll connect by including a separate file called connect.php, but you can connect in the same file if you wish. After the connection is established, we're going to want to construct a SELECT query to get our article data from the articles table. Let's also assume that we want to sort the results with the most recent at the top and also that we only want 5 records. We'll also need to format the date correctly. To do all of that, we'll use the following code:



//Connect to the database here
require_once ('connect.php');

//Create the SELECT statement and execute it
$query = "SELECT *, DATE_FORMAT(article_date, '%a, %d %b %Y %T PST') AS article_pubdate FROM articles ORDER BY article_date LIMIT 5";
$result = mysql_query($query);

The next step is to iterate over each record from our query and to echo it, along with the XML information, onto the page. To do that, we'll use a while loop and the mysql_fetch_array function with the result type parameter set to MYSQL_ASSOC so that we can retrieve an associative array.

//Iterate over the rows to create each item

while ($row = mysql_fetch_array($result, MYSQL_ASSOC) {
echo '
'.$row['article_title'].'
'.$row['article_url'].'
'.$row['article_text'].'
'.$row['article_author'].'
'.$row['article_category'].'
'.$row['article_pubdate'].'
Virtuosi Media

}

?>

Now that we've closed off the PHP tag, we'll simply need to close the and tags as well.

If everything has gone according to plan, you'll now have a working dynamically updating RSS feed that display the last 5 articles in your database and lists them with the most recent article at the top. Just to compare notes, your entire rss.php file should look like this:





Virtuosi Media
http://www.virtuosimedia.com
A feed for articles on web development
en-us
2008 Virtuosi Media
jane.doe@example.com (Jane Doe)
john.doe@example.com (John Doe)
Tue, 08 Jul 2008 22:31:45 EDT
Tue, 08 Jul 2008 22:31:45 EDT
Web Development
Virtuosi Media RSS Generator
http://www.rssboard.org/rss-specification
60

http://www.virtuosimedia.com/images/logo_fancy.png
Virtuosi Media
http://www.virtuosimedia.com
95
133
A web development resource center



//Connect to the database here
require_once('connect.php');

//Create the SELECT statement and execute it
$query = "SELECT *, DATE_FORMAT(article_date, '%a, %d %b %Y %T PST') AS article_pubdate FROM articles ORDER BY article_date LIMIT 5";
$result = mysql_query($query);

//Iterate over the rows to create each item
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) {
echo '
'.$row['article_title'].'
'.$row['article_url'].'
'.$row['article_text'].'
'.$row['article_author'].'
'.$row['article_category'].'
'.$row['article_pubdate'].'
Virtuosi Media

}

?>



Wrapping it up

There are still a few loose ends that apply to both static and dynamic feeds that need to be wrapped up and so we'll cover those issues here.

Validation

Once you've created your feed, you'll also want to check to make sure there are no validation errors. To do that, you can use the Feed Validator tool provided by the W3C. The validator should also point out additional ways to ensure that your feed is interoperable across a wide range of feed readers.

Creating an feed icon in the address bar

You can signal to the browser that a feed is available on any webpage. Firefox will display the feed icon in the address bar and Internet Explorer 7 will display it on the toolbar. To do so, simply enter the following line of code into the of your HTML document, making sure to change the information to match your website.

  1. rel="alternate" type="application/rss+xml" title="Virtuosi Media Article RSS Feed" href="http://www.virtuosimedia.com/mainfeed.php" />

If you have multiple feeds, you can add the above line multiple times, changing it as needed for each feed.

Other syndication formats

RSS is the only syndication format out there. Atom is another popular format. Though Atom is more flexible, at this point, it seems as if RSS has the market share. However, you can easily supply both formats for your website using the principle you learned in this tutorial.

Another, more recent RSS flavor is GeoRSS, which allows you to encode geographic information into both RSS and Atom feeds. This may not be appropriate for every feed, but it will allow different feeds to be grouped and mashed up by location.

Getting your feed out there

Having a feed is a great feature that extends the functionality of your website, but you also want to let people know about it. First, be sure to make it accessible on your website by letting people know that it exists and where to find it. You may also want to submit your feed to a service outside your website. FeedBurner is a very popular service that aggregates and helps promote almost 2 million feeds while also providing subscription statistics. Syndic8 is another large feed directory.

Other uses for RSS

We've used a common example of displaying recent articles for this tutorial, but RSS has many more uses. It can be used to display any similar information that gets updated. Here are just a few of the ways RSS can be used:

  • Recent articles, stories, or blog posts
  • Forum or message board posts
  • Search results
  • Directory listings
  • Classified ads
  • Products
  • Product reviews
  • Events
  • Status updates (microblogging)
  • Auction bids
  • Galleries
  • Support or error tickets

Sources

This tutorial references and paraphrases the RSS specification available at http://www.rssboard.org/rss-specification, which is provided under a Creative Commons Attribution/Share Alike license. For more detailed information on the technical specification, please visit the RSSBoard website.

Conclusion

Quite obviously, RSS has many different and exiting uses. It's a great way to syndicate and subscribe to information. We hope this tutorial has been useful to you. If so, feel free to subscribe to our feed, link to us, or otherwise promote us around the web. We'll be publishing more articles like this from time to time and also have a few tools for web developers in the works that should be appearing in the coming months.

Comments
0 comments
Do you have any suggestions? Add your comment. Please don't spam!
Subscribe to post feed

Your comments here...

Some Useful Books

http://books.google.com.np/books?id=Lan3g76cCFYC&lpg=PP1&dq=photoshop%20shortcuts&hl=en&pg=PT1#v=onepage&q=photoshop%20shortcuts&f=false

About Me

SubscribeSubscribe via RSS

SubscribeRecent Posts

SubscribeHot Links

Archive

Live Traffic Feed