Generating a podcast 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 podcast RSS feed using PHP. (Note: This tutorial assumes that your server is running on apache and that you can use .htaccess files.)

.htaccess Setup
The first step I always use when creating a podcast 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 would execute the script located at 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 podcast 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!

    header('Content-type: text/xml');
    $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></link>';
    $output .= '<copyright>Your copyright details</copyright>';
   $output .= '<item>';
        $output .= '<title>Item Title</title>';
        $output .= '<description>Item Description</description>';
        $output .= '<link>Link to Item</link>';
        $output .= '<enclosure url="" length="6768" type="audio/mpeg"/>'; //URL: FULL FILE PATH   LENGTH: IN BYTES
        $output .= '<pubDate>Date Published</pubDate>';
   $output .= '</item> ';
   $output .= '</channel>';
   $output .= '</rss>';

If you’re looking 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