RSS Feed Class

This class makes it easy to create an RSS Feed. I followed The Official RSS 2.0 Specifications at Harvard Law to a tee when creating this class, and the following websites were also helpful:

You don't have to worry about formatting any of your dates as we do all of that in-house. Any date that can be translated by PHP's strtotime() function will do just fine. After you're done, Validate Your RSS Feed to make sure you got things right. Finally, place the following at the top of every page that corresponds to your RSS Feed so that browsers will recognize it:

$page->link('<link rel="alternate" type="application/rss+xml" href="http://www.your-website.com/rss.xml" title="Your title" />');

object RSS ( string $title, string $link, string $description )

This is the constructor for the class, and it takes in the three required channel elements to get your RSS feed on it's feet.

$title The name of your RSS feed.
$link The website link that corresponds to the feed.
$description A short description of your feed.
Returns An RSS object.
Example
$rss = new RSS ('My RSS Feed', 'http://www.website.com/', 'The latest news from my website.');

channel ( array $elements )

This method allows you to insert all of the other optional channel elements into your feed. See the examples below. You don't have to keep calling this method for every element, you can just throw everything into an array and call the method only once.

$elements Key and value pairs of channel elements, and their values.
Example
$rss->channel(array('atom:link'=>array('href'=>'http://www.website.com/rss.xml', 'rel'=>'self', 'type'=>'application/rss+xml'), 
                    'language'=>'en-us', 
                    'pubDate'=>'2010-10-28 09:52:33', 
                    'category'=>'news', 
                    'generator'=>'The awesome RSS Class at PHP-Ease.com!' 
                     ));

atom:link

According to the RSS Advisory Boards recommended best practices, this atom:link should be included in your feed, and it should point to the RSS Feed URL itself.

Example
$rss->channel(array('atom:link'=>array('href'=>'http://www.website.com/rss.xml', 'rel'=>'self', 'type'=>'application/rss+xml')));

language

The language the channel is written in. See the Allowable values for language in RSS.

Example
$rss->channel(array('language'=>'en-us'));

copyright

A copyright notice for the channel.

Example
$rss->channel(array('copyright'=>'Copyright 2010 Website'));

managingEditor

The email address of who is responsible for the content.

Example
$rss->channel(array('managingEditor'=>'person@website.com'));

webMaster

The email address of who is responsible for any techincal issues relating to the feed.

Example
$rss->channel(array('webMaster'=>'webmaster@website.com'));

pubDate

The channel's publication date.

Example
$rss->channel(array('pubDate'=>'today'));

lastBuildDate

The last time the content in the feed changed.

Example
$rss->channel(array('lastBuildDate'=>'yesterday'));

category

A general category that the feed would fall under.

Example
$rss->channel(array('category'=>'news'));

generator

Any software kudos you would like to give.

Example
$rss->channel(array('generator'=>'PHP-Ease.com RSS Class'));

docs

A URL that points to the documentation for the format used in the RSS file.

Example
$rss->channel(array('docs'=>'http://cyber.law.harvard.edu/rss/rss.html'));

cloud

To specify a web service that supports the rssCloud interface. See SOAP meets RSS.

Example
$rss->channel(array('cloud'=>array('domain'=>'rpc.sys.com', 
                                   'port'=>80, 
                                   'path'=>'/RPC2', 
                                   'registerProcedure'=>'myCloud.rssPleaseNotify', 
                                   'protocol'=>'xml-rpc' 
                                   )));

ttl

"Time To Live" - How long (in seconds) a channel should be cached before the source is refreshed.

Example
$rss->channel(array('ttl'=>60));

image

A jpg, gif, or png image to display with the channel. The 'url', 'title', and 'link' values are required, the rest are optional. 'title' and 'link' should have the same values as the channel's title and link.

