Providing RSS Feeds for your website is fast becoming a necessity in today's web2.0 world. The amount of surfers taking advantage of everything RSS Feeds has to offer has exploded in recent history, and it's easy to see why. The amount gained from offering a RSS Feed far outweigh the effort needed to create one.

RSS Feeds offer your visitors to keep up to date with any recent activity on your website without the hassle of checking in every day. The purpose of this article is to show how to create a static RSS feed for your website. Why static? Because it is less of a drain on your web server's resources. So here we go.

The first thing this article assumes is that you have a MySQL database with the records you wish to have in your feed. For this example lets say we have a table of articles.

This is the beginning of our file. As you can see I plan to store the entire file into a string variable $feed. The first few lines of the .rss file are the general name, language, and description of the website. The next part is to query the database and output some articles into the feed.

  So we have asked the database to give us the 5 latest articles from the table and are about to loop them so we can output each to the .rss file.

 This adds this chunk of code to our $feed variable. The <item> tag tells feed readers that a new item is starting. It's pretty obvious what the rest is except for possibly <pubdate>. This contains the Publish Date for the item.

In every case you may have noticed that I have wrapped everything with a custom function xmlentities(). This is not a php function but a function I stole from someone who was kind enough to leave it as a note in the PHP.net manual. This function translates characters that are not compatible with RSS into ones that are.

Once $feed has been appended to contain our items we can finish it up. 

This concludes the contents of our rss file. Now to make it static. First there needs to be a file named 'feed.rss' with proper permissions for PHP to write to this file. 

 And that's it! Note: file_put_contents if PHP version 5>

The reason we have to loop file_put_contents until it returns true, or succeeds, is because the server might be accessing that file while people are viewing it so PHP will overwrite the file at the next available opportunity. This is my method for making, or updating, any kind of static page. 

 

Hope this helps. Here is some extra reading on the ins and outs of RSS.

http://www.xul.fr/en-xml-rss.html- Building and Using RSS Feeds.

Get my RSS Feed!

Comments

No comments yet. Be the first!

* Name
* Email (Will not be displayed)
Website