Creating a Custom RSS Feed with PHP and MySQL | WebReference

Creating a Custom RSS Feed with PHP and MySQL

By Kris Hadlock

RSS has become the standard technology for syndicating information to large audiences. Many people have something to say, but its finding the right audience for your voice that matters. A great place to start is by creating your own RSS feed and adding to it as often as you can.

In this article you'll learn how to syndicate your own custom RSS feeds using PHP and MySQL. We'll first learn how to create two database tables and then how to retrieve data from them which will be formatted into an RSS feed. Here's an example of the completed RSS feed and a download link to the code. Let's start by taking a look at how to create the MySQL database tables.

Creating the MySQL RSS Tables

An RSS feed consists of the main details for the feed, such as the title, description, url, image and so on. Next are the items, probably the most important parts of the feed. i.e., different stories from a newspaper, posts from a blog and so on. Therefore, we will create two database tables, the first is called webref_rss_details, which contains the details for the feed and the second is called webref_rss_items, which contains all of the items. If you would like to get a better idea of the RSS structure you can take a look at The Anatomy of an RSS Feed, which is a previous article I wrote on the basics of the RSS structure.

In order to get started with the database tables, use the following code, which can be found in the webref_rss_details.sql file, to create the webref_rss_details table:

The following code, which can be found in the webref_rss_items.sql file, will create the webref_rss_items table:

Our first table, webref_rss_details, contains 10 columns, which are the id, title, description, link, language, image_title, image_url, image_link, image_width and image_height. Each of these columns are self-explainatory once you are familiar with the structure of an RSS feed, the non-RSS related column is the id, which is used to represent the id for the row of data in the database. The second table, webref_rss_items, contains 4 columns, which include the id, title, description and link. In the example I'm linking to the home page of the current Web site, but this URL would typically link to the location of the original text, such as a specific blog page where an entry has been posted. Now that we officially have the MySQL tables created, add the data that you want to syndicate, then proceed to the next section and take a look at how to construct a valid RSS 2.0 feed with PHP.

Creating a valid RSS 2.0 feed with PHP

Once the database tables are in place we'll create three PHP files. The first is called index.php, which belongs in the root of our site directory, the second RSS.class.php, which belongs in a directory called classes and the third is mysql_connet.php, which should be placed in a directory that is inaccessible to the outside world. For the purposes of this article I placed the sample file in the same directory as the RSS class. In the index.php file, start by adding a header that will configure the content type of the document as valid XML and choose the charset that you need. Next, include the RSS class, instantiate the object and trigger a method called GetFeed. The GetFeed method will return the actual RSS feed once it has been constructed in the class, so perform an echo on the return value to write the data to the index file.

The mysql_connect.php file defines our database connection information, makes the initial connection and selects the defined database.

The RSS class constructor is a public method that requires the mysql_connect.php file. Remember to put this file in a safe location on your server so it's not exposed to hackers. Once the file's in place change the path in the RSS constructor. The next method in the RSS class is GetFeed, which is a public method that we're calling from the index. This method will concatenate the details and items and return a valid RSS 2.0 structure. The first method it calls is a private method called getDetails. This method makes a connection to our webref_rss_details table and selects all of the values. Once the results are retreived the RSS file is constructed from the database data in a while loop that iterates the rows in the database. Once complete the getDetails method returns the details for the RSS structure.

The second method that GetFeed calls is a private method called getItems. This method selects all of the values in the webref_rss_items table and iterates them. While iterating the values a string of XML is constructed that represents the item structure for the RSS file. Once complete, the getItems method returns the item structure to the GetFeed method, which in turn returns the entire RSS structure to the index to be written to the page.


This code sample is a good start for a complex custom RSS feed creator. We could easily create a form that adds or updates new items in our database. For now I give you the base because I'm sure you have many ideas of your own. Armed with this new RSS file you can go to a Web site such as FeedBurner to make it easier to deliver your content to the public.

About the Author

Kris Hadlock is the co-founder of 33Inc alongside Robert Hoekman. He is the author of Ajax for Web Application Developers and has been a feature writer for numerous Web sites and design magazines.

Original: November 1, 2006