Version: @(#) $Id: rsswriterclass.class,v 1.7 2002/10/17 05:49:40 mlemos Exp $
RSS writer
Manuel Lemos (mlemos@acm.org)
Copyright © (C) Manuel Lemos 2002
@(#) $Id: rsswriterclass.class,v 1.7 2002/10/17 05:49:40 mlemos Exp $
Version: @(#) $Id: xmlwriterclass.class,v 1.32 2002/10/17 05:38:11 mlemos Exp $
Composing and generating Rich Site Summary (RSS) XML documents.
The class extends the XML writer class to provide additional functions and variables that simplify the composition and generation of RSS documents.
Use the functions addchannel and additem to define the RSS document content and then use the writerss to generate the document output.
Here follows an example of typical use of this class:
<?php /* * First create an object of the class. */ $rss_writer_object=new rss_writer_class; /* * Choose the version of specification that the generated RSS document should conform. */ $rss_writer_object->specification="1.0"; /* * Specify the URL where the RSS document will be made available. */ $rss_writer_object->about="http://www.phpclasses.org/channels.xml"; /* * Specify the URL of an optional XSL stylesheet. * This lets the document be rendered automatically in XML capable browsers * such as Internet Explore 5, Mozilla 5/Netscape 6 or better. */ $rss_writer_object->stylesheet="http://www.phpclasses.org/rss2html.xsl"; /* * When generating RSS version 1.0, you may declare additional * namespaces that enable the use of more property tags defined * by extension modules of the RSS specification. */ $rss_writer_object->rssnamespaces["dc"]="http://purl.org/dc/elements/1.1/"; /* * Define the properties of the channel. */ $properties=array(); $properties["description"]="Repository of components and other resources for PHP developers"; $properties["link"]="http://www.phpclasses.org/"; $properties["title"]="PHP Classes repository"; $properties["dc:date"]="2002-05-06T00:00:00Z"; $rss_writer_object->addchannel($properties); /* * If your channel has a logo, before adding any channel items, specify the logo details this way. */ $properties=array(); $properties["url"]="http://www.phpclasses.org/graphics/logo.gif"; $properties["link"]="http://www.phpclasses.org/"; $properties["title"]="PHP Classes repository logo"; $properties["description"]="Repository of components and other resources for PHP developers"; $rss_writer_object->addimage($properties); /* * Then add your channel items one by one. */ $properties=array(); $properties["description"]="Latest components made available"; $properties["link"]="http://www.phpclasses.org/browse.html/latest/latest.xml"; $properties["title"]="Latest class entries"; $properties["dc:date"]="2002-05-06T00:00:00Z"; $rss_writer_object->additem($properties); $properties["description"]="Latest book reviews released"; $properties["link"]="http://www.phpclasses.org/products.html/latest/latest.xml"; $properties["title"]="Latest reviews"; $properties["dc:date"]="2002-05-06T00:00:00Z"; $rss_writer_object->additem($properties); /* * If your channel has a search page, after adding the channel items, specify a search form details this way. */ $properties=array(); /* * The name property if the name of the text input form field on which the user will enter the search word. */ $properties["name"]="words"; $properties["link"]="http://www.phpclasses.org/search.html?go_search=1"; $properties["title"]="Search for:"; $properties["description"]="Search in the PHP Classes repository"; $rss_writer_object->addtextinput($properties); /* * When you are done with the definition of the channel items, generate RSS document. */ if($rss_writer_object->writerss($output)) { /* * If the document was generated successfully, you may not output it. */ Header("Content-Type: text/xml; charset=\"".$rss_writer_object->outputencoding."\""); Header("Content-Length: ".strval(strlen($output))); echo $output; } else { /* * If there was an error, output it as well. */ Header("Content-Type: text/plain"); echo ("Error: ".$rss_writer_object->error); } ?>
string
"1.0"
Define the version of the specification that the generated RSS document should conform.
Currently the class supports only the versions 0.9, 0.91 and 1.0 but in the future it will also support version 0.92.
string
""
Specify a unique URL where the RSS will be made available.
Define this variable before adding the channel definition if the specification is "1.0".
array
array()
Specify extra namespaces that provide the definition of additional tags to be allowed within channel, items, image and textinput tags.
If you need to use extra tags defined in modules for RSS 1.0, specify them by adding entries to this associative array. The entry indexes are the namespace prefix and the entry values are the respective URL where such XML namespaces are defined. The namespace rdf is already defined by default, so you may not redeclare it.
Set additional namespaces modules you need. In this case it is adding the namespace dc that defines tags such as <dc:date> and <dc:source> for specifying respectively the date and the source of the channel items.
<?php $rss_writer_object->rssnamespaces["dc"]="http://purl.org/dc/elements/1.1/"; ?>
bool
0
Option to specify whether the channel may have no items.
If for some reason you need to generate a channel that may have no items, set this variable to 1.
string
"Generated by: http://www.phpclasses.org/rsswriter \$Revision: 1.7 \$"
Define a text string that is used as a XML comment to identify the program that generated the XML document.
Set this variable to the name of the program that uses this class to generate XML documents. Set to an empty string to avoid generating this comment.
"Generated by: my.e-commerce.com"
bool addchannel(
Add the definition of the RSS channel.
Pass an associative array of simple properties that define the channel. Compound properties such as channel items should be defined with other more specific functions.
properties - Associative array that defines the list of properties of the channel. The indexes of the array entries are the names of the properties and the values array entries are the property text string values. Required properties are: "description", "language", "link", "title". Please check the RSS specification documents to learn about the optional properties.
Success boolean flag. If this flag is 0 then the error variable contains the error message that explains the failure. This return value may be safely ignored if the function call arguments are correctly defined.
bool additem(
Add the definition of the RSS channel item.
Pass an associative array of simple properties that define the channel item.
properties - Associative array that defines the list of properties of the channel item. The indexes of the array entries are the names of the properties and the values array entries are the property text string values. Required properties are: "link", "title". The "description" property is optional.
Success boolean flag. If this flag is 0 then the error variable contains the error message that explains the failure. This return value may be safely ignored if the function call arguments are correctly defined.
bool addimage(
Add the definition of an image to be associated with the RSS channel.
Pass an associative array of simple properties that define the channel image. This function can only be used before defining any channel items using additem.
properties - Associative array that defines the list of properties of the channel item. The indexes of the array entries are the names of the properties and the values array entries are the property text string values. Required properties are: "url", "link" and "title". The properties "description", "width" and "height" are optional.
Success boolean flag. If this flag is 0 then the error variable contains the error message that explains the failure. This return value may be safely ignored if the function call arguments are correctly defined.
bool addtextinput(
Add the definition text input for searching the RSS channel.
Pass an associative array of simple properties that define the channel text input.
properties - Associative array that defines the list of properties of the channel item. The indexes of the array entries are the names of the properties and the values array entries are the property text string values. Required properties are: "description", "link", "name" and "title".
Success boolean flag. If this flag is 0 then the error variable contains the error message that explains the failure. This return value may be safely ignored if the function call arguments are correctly defined.
bool writerss(
Generate the RSS XML document.
This function calls the write to generate the RSS XML document. However, you should call this function instead because it will set a few document parameters that are required to generate RSS XML documents properly. Pass a reference to a string variable that will hold the resulting XML document.
output - Reference to a variable that will hold the resulting RSS XML document.
Success boolean flag. If this flag is 0 then the error variable contains the error message that explains the failure.