PHP class generating RSS feed

Published:

Add / read comments

RSS (Rich Site Summary) is a format for delivering regularly changing web content. Many weblogs and other online publishers syndicate their content as an RSS Feed to whoever wants it. RSS solves a problem for people who wants to easily stay informed by retrieving the latest content from the sites you are interested in.

You save time by not needing to visit each site individually. You ensure your privacy, by not needing to join each site's email newsletter.

RSS feed represents XML file, so I decided to create my own PHP class, which is generating RSS syndicate of my personal blog. Anybody who is interested in my articles can then easily add my channel to his feed reader. You can do it here

So I will start with PHP class and then show you how to use it. So, let's have final class (which means that I am not planning to extend it) with simple object constructor:

// PHP RSS feed class
final class RSS {

  // class constructor
  public function __construct(){
 
  }  
}

So far, so good :-) I will place some variables inside of the class, which I will need:

  /** @var string */
  static public $title = string;
 
  /** @var string */
  static public $link = string;
 
  /** @var string */
  static public $copyright = string;
 
  /** @var string */
  static public $description = string;
 
  /** @var string */
  static public $category = string;
 
  /** @var string */
  static public $language = string;
 
  /** @var array */
  protected $items = array();
 
  /** @var string */	
  private $render = string;

"Public" means that I can change the value of the variable from outside of the class. "Private" means that I can not access and inherit the value of this variable and "protected" means that I can not access but I can inherit the value of the variable. More about inheritance.

Then I will need some public method, which create and render XML sheet of RSS channel.

/**
 *  @return xml sheet	 	 
 */
 
public function render() 
{
	$this->render = '<?xml version="1.0" encoding="utf-8"?&>'
	  .'<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'
	    .'<channel>'
	      .'<title>'.self::encode(self::$title).'</title>'
	      .'<link>'.self::$link.'</link>'	  
                .'<copyright>'.self::encode(self::$copyright).'</copyright>'
                .'<description>'.self::encode(self::$description).'</description>'
                .'<category>'.self::encode(self::$category).'</category>'
                .'<language>'.self::$language.'</language>';  
 
  $this->render .= $this -> render_items();
 
  $this->render .= '</channel>'
                .'</rss>';
 
  return $this->render;
}

As we can see, method render is calling object method render_items (that's why there is keyword "this"). Method render_items will return XML sheet with all items (articles in my case):

/**
 *  @return xml sheet with items	 	 
 */
 
public function render_items() 
{		
  foreach($this->items as $item) 
  {
    $this->render .= 
                    '<item>'

		      .'<title>'.self::encode($item['title']).'</title>'
		      .'<link>'.$item['link'].'</link>'

		      .'<description>'.$item['description'].'</description>'
		    .'</item>';	
  }
  return $this->render;
}

Then I will need some public method, through which I can add items into object's variable and method render_items is then rendering items from this array.

/**
 *  @return void	 	 
 */  
 
public function add_item(array $properties) 
{
  $this->items[] = &$properties;
}

And then private method which strips unprintable characters without changing any whitespace and convert special characters to HTML entities to prevent against XSS (cross-site-scripting). But in this case it's not necessary because channel source is loaded from database table filled by me. But if you would like to create channel from discuss forum, then it is good way how to prevent against XSS attact.

/**
 *  @param string
 *  @return string	 	 	 
 */
 
private static function encode($string) 
{
  return htmlspecialchars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#', '', $string), ENT_QUOTES);
}

So, well done and now I move from the class's code to the explanation of how to use this class.

First I need to create the object (instance of the class):

// object (instance of the class)
$rss = new RSS();

Then I will set up header of the XML channel. That's why I made variables of the class public and static so I can access them using :: (Scope Resolution Operator)

// set up XML header
RSS::$title = 'dubak.sk';
RSS::$link = 'http://www.dubak.sk/blog';
RSS::$copyright = 'info@dubak.sk';
RSS::$description = 'blog about internet applications programing, tips and tricks, LAMP technology, open source';
RSS::$category = 'blog';
RSS::$language = 'en';

Then I need to get all articles from database and assign their properties into array. To do so, I use simple foreach loop. I have to also mention that I have special manager class which is getting articles from database, but it's on you which way you do it.

// empty array for articles
$properties = array();
 
// loop throug all articles
foreach ($articles as $item)
{
  $properties['title'] = $item['title'];        
  $properties['description']= $item['description'];
 
  // will generate link to an article into XML    
  $properties['link']= RSS::$link.$item['id'].'/'.$item['title_url'].'/';
 
  // object's public method will add one article record into RSS channel
  $rss->add_item($properties); 
}

And at the end I just render the whole XML sheet ( which represents RSS feed channel ) with object's public method render.

// method render XML sheet
echo $rss->render();

And that's it! Download source code of the RSS PHP class. Please keep credits inside of the code intact.

Published:

Add / read comments

FIND ME

Share, follow or connect with me.