Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Web Development

Shelling the Pod


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 feed—one 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 arrays—one 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;
  }
}
?>
Listing One

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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.