Feeeeed Meee
RSS feeds are XML files that conform to the RSS specification. I think of RSS feeds as if they're describing a TV channel. The feed provides information about the channel itself, as well as information about individual programs that are available on the channel.
This way of thinking about the RSS feed is facilitated by the CHANNEL tag, which encompasses all the other tags in the file. Carrying the analogy further, each program on your TV channel is described via the ITEM tag. In podcasting terms, the ITEM tag encompasses all the information about a particular episode of the podcast.
The top-level RSS tag lets you specify XML namespaces as attributes. These namespaces are codified using XML Document Type Definition (DTD) files.
At this writing, there are two namespaces you almost certainly want to include in your feedone from iTunes and the other from Yahoo. If you want to add your podcast to the Yahoo podcast directory, you must include the Media DTD as an attribute of the RSS tag, even if you are not using any of the tags from the DTD. Otherwise, Yahoo won't accept your feed for inclusion in its directory.
You can place your RSS feed anywhere you like on your site. When deciding on a location, keep in mind that the URL to your feed will not only be used by the podcast aggregators but may also be passed around by your growing legion of listeners, so you probably want to keep the feed URL as short as possible.
When you've uploaded your initial feed and after every update to the feed, it makes sense to validate the entire feed. Fortunately, this is made easy by an online RSS feed validator at feedvalidator.org. Simply type in the feed URL, and the validator lets you know if you're ready to roll.
All You Need Is Feed
PHP by default includes XML parser functions (see us2.php.net/manual/en/ref.xml.php) that let you create a parser and specify callback functions that are called when the parser reaches the start tags, data within a tag, and the end tags in the XML file.
To encapsulate the process of obtaining information from the RSS feed, I've created the RSSInfo class (Listing One). The member data consists of two arraysone for CHANNEL data and one for ITEM data. The ITEM data will be a 2D array representing the 0 or more items that may be present in an RSS feed.
<?php // Get RSS info using passed in feed URL. class RSSInfo { // Constructor, parse passed in XML file function RSSInfo($url) { $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "StartTag", "EndTag"); xml_set_character_data_handler($xml_parser, "TagData"); $fp = fopen($url, "r") or sprintf("Could not open XML input from file %s.", $url); while ($data = fread($fp, 4096)) { xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } xml_parser_free($xml_parser); global $channel; global $allItems; $this->channel = $channel; $this->allItems = $allItems; } // end constructor // Member data, to hold channel and item data. // item data will be a 2D array. var $channel = array(); var $allItems = array(); } // globals for call backs $channel = array(); $allItems = array(); $currentItem = 0; $inItem = 0; $inImage = 0; // XML parser call backs // Start tag. function StartTag($parser,$tagName,$tagAttributes) { global $currentTag; $currentTag = $tagName; global $inItem; global $inImage; if($tagName == 'ITEM') $inItem = 1; elseif($tagName == 'IMAGE') $inImage = 1; // Get the URL attribute of the enclosure tag. // This will be used for the link to the podcast audio file. if($inItem && $currentTag == 'ENCLOSURE') { global $allItems; global $currentItem; $allItems[$currentItem]['URL'] = $tagAttributes['URL']; } } // End tag. function EndTag($parser,$tagName) { global $allData; // remove any while space at the beginning and end (optional) $allData = trim($allData); // If we are leaving an ITEM tag, note that and bump up the item count. global $inItem; global $inImage; global $currentItem; if($tagName == 'ITEM') { $inItem = 0; $currentItem++; } elseif($tagName == 'IMAGE') $inImage = 0; // We are not interested in any tags with no data. if($allData != '') { global $channel; global $allItems; global $currentTag; if($inItem) $allItems[$currentItem][$currentTag] = $allData; else // channel data { if($inImage) $currentTag = 'IMAGE:' . $currentTag; $channel[$currentTag] = $allData; } } $allData = ''; $currentTag = ''; } // Get the data between the start and end tags. function TagData($parser,$data) { global $currentTag; if($currentTag != '') { global $allData; $allData .= $data; } } ?>
The RSSInfo constructor takes a URL that is the RSS feed. Some of the code in this constructor is derived from the XML documentation available in the PHP manual. The constructor creates the parser, specifies the callback functions, parses the RSS feed, and frees the parser. After the RSS parsing is complete, I transfer the global variables to the class data members, channel, and item.