How Do I Set Up an Automatic RSS Feed on My Webpage?
Setting up an automated RSS (Really Simple Syndication) feed for a webpage is a snap. All that's needed is a bit server-side scripting. In this example, PHP will be used, but any programming language such as Java, Ruby, Perl or C# will certainly do the trick.<br /><br />An RSS feed is simply an XML document--it's just text. The purpose of RSS is to easily distribute news items and content that can be gobbled up by an RSS reader and viewed by a user. On his blog, Mark Nottingham describes RSS as an \"XML-based format that allows the syndication of lists of hyperlinks, along with other information, or metadata, that helps viewers decide whether they want to follow the link.\" In other words, it's an easy way to syndicate content on the web.
Instructions
-
-
1
Choose which RSS standard you want to implement. As often happens with Web \"standards,\" there are many standards to choose from--RSS 1.0, RSS 2.0 or Atom. There are also RSS 0.9, 0.91 and 0.92, but these are largely out of date and should be avoided.<br /><br />Choosing which format to implement isn't really that big of a deal. While there are differences among the formats, they are minor. As long as a web developer creates a valid feed for the format she has chosen, any news reader or aggregator should be able to read the feed just fine.<br /><br />In this example, an RSS 2.0 feed will be created because it's the most simple of the three modern formats.<br /><br />The basic structure of an RSS 2.0 feed looks like this:<br /><br /><?xml version=\"1.0\"?><br /><rss version=\"2.0\"><br /> <channel><br /> <title>My News Feed</title><br /> <link>http://www.yourdomain.com/</link><br /> <description>This is the news from my site.</description><br /> <item><br /> <title>How to Setup An Automated RSS Feed for a Web Page</title><br /> <link>http://www.yourdomain.com/2010/02/04</link><br /> <description>Setting up an automated RSS for Web page is a snap.</description><br /> </item><br /> <item><br /> <title>You can have as many items as you have stories</title><br /> <link>http://www.yourdomain.com/2010/02/05</link><br /> <description>There once was a man from Nantucket ...</description><br /> </item><br /> </channel><br /></rss>
-
2
To create this feed every time the page loads, build a simple PHP script. First pull the stories you want to syndicate from the database. Then set the header response to text/xml before dumping out a well-formed RSS 2.0 feed to the browser or aggregator via HTTP.<br /><br />In a real-world implementation, the web developer would pull the list of stories from her CMS database. But for the sake of simplicity, in this example, a hard-coded simple data structure of an array of hashes is created using PHP:<br /><br /><?php<br />$stories[0]['title'] = 'How to Setup An Automated RSS Feed for a Web Page'<br />GO<br />$stories[0]['link'] = 'http://www.yourdomain.com/2010/02/04'<br />GO<br />$stories[0]['description'] = 'Setting up an automated RSS feed for a Web page is a snap.'<br />GO<br />$stories[1]['title'] = 'You can have as many items as you have stories'<br />GO<br />$stories[1]['link'] = 'http://www.yourdomain.com/2010/02/05'<br />GO<br />$stories[1]['description'] = 'There once was a man from Nantucket ...'<br />GO<br /><br />// Set the header to text/xml so that the aggregator or Web browser knows this an XML document and not HTML.<br />header('content-type:text/xml; charset=UTF-8')<br />GO<br />echo \"<?xml version=\\\"1.0\\\"?>\\n\"<br />GO<br />echo \"<rss version=\\\"2.0\\\">\\n\"<br />GO<br />echo \"<channel>\\n\"<br />GO<br />echo \"<title>My News Feed</title>\\n\"<br />GO<br />echo \"<link>http://www.yourdomain.com/</link>\\n\"<br />GO<br />echo \"<description>This is the news from my site.</description>\\n\"<br />GO<br />for ($i = 0; $i < @count($stories); $i++) {<br /> echo \"<item>\\n\"<br />GO<br /> echo \"<title>\" . $stories[$i]['title'] . \"</title>\\n\"<br />GO<br /> echo \"<link>\" . $stories[$i]['link'] . \"</link>\\n\"<br />GO<br /> echo \"<description>\" . $stories[$i]['description'] . \"</description>\\n\"<br />GO<br /> echo \"</item>\\n\"<br />GO<br />}<br />echo \"</channel>\\n\"<br />GO<br />echo \"</rss>\\n\"<br />GO<br />?><br /><br />Of course, this is a simplified version of what a developer would implement on a live website, but it shows how easy it is to create an RSS feed for any webpage.
-
-
3
To get web users to subscribe to your RSS feed, add the following code to each page that has syndicated content:<br /><br /><link rel=\"alternate\" type=\"application/rss+xml\" title=\"My RSS Feed\" href=\"rss.php\" /><br /><br />Putting this tag inside the head tag of a webpage will add the little RSS logo to the location bar of the browser, making it simple for anyone to click and subscribe. Of course, adding a link directly from the website should also be done to further promote the presence of the RSS feed.
-
1
References
Resources
- Photo Credit Hemera Technologies/AbleStock.com/Getty Images