Useful Information Company

RSSWriter Documentation

Introduction

RSSWriter is a convenience class for generating RSS 1.0 feeds with PHP. It supports the core RSS vocabulary and allows you to use modular extensions with little fuss.

To get started, download rss10.inc. The example created in this tutorial is also available: example.php.

Using RSSWriter

Put rss10.inc somewhere your PHP interpreter can find it. Then include it:

<?php
include("rss10.inc");

OK, let's imagine your web site is http://www.example.org/.

Then create your RSS object using the URL, title and description.

$rss=new RSSWriter("http://www.example.org/",
"Example Site", "The best examples out there.");

If you have any additional information, such as Dublin Core metadata, you can put it in an array as the fourth parameter. Like this:

$rss=new RSSWriter("http://www.example.org/",
"Example Site", "The best examples out there.",
array("dc:publisher" => "Example Publishing Inc.",
"dc:creator" => "E X Ample <me@example.org>"));

Setting an image

Most sites like to have an image icon. Typically this is an 88x31 GIF image. Add an image like this:

$rss->setImage("http://www.example.org/images/rss.gif",
"Example Site: All the Examples Fit to Print");

The defaults are to assume 88 high and 31 wide for the image. If you want to overload these, specify them as the third and fourth parameters. For example, for an image 100 wide by 50 high:

$rss->setImage("http://www.example.org/images/rss.gif",
"Example Site: All the Examples Fit to Print", 100, 50);

Adding the items

Now you need to add an entry for every page you want to feature. We'll just do two:

$rss->addItem("http://www.example.org/page1.html",
"First Example Page");

This is the minimum you need to add, the URL and the title. However, most people add description sentences too, and might also want to add other data from RSS 1.0 modules, such as Dublin Core. Note Dublin Core comes pre-defined with the prefix dc -- if you want to use any other module you must register it first. See below, "Advanced usage".

$rss->addItem("http://www.example.org/page2.html",
"Second Example Page",
array("description" => "This is the second page of examples on this wonderful site",
"dc:subject" => "examples",
"dc:creator" => "Fred <fred@example.org>"));

RSSWriter strips out any HTML markup you put in the title or description. All other fields are left untouched.

Advanced usage

It may be that you want to include items from a namespace other than the normal RSS 1.0 namespace. For convenience Dublin Core comes pre-registered. Add your own module namespace like this by specifying the prefix and URI:

$rss->useModule("ex", "http://www.example.org/ns/myModule/");

Writing out your RSS file

Now all the data's in, all you need do is call the serialize() method and finish your file:

$rss->serialize();
?>

Be careful not to have any whitespace in your document outside of the <?php ... ?> tags.

And the final result...

<?xml version="1.0" encoding="iso-8859-1"?>
<rdf:RDF 
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns="http://purl.org/rss/1.0/"
         xmlns:mn="http://usefulinc.com/rss/manifest/"
         xmlns:dc="http://purl.org/dc/elements/1.1/"
 >

  <channel rdf:about="http://www.example.org/rss10.php">
    <title>Example Site</title>
    <link>http://www.example.org/</link>
    <description>The best examples out there.</description>
    <dc:publisher>Example Publishing Inc.</dc:publisher>
    <dc:creator>E X Ample <me@example.org></dc:creator>
    <image rdf:resource="http://www.example.org/images/rss.gif" />
    <items>
      <rdf:Seq>
        <rdf:li rdf:resource="http://www.example.org/page1.html" />
        <rdf:li rdf:resource="http://www.example.org/page2.html" />
      </rdf:Seq>
    </items>
  </channel>

  <image rdf:about="http://www.example.org/images/rss.gif">
     <title>Example Site: All the Examples Fit to Print</title>
     <url>http://www.example.org/images/rss.gif</url>
     <link>http://www.example.org/</link>
     <dc:description>The best examples out there.</dc:description>
  </image>

  <item rdf:about="http://www.example.org/page1.html">
    <link>http://www.example.org/page1.html</link>
    <title>First Example Page</title>
  </item>

  <item rdf:about="http://www.example.org/page2.html">
    <link>http://www.example.org/page2.html</link>
    <title>Second Example Page</title>
    <description>This is the second page of examples on 
       this wonderful site</description>
    <dc:subject>examples</dc:subject>
    <dc:creator>Fred &lt;fred@example.org&gt;</dc:creator>
  </item>

  <rdf:Description rdf:ID="manifest">
    <mn:channels>
      <rdf:Seq>
        <rdf:li rdf:resource="http://www.example.com/rss10.php" />
      </rdf:Seq>
    </mn:channels>
  </rdf:Description>

</rdf:RDF>