PHPRO.ORG

This class, as the name suggests, creates an RSS feed. The rss class extends the DOMDocument class that is built into PHP which enables direct access to all the functionality of the DOMElement extension.

Each rss item is defined as an array and passed to the addItem method. Items include images and other elements which have sub types. The addItem method returns an instance of the class to enable method chaining as seen in the Example Usage.

The rss class supports all elements from the RSS2 Specification and even includes images, skipDays, and skipHours which contain sub elements. Sub elements are simply passed as an array of values, and then added to the XML structure.

The supported element names are held in a switch (see phpswitch.com) to ensure only valid RSS elements are provided, and also delegates what should happen to each valid element when declared. Invalid elements are quitly discarded as there can be no exceptions thrown when using __toString.

The __toString method is provided to do away with the need for a seperate getRSS method so the object is echo\'ed directly in this example. However, it is recommended that when using this class, the output is written to a file and the file used as the point of access for rss feeds. This will save much overhead, particularly if the items are generated from a database result set.


<?php

/**
 *
 * A class to create RSS feeds using DOM
 *
 * @Author Kevin Waterson
 *
 * @copyright 2009
 *
 * @author Kevin Waterson
 *
 * @license BSD
 *
 */
class rss extends DomDocument
{
    
/**
     * @ the RSS channel
     */
    
private $channel;

    
/**
     *
     * @Constructor, duh! Set up the DOM environment
     *
     * @access public
     *
     * @param string $title The site title
     *
     * @param string $link The link to the site
     *
     * @param string $description The site description
     *
     */
    
public function __construct($title$link$description)
    {
        
/*** call the parent constructor ***/
        
parent::__construct();

        
/*** format the created XML ***/
        
$this->formatOutput true;

        
/*** craete the root element ***/
        
$root $this->appendChild($this->createElement('rss'));

        
/*** set to rss2 ***/
        
$root->setAttribute('version''2.0');

        
/*** set the channel node **/
        
$this->channel $root->appendChild($this->createElement('channel'));

        
/*** set the title link and description elements ***/
        
$this->channel->appendChild($this->createElement('title'$title));
        
$this->channel->appendChild($this->createElement('link'$link));
        
$this->channel->appendChild($this->createElement('description'$description));
    }


    
/**
     *
     * @Add Items to the RSS Feed
     *
     * @access public
     *
     * @param array $items
     *
     * @return object Instance of self for method chaining
     *
     */
    
public function addItem($items)
    {
        
/*** create an item ***/
        
$item $this->createElement('item');
        foreach(
$items as $element=>$value)
        {
            switch(
$element)
            {
                
/*** create sub elements here ***/
                
case 'image':
                case 
'skipHour':
                case 
'skipDay':
                
$im $this->createElement($element);
                
$this->channel->appendChild($im);
                foreach( 
$value as $sub_element=>$sub_value )
                {
                    
$sub $this->createElement($sub_element$sub_value);
                    
$im->appendChild$sub );
                }
                break;

                case 
'title':
                case 
'pubDate':
                case 
'link':
                case 
'description':
                case 
'copyright':
                case 
'managingEditor':
                case 
'webMaster':
                case 
'lastbuildDate':
                case 
'category':
                case 
'generator':
                case 
'docs':
                case 
'language':
                case 
'cloud':
                case 
'ttl':
                case 
'rating':
                case 
'textInput':
                case 
'source':
                
$new $item->appendChild($this->createElement($element$value));
                break;
            }
        }
        
/*** append the item to the channel ***/
        
$this->channel->appendChild($item);

        
/*** allow chaining ***/
        
return $this;
    }

    
/***
     *
     * @create the XML
     *
     * @access public
     *
     * @return string The XML string
     *
     */
    
public function __toString()
    {
        return 
$this->saveXML();
    }
}
?>

Example Usage


<?php

/*** the first article item ***/
$item1 = array(
    
'title'=>'Zend Framework Example Site'
    
'link'=>'http://www.phpro.org/articles/Zend-Framework-Example-Site.html'
    
'description'=>'This example site hopes to introduce the newcomers to Zend Framework in a friendly way, by providing a simple modular site layout and can have the newcomer up and running in minutes.'
    
'pubDate'=>date(DATE_RSS),
    
'image'=>array('link'=>'http://phpro.org''url'=>'http://phpro.org/images/spork.jpg''title'=>'SPORK'),
    
'language'=>'en');

/*** second article item ***/
$item2 = array(
    
'title'=>'Xajax-In An Object Oriented Environment',
    
'link'=>'http://www.phpro.org/tutorials/Xajax-In-An-Object-Oriented-Environment.html',
    
'description'=>'This tutorial takes the next step in development and shows, by way of example, how xajax can be utilized in an Object Oriented environment',
    
'pubDate'=>date(DATE_RSS),
    
'language'=>'en');

/*** third article item ***/
$item3 = array(
    
'title'=>'Introduction to SPL DirectoryIterator',
    
'link'=>'http://www.phpro.org/tutorials/Introduction-to-SPL-DirectoryIterator.html',
    
'description'=>'The DirectoryIterator is one of the more oft used of the Iterator classes and this tutorial helps to expose the user to developing in a standardised and Object Oriented approach',
    
'pubDate'=>date(DATE_RSS),
    
'language'=>'en');

/*** a new RSS instance, pass values to the constructor ***/
$rss = new rss('PHPRO.ORG''http://phpro.org''PHP Articles Tutorials Examples Classes');

/*** add the items from above ***/
$rss    ->addItem($item1)
    ->
addItem($item2)
    ->
addItem($item3);

/*** show the RSS Feed ***/
echo $rss;

?>

Demonstration

<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>PHPRO.ORG</title>
    <link>http://phpro.org</link>
    <description>PHP Articles Tutorials Examples Classes</description>
    <image>
      <link>http://phpro.org</link>

      <url>http://phpro.org/images/spork.jpg</url>
      <title>SPORK</title>
    </image>
    <item>
      <title>Zend Framework Example Site</title>
      <link>http://www.phpro.org/articles/Zend-Framework-Example-Site.html</link>
      <description>This example site hopes to introduce the newcomers to Zend Framework in a friendly way, by providing a simple modular site layout and can have the newcomer up and running in minutes.</description>

      <pubDate>Thu, 01 Jan 2009 08:11:20 +1100</pubDate>
      <language>en</language>
    </item>
    <item>
      <title>Xajax-In An Object Oriented Environment</title>
      <link>http://www.phpro.org/tutorials/Xajax-In-An-Object-Oriented-Environment.html</link>
      <description>This tutorial takes the next step in development and shows, by way of example, how xajax can be utilized in an Object Oriented environment</description>

      <pubDate>Thu, 01 Jan 2009 08:11:20 +1100</pubDate>
      <language>en</language>
    </item>
    <item>
      <title>Introduction to SPL DirectoryIterator</title>
      <link>http://www.phpro.org/tutorials/Introduction-to-SPL-DirectoryIterator.html</link>
      <description>The DirectoryIterator is one of the more oft used of the Iterator classes and this tutorial helps to expose the user to developing in a standardised and Object Oriented approach</description>

      <pubDate>Thu, 01 Jan 2009 08:11:20 +1100</pubDate>
      <language>en</language>
    </item>
  </channel>
</rss>