Feed your site with RSS

Do you need to feed your site? It's possible with RSS. Just use this script and you are done! You can set multiple feeds and show them as one list.

Does this script work? Yes, I'm using it on my frontpage to show delicious links.

Requirements

this script needs PHP5 or higher. It uses the DOMDocument class to parse XML

The Script

$iRssItems = 20;

$oRSS = new RSSreader();
$oRSS->setRSSfeed( 'http://del.icio.us/rss/dicabrio' );
$oRSS->parseFeeds();

if( $oRSS->length() < $iRssItems )
{
  $iRssItems = $oItemList->length;
}

echo '<ul>';
for( $i=0; $i < $iRssItems; $i++ )
{
  $oRSSitem = $oRSS->getFeedItem( $i );
  echo '<li><span title="'. $oRSSitem->getTag( 'link' ) 
           .'">'.substr( $oRSSitem->getTag( 'title' ), 0, 28 ) 
           . '</span></li>';
}
echo '</ul>';


// classes
class RSSreader extends DOMDocument
{
  private $m_aRSSfeeds;
	
  private $m_aFeedItems;
	
  public function __construct()
  {
    parent::__construct('1.0', 'UTF-8');
    $this->m_aRSSfeeds = array();
  }

  public function setRSSfeed( $p_sFileName )
  {
    if( !in_array( $p_sFileName, $this->m_aRSSfeeds ) )
    {
      $this->m_aRSSfeeds[] = $p_sFileName;
    }
  }

  public function parseFeeds()
  {
    // Loop throught the feeds specified
    for( $i=0; $i < count( $this->m_aRSSfeeds ); $i++ )
    {
      parent::load( $this->m_aRSSfeeds[$i] );
      $this->m_oFeedItems = parent::getElementsByTagName( 'item' );
      foreach( $this->m_oFeedItems as $oFeedItem )
      {
        $this->m_aFeedItems[] = new RSSfeedItem( $oFeedItem );
      }
    }
  }
  
  public function getFeedItem( $p_iItemNum )
  {
    if( isset( $this->m_aFeedItems[$p_iItemNum]) )
    {
      return $this->m_aFeedItems[$p_iItemNum];
    }
  }
  
  public function getFeedItems()
  {
    return $this->m_aFeedItems;
  }
  
  public function length()
  {
    return count( $this->m_aFeedItems );
  }
}

class RSSfeedItem
{
  private $m_oFeedItem;
  
  public function __construct( $p_oFeedItem )
  {
    $this->m_oFeedItem = $p_oFeedItem;
  }
  
  public function getTag( $p_sTagName )
  {
    return $this->m_oFeedItem->getElementsByTagName( $p_sTagName )¶
->item(0)->nodeValue;
  }
}