Introduction

This class uses DOMDocument to generate an RSS feed. It really only covers the basic RSS 2.0 specification and does not get into some of the fancier namespace extensions to RSS 2.0.

Dependencies

Requires PHP 5.x with libxml2 for DOMDocument.

Class Source

Using the Class

<?php
require_once('xml_rssClass.inc');
$rss = new DOMDocument('1.0','UTF-8');
$rss->preserveWhiteSpace = false;
$rss->formatOutput = true;
$myRss = new DOMrssFeed($rss);
?>

In practice, rather than calling the DOMrssFeed class directly, you probably want to define a class that extends DOMrssFeed and sets the initial public variables. You can also set the public variables after initiating the class.

Public Variables

Required:

public $title
String, the title of your channel.
public $link
URL, link to web site corresponding to your channel.
public $desc
String, a textual description of your channel.

Optional:

public $atomLink
URL, the link to your RSS. If you define it, the class will properly create a atom:link element that is useful to some aggregators.
public $stylesheet
URL, the URL of a style sheet associated with your RSS feed.
public $language
String, the language of your RSS feed. Defaults to en-us.
public $copyright
String, a copyright notice for the feed.
public $managingEditor
String, the managing editor for the feed. Usually of the form someone@example.com (Someone Important)
public $webMaster
String, the web master responsible for the feed. Usually of the form webmaster@example.com (Someone Under-appreciated)
public $category
String, Category the RSS feed belongs to.
public $generator
String. Specifies the software used to generate the feed. If left blank, it defaults to specifying PHP and DOMDocument. If you are using this class as, say, part of some BLOG software you are developing, this is where you want to define the name of your software.
public $ttl
Integer, how many minutes the channel can be cached.
public $skipHours
Array of Integers, tells aggregators what hours they can skip.
public $skipDays
Array of full textual representation of days, tells aggregators what days they can skip.
public $debug
Boolean. Set to true, and the feed will be sent with the incorrect text/plain mime type making it a bit easier to visually inspect the resulting XML.

Once you have set up the public variables you want either by using a class that extends this class or manually defining them after initiating this class you need to create the channel node:

<?php
$myRss->makeChannel();
?>

The function takes two optional arguments. The first defines the pubDate and the second defines the lastBuildDate. Both are option, and expect the arguments to be in seconds from Unix Epoch (standard Unix Time.) The class converts Unix Time to the RFC 822 format used by RSS 2.0.

The channel node will now be set up according to your defined public variables. There are however still some optional functions you may want to use before you start adding items to the channel:

Channel Functions

addImage()
Adds an image to your RSS feed. If you use it, only use it once. Takes 6 arguments, 1 required.
-- $url
First Argument. URL. Required. Specifies the URL of the image.
-- $title
Second Argument. String. Optional. Used in the alt tag when image is rendered in HTML. If not defined, the public title variable will be used instead.
-- $link
Third Argument. URL. Optional. Used to create an image link when rendered as HTML pointing to URL of the site the feed is associated with. If not defined, the public link variable will be used instead.
-- $width
Fourth Argument. Integer. Optional. Used to specify the width of the image. Should not be larger than 144.
-- $height
Fifth Argument. Integer. Optional. Used to specify the height of the image. Should not be larger than 400.
-- $description
Sixth Argument. String. Optional. Used to define the title attribute of the image when rendered as HTML.

Please note that I wrote above function for completeness. I have not used it myself.

cloud()
Specifies a web service that supports the rssCloud interface. 5 arguments, all required.
-- $domain
First Argument. String. Specifies the domain of the cloud.
-- $port
Second Argument. Integer. Specifies the port to use.
-- $path
Third Argument. Path. Specifies the path to use.
-- $registerProcedure
Fourth Argument. String. Specifies the registered procedure.
-- $protocol
Fifth Argument. String. Specifies the protocol. Should be http-post, xml-rpc, or soap 1.1

Please note that I wrote above function for completeness. I have not used it myself.

textInput()
Used to create a text input. Takes four arguments. Three are required, the fourth has a reasonable default.
-- $name
First Argument. String. Name of the text object in the text input area.
-- $description
Second Argument. String. Describes the text input area.
-- $cgi
Third Argument. URL. Link to the script that should receive the data when the data is submitted.
-- $submit
Fourth Argument. String. The text for the Submit button. Defaults to Submit.

Please note that I wrote above function for completeness. I have not used it myself.

Adding Items

Now you are ready to add items to your feed. You can use the newItem() function as many times as you need.

newItem()
Creates a new item. Takes four arguments, all are optional but if you do not use the first then you must use the second.
-- $title
First Argument. String. The title of the item.
-- $desc
Second Argument. String. Description of the item. It is OK to have raw HTML in the string, DOMDocment will sanitize it for you. The most I ever use the string is <br /> but I do not think there are any hard rules.
-- $link
Third Argument. URL. Link to what the item is about, where to get more info.
-- $pubdate
Fourth Argument. UNIX Time. When the item was last modified. The class converts UNIX time to RFC 822 for you.

When a new item has been created, there are a few functions that add to the new item you may want to use:

addCategory()
Creates category information for the item. Takes two arguments, only first is required.
-- $cat
First Argument. String. Required. The category of the item.
-- $domain
Second Argument. String. Optional. Identifies a hierarchic location in the indicated taxonomy. I am not personally sure how that is useful for RSS feeds but the class supports it if you do.
addMedia()
Adds a media component to an item. Three arguments, all required.
-- $url
First Argument. URL. Required. The URL resource of the media.
-- $size
Second Argument. Integer. Required. The size in bytes of the media.
-- $mime
Third Argument. String. Required. The mime type of the media.

Please note that I wrote above function for completeness. I have not used it myself.

addGuid()
Adds a unique identified to the item. Usually an item specific hyperlink but not always. Takes two arguments, the first is required. The second has a reasonable default.
-- $guid
First Argument. String. Required. A string that uniquely identifies the item.
-- $permalink
Second Argument. Boolean. Optional. If set to true, the aggregator assumes the GUID is a permanent link to the item. Defaults to true.
addExtraData()
Adds a couple of other misc nodes. Both arguments are optional and independent of each other.
-- $author
First Argument. String. The author of the item. You only really need to use this if you have items authored by more than one person.
-- $comments
Second Argument. URL. If defined, it points to a page where comments about the item may be made.
itemSource()
Used to specify the source of the item. Two arguments, both required.
-- $source
First Argument. String. A textual description of the source.
-- $url
Second Argument. URL. The URL of the source.

Please note that I wrote above function for completeness. I have not used it myself.

addCdata()
Adds a CDATA section to the item. One argument, a string containing the contents of the CDATA section. I am not really sure what benefit a CDATA section in an RSS item is, but some people seem to use it, so it is here.

You can add as many items as you like. Once you create a new item, it appends any previous items to the DOM and the item specific functions you want to use need to be used again on the new item. When you use the function to serve the RSS, the last item is appended to the DOM first.

Serving

<?php
$myRss->sendRss();
?>

That is it! The class will send the appropriate header followed by the content. For an example standards compliant RSS feed served with this class:
http://www.shastaherps.org/ProductReviews.rss

More Information

For more information on RSS, you can start with the following web sites:

[W3C Valid]