Shelling the Pod
by David Summer

Listing One

<?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 begining 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;
  }
}
?>


2


