How to Create a Simple RSS Feed using PHP and MySQL

Generating a RSS feed with PHP is actually very easy. I have had to do it a few different times and every time I do it I have to look up the syntax of the XML document. To save myself time in the future I thought I would document the process here for myself and others who may need to create a RSS feed using PHP. (Note: This tutorial assumes that your server is running on apache and that you can use .htaccess files.)

.htacces Setup
The first step I always use when creating a RSS feed with PHP is to set a value in my .htaccess file that indicates that files with the type .xml should be interpreted as PHP. Your browser or RSS reader is going to be looking for pages with a .xml extension and creating a dynamic .xml file using PHP is easier than creating a number of static XML files for each feed or updating a .xml text file using a PHP script, at least in my opinion, although this is certainly possible if you don’t have the ability to use .htaccess or don’t know how. A simple example of an .htaccess entry is included below:

AddType application/x-httpd-php .xml

Also using .htaccess you can create a custom RSS feed by variable by using a statement similar to:

RewriteEngine on
RewriteRule ^/?podcast/([0-9]+).xml?/?$ 	/podcast.php?id=$1

Using this statement would mean that going to http://www.yoursite.com/podcast/23.xml would execute the script located at http://www.yoursite.com/podcast.php?id=23 passing the 23 from the address to your script so that you could create an RSS feed specifically for id 23 which could be a user id, a unique page, etc.

The Code
The first function is a simple PHP header command which designates this page as an xml document. This should be the first item on the page and no whitespace or output should take place prior to this in the script.

The PHP code example below is a simple example of a very basic RSS feed using PHP. I have done this to avoid confusion that may occur with a more complicated example.

The first section is what I call the RSS feed headers. This section basically represents information about the feed. This is generic information about the feed.

The second of section which I call the body of the RSS feed. This section should be repeated for each item in the RSS feed. (Note: Everything in between item and /item would be repeated for each item in the RSS feed.) This can be done manually or it could be automated using PHP and a MySQL query or other type of loop to build a number of entries. For this example I showed this area as a static single entry to help avoid confusion.

The final section closes out the channel and RSS feed and the final function sends the complete RSS feed to the browser.

All in all this is a very basic example and I hope it works for everyone. I have tried to keep it as basic as possible. Here is the complete PHP script. Don’t forget to setup your .htaccess file or this won’t work as expected!

<?PHP
 
    //SET XML HEADER
    header('Content-type: text/xml');
 
    //CONSTRUCT RSS FEED HEADERS
    $output = '<rss version="2.0">';
    $output .= '<channel>';
    $output .= '<title>Your RSS Feed Name or Website Name</title>';
    $output .= '<description>A description of your feed or site.</description>';
    $output .= '<link>http://www.yoursite.com/</link>';
    $output .= '<copyright>Your copyright details</copyright>';
 
    //BODY OF RSS FEED
   $output .= '<item>';
        $output .= '<title>Item Title</title>';
        $output .= '<description>Item Description</description>';
        $output .= '<link>Link to Item</link>';
        $output .= '<pubDate>Date Published</pubDate>';
   $output .= '</item> ';
 
    //CLOSE RSS FEED
   $output .= '</channel>';
   $output .= '</rss>';
 
    //SEND COMPLETE RSS FEED TO BROWSER
    echo($output);
 
?>

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

[...] to just create an RSS Feed all you have to do is remove the enclosure line! Or click here for a tutorial on creating RSS feeds with PHP and MySQL Share and [...]

Wow, thank you very much. I searched for a solution to enable my feed to precisely track its amount of readers, and your line in .htaccess seems to solve all my problems.

Cool!

Man, that is the most powerful custom RSS Feed tutorial that I have seen so far. Clean & clear, simply and powerful. You are my hero. Many thanks.
Bruce

@Bruce Glad to hear you found it useful.

can u please help me?
if i have to read the feed from a site
and then display it on my site
how will it be done?

i am done with about 75% work

the only problem i get is with the tag
it gives error “not well-formed”

please help!!!!!!!!

thanks in advance

There are many ways to parse the feed you create. I have a simple example here: http://www.stemkoski.com/how-to-easily-parse-a-rss-feed-with-php-4-or-php-5/ or you can use the built in XML functions in PHP. If you are getting an error not saying the XML is not well formed there is probably some sort of error with your coding or the data within the code. Try to determine where the error is and run htmlentities() on the data prior to building the feed. Also make sure there are no invalid characters in your content: http://www.stemkoski.com/php-remove-non-ascii-characters-from-a-string/.

i have installed estoreplazza template on my website
it is working fine
but there is one problem

the menu bars are not visible…
in the backend it says “cannot assign default template”

can someone please help me how to enable menu bars in estoreplazza template

Hi this is really helpful i was searching for this but when i validate my created feed it says that “A feed needs to contain publish dates or GUIDs in order to work” :( is there anyway to automate this? And also how can set up my feed to automatically update once it changes? i think its a cron job thingy hope you can help me

I am thinking of implementing something like that in http://islandinfo.mu website.

It’s really nice the way you have arranged those php codes, it’s easy to understand and use it.

Thanks :)

Leave a comment

(required)

(required)