Example
$rss->channel(array('image'=>array('url'=>'http://www.website.com/image.jpg', 
                                   'title'=>'My RSS Feed', 
                                   'link'=>'http://www.website.com/', 
                                   'width'=>'144', // (max - default is 88) 
                                   'height'=>'400', // (max - default is 31) 
                                   'description'=>'a few thoughts...' 
                                   )));

rating

The PICS (an outdated protocol to describe web content) rating for the channel.

Example
$rss->channel(array('rating'=>'(PICS-1.1 "http://www.classify.org/safesurf/" l r (SS~~000 1))'));

textInput

A text input box. Most aggregators ignore it. The purpose for this element is unclear.

Example
$rss->channel(array('textInput'=>array('title'=>'Search', 
                                       'description'=>'Search Google', 
                                       'link'=>'http://www.google.no/search?', 
                                       'name'=>'q' 
                                       )));

skipHours

An array of hours (between 0 and 23) when you don't want aggregators to read your channel.

Example
$rss->channel(array('skipHours'=>array(16, 17, 18)));

skipDays

An array of days ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday') when you don't want aggregators to read your channel.

Example
$rss->channel(array('skipDays'=>array('Saturday', 'Sunday')));

item ( string $title [, array $elements ] )

This method creates an item in your feed. The only required element is either the 'title', or a 'description'. We settled on requiring a title.

$title The title of the item.
$elements Key and value pairs of item elements, and their values. See below for examples.
Example
$rss->item('Item Title', 
           array('link'=>'http://www.website.com/link.html', 
                 'description'=>'A description of the item.', 
                 'pubDate'=>'2004-08-15 16:23:42'));

link

The url link of the item.

Example
$elements['link'] = 'http://www.website.com/link.html';

description

A description of the item. To include html in your description then wrap it in CDATA tags like so:
<![CDATA[An <b>HTML</b> description.]]>

Example
$elements['description'] = 'A description of the item.';

author

The author's email address.

Example
$elements['author'] = 'name@website.com';

category

A category that the item belongs to.

Example
$elements['category'] = 'news';

comments

A url address where comments for the item are located.

Example
$elements['comments'] = 'http://www.website.com/item.php#comments';

enclosure

