HighlyStructured.com is a blog website run by Mike D'Agostino about search engine positioning, online marketing, php/MySQL, tae kwon do, and various other topics.

Creating an RSS Feed Using PHP

December 08, 2005

In a previous article I wrote about the benefits of having an RSS news feed on your site to syndicate your content and increase your ranking on Google and the other search engines. This is great, but how can website developers create their own RSS feed? I will describe the techniques I've used to create an RSS feed for this website using a MySQL database and PHP.

The procedure is very similar to creating static HTML pages from database content using PHP. In essence you are retrieving content from a database, writing code to a variable, and then writing that variable to a file using the PHP function FOPEN. I actually find creating an RSS feed using PHP easier and simpler than creating the static HTML archive pages.

Description of the basics of an RSS document

RSS, like HTML, uses tags to delineate different sections of content. The tags still use brackets (<>), only the tag title are slightly different. The different versions of RSS seem to use different methodologies for creating the code, but I'm sticking with RSS 2.0 which is what I used to create the RSS news feed for this site.

I would think of the RSS code as being divided into two sections: the "header" code and the "body" code. The "header" code contains the base elements including the xml version signifier, the rss version signifier, and the channel code. The channel code is more or less a description of who is authoring the RSS feed.

The "body" code contains the actual headlines/descriptions. Each headline/description consists of, among other tags, a title tag, a link tag, and a description tag. The title and description tags are obvious, and the link tag is the URL back to the actual article that the title/description describe.

For a full explanation of all the RSS 2.0 tags you can use please see the RSS 2.0 specification page.

Writing the RSS News Feed PHP Script

The first step in the script is to write the beginning RSS/XML code to a variable. Again, I've used RSS version 2.0 for the RSS news feed on this site, and the beginning code might look something like this:

$rss_code .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; $rss_code .= "<rss version=\"2.0\">\n"; $rss_code .= "<channel>\n"; $rss_code .= "<title>YourSite's RSS Syndication</title>\n"; $rss_code .= "<link>http://www.yoursite.com/newsfeed.xml</link>\n"; $rss_code .= "<description>Site/RSS news feed description goes here</description>\n"; $rss_code .= "<pubDate>$todays_date</pubDate>";

One word of warning - for complete RSS 2.0 validation, you must use the date and time specification of RFC 822. More on RSS 2.0 validation below...

After that, it is time to query the MySQL database and get your headlines/content. Now, you may have tens or hundreds or thousands of entries in your database. But, you should really only use the most recent entries for your RSS syndication. How would you feel subscribing to an RSS news feed and receiving 400 headlines from one site every time you opened your RSS reader? I would limit your RSS feed to about 10 or so entries.

Once you've queried your database to retrieve your articles, you need to run the htmlspecialchars PHP function to remove any HTML tags. RSS does not like HTML characters! When I first put my RSS feed together for this site I ran into problems getting the code to validate because my entries contained hyperlink and other HTML tags causing the RSS validation to fail. In addition, I use stripslashes to remove any backslashes in the entry code.

Now you are ready to continue putting together the RSS/XML code. I would loop through each entry (again, limiting the loop to a 10-count) assigning variables to the title, description, and URL (must be a field in the database "entries" table), and compile the code as such:

$rss_code .= " <item> <title>$title</title> <link>http://www.yourdomain.com/$html_variable_name</link> <description>$description...</description> </item> ";

Now you have the "header" and "body" RSS/XML code, and you simply need to close the code out:

$rss_code .= " </channel> </rss> ";

Now you have your entire XML document code saved into one variable! All you have to do is use the FOPEN function in PHP to write your variable to a filename of your choice and you're done! Well almost...you should really validate your RSS/XML news feed code to ensure it will be read correctly (if at all) by standardized RSS news readers and RSS parsers. The Feed Validator site is a great RSS/XML code validator and even tells you which lines of code you have errors.

Your last step is to put a nice little "RSS Feed" icon on your site linking to your RSS/XML news feed syndication URL and people can start subscribing to your news feed.

As a sidenote...I've incorporated my RSS News Feed Syndication PHP script into my "generate HTML archive" script. So, every time I generate the static HTML archive of this site, the RSS news feed is automatically updated/created. It doesn't make sense to offer an RSS news feed if you don't update the content!

Technorati Tags:             

Recent Articles

Topics

Archive

Other Blogs of Interest