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 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.

Page 1 of 2

1 2
Next Page

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date