A media object attached to the item. All three attributes ('url, 'length', and 'type') are required.

Example
$elements['enclosure'] = array('url'=>'http://www.website.com/audio.mp3', 
                               'length'=>'12216320', 
                               'type'=>'audio/mpeg');

guid

A "globally unique identifier" for the item. It can be a string, url, number . . . anything as long as it remains unique. If you would like to include the 'isPermaLink' attribute, just make it an array.

Example
$elements['guid'] = 'http://www.website.com/item.html'; // or . . . 
$elements['guid'] = array('isPermaLink'=>'true', 'value'=>'http://www.website.com/item.html'); // meaning this is a url that can be opened in a browser

pubDate

When the item was published.

Example
$elements['pubDate'] = '2004-08-15 16:23:42';

source

The name (title) of the RSS channel that the item came from. It has a required 'url' attribute, so define it like so:

Example
$elements['source'] = array('url'=>'http://www.otherguys.com/rss.xml', 'value'=>"Other Guy's RSS Feed");

display ( )

This method puts the whole RSS Feed together, sets the appropriate header, echo's the xml, then exits the script.

Example
$rss->display();
Click to Download the PHP RSS Feed Class

 Subscribe to our feed

RSS.php

<?php 
 
/* 
 *    author:           Kyle Gadd 
 *    documentation:    http://www.php-ease.com/classes/rss-feed.html 
 * 
 *    This program is free software: you can redistribute it and/or modify 
 *    it under the terms of the GNU General Public License as published by 
 *    the Free Software Foundation, either version 3 of the License, or 
 *    (at your option) any later version. 
 * 
 *    This program is distributed in the hope that it will be useful, 
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *    GNU General Public License for more details. 
 * 
 *    You should have received a copy of the GNU General Public License 
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */ 
 
class RSS { 
 
  public $encoding = 'utf-8'; 
  private $channel = array(); 
  private $items = array(); 
 
  public function __construct ($title, $link, $description) { 
    $this->channel['title'] = $title; 
    $this->channel['link'] = $link; 
    $this->channel['description'] = $description; 
  } 
   
  public function channel ($elements) { 
    foreach ($elements as $key => $value) $this->channel[$key] = $value; 
  } 
   
  public function item ($title, $elements) { 
    $item = array(); 
    $item['title'] = $title; 
    foreach ($elements as $key => $value) $item[$key] = $value; 
    $this->items[] = $item; 
  } 
   
  public function display () { 
    $xml = '<?xml version="1.0" encoding="' . $this->encoding . '"?>' . "\n"; 
    $xml .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">' . "\n"; 
    $xml .= '<channel>' . "\n"; 
    foreach ($this->channel as $key => $value) $xml .= '  ' . $this->tag($key, $value) . "\n"; 
    foreach ($this->items as $item) { 
      $xml .= "  <item>\n"; 
      foreach ($item as $key => $value) $xml .= '    ' . $this->tag($key, $value) . "\n"; 
      $xml .= "  </item>\n"; 
    } 
    $xml .= '</channel>' . "\n"; 
    $xml .= '</rss>'; 
    header("Content-Type: application/rss+xml"); 
    echo $xml; 
    exit; 
  } 
   
  private function tag ($key, $values) { 
    $tag = ''; 
    list($value, $attributes) = $this->values($values); 
    if (in_array($key, array('pubDate', 'lastBuildDate'))) $value = $this->date($value); 
    if ($key == 'image') { 
      $tag .= '<' . $key . '>'; 
        $tag .= '<url>' . $values['url'] . '</url>'; 
        $tag .= '<title>' . $values['title'] . '</title>'; 
        $tag .= '<link>' . $values['link'] . '</link>'; 
        if (isset($values['width'])) $tag .= '<width>' . $values['width'] . '</width>'; 
        if (isset($values['height'])) $tag .= '<height>' . $values['height'] . '</height>'; 
        if (isset($values['description'])) $tag .= '<description>' . $values['description'] . '</description>'; 
      $tag .= '</' . $key . '>'; 
    } elseif ($key == 'textInput') { 
      $tag .= '<' . $key . '>'; 
        $tag .= '<title>' . $values['title'] . '</title>'; 
        $tag .= '<description>' . $values['description'] . '</description>'; 
        $tag .= '<name>' . $values['name'] . '</name>'; 
        $tag .= '<link>' . $values['link'] . '</link>'; 
      $tag .= '</' . $key . '>'; 
    } elseif ($key == 'skipHours') { 
      $tab .= '<' . $key . '>'; 
      if (!is_array($values)) $values = array($values); 
      foreach ($values as $hour) $tab .= '<hour>' . $hour . '</hour>'; 
      $tab .= '</' . $key . '>'; 
    } elseif ($key == 'skipDays') { 
      $tab .= '<' . $key . '>'; 
      if (!is_array($values)) $values = array($values); 
      foreach ($values as $day) $tab .= '<day>' . $day . '</day>'; 
      $tab .= '</' . $key . '>'; 
    } else { 
      if (!empty($value)) { 
        $tag .= '<' . $key . $attributes . '>' . $value . '</' . $key . '>'; 
      } else { 
        $tag .= '<' . $key . $attributes . ' />'; 
      } 
    } 
    return $tag; 
  } 
   
  private function values ($array) { 
    if (!is_array($array)) return array($array, ''); 
    $value = (isset($array['value'])) ? $array['value'] : ''; 
    unset ($array['value']); 
    $attributes = ''; 
    foreach ($array as $k => $v) { 
      $attributes .= " {$k}=\"" . addslashes($v) . '"'; 
    } 
    return array($value, $attributes); 
  } 
   
  private function date ($date) { 
    return date(DATE_RFC2822, strtotime($date)); 
  } 
 
} 
 
?>

comments powered by Disqus
Copyright © 2011 PHP-Ease.com - PHP Made Easy