Php FeedWriter (v3.0 beta) now provides the ability to output to various feed formats. Php FeedWriter can currently generate Atom 1.0, RSS 2.0, RSS 1.0, RSS 0.92 and RSS 0.91 compatible feeds.
View more information about Php FeedWriter, including online documentation. For information about the latest update and release, see the article Php FeedWriter 3.0 (beta) Released.
A tutorial demonstrating how to build a feed using Php FeedWriter is also available on this blog:
How to Generate Feed Using Php (Atom 1.0 and RSS 2.0 Compatible)
[Edit: 9th January 2010]
This Class has now been updated to Version 2. The information provided in this post is now out of date. Please see the Php RSS2Writer Class page for information about the updated version (Version 2.0).
Updated Version (2.0): Php RSS 2 Writer - Download and Usage Instructions
----------------------------------------------------
The following class provides basic functionality required to dynamically generate an RSS 2.0 feed using Php.
This script may be used or developed at your own risk as long as it is referenced appropriately. Please post any comments or suggestions for improvement to this article.
The main RSS 2 Writer class is the RSS2Writer class in RSS2Writer.php, which contains functions that required to generate RSS 2.0 feeds. Comments throughout the code provide information about using the class and functions.
The sampleController.php file provides detailed information as well as examples of how to use the class. Any functionality requirements are noted in the sample controller.
The Php scripts are available for download here:
Free Download: Php RSS2 Writer
Simple RSS2Writer (v1.0) and sample controller
(c) Copyright Daniel Soutter
Written on the 13th April 2009
Blog: http://dsoutter.blogspot.com/
This script may be used or developed at your own risk as long as it is
referenced appropriately.
Please post any comments or suggestions for improvement to
my blog (above).
References: http://www.phpbuilder.com/board/showthread.php?t=10356853
http://forum.tvphp.net/archive/index.php/t-8305.html
http://www.w3schools.com/Schema/schema_example.asp
http://cyber.law.harvard.edu/rss/rss.html
I am currently working on a generic RSSWriter that generates RSS 1.0, 2.0
and ATOM feeds. I propose to use this class to generate feeds of dynamic
website content.
Contents of RSS2Writer.php
class RSS2Writer
{
//Declare variables
var $xml; // Used to store the rss xml
var $itemOpen = false; // Boolean value to determine if an item element is currently open
/*********************************************************************************
* Class Constructor
*
* Description:Creates an instance of the XMLWriter and starts an xml 1.0
* document. Starts required RSS 2.0 elements (rss, channel)
*
* Paramaters: String $title - Channel title
* String $description - Channel description
* String $link - link to channel (unique)
* Int $indent - Xml indent level
*
* Returns: Void
**********************************************************************************/
function __construct($title, $description, $link, $indent = 4)
{
//Set the default timezone}
@date_default_timezone_set("GMT");
//Create the xml write object
$writer = new XMLWriter();
//XMLWriter Output method:
//--------------------------------------------------------------------------------------
$writer->openMemory(); // Xml stored in memory (store in variable, output
// to file, print/echo to user, etc.
//$this->$writer->openURI('php://output'); // Send xml to browser/user
//-----------------------------------------------------------------------------------------
//XML Version
$writer->startDocument('1.0');
//Indent level
$writer->setIndent($indent);
//Create first element / main block (Xml type - RSS 2.0)
$writer->startElement('rss');
//StartRSS--------------------------------------------------------------------------------
//*****************************************************************************************
//RSS attribute(s)
$writer->writeAttribute('version', '2.0');
$writer->startElement("channel");
//Start Channel------------------------------------------------------------------------
//Required Channel Elements
//---------------------------------------------------------
$writer->writeElement('title', $title);
$writer->writeElement('description', $description);
$writer->writeElement('link', $link);
$this->xml = $writer->outputMemory(true);
/*********************************************************************************
* function addElement (Optional)
*
* Description: Generic function to add any channel or item element
*
* Paramaters: String $elementName - Name of the element to add
* String $val - Value of the element
*
* Returns: Void
**********************************************************************************/
function addElement($elementName, $val){
$writer = new XMLWriter();}
$writer->openMemory();
$writer->setIndent(4);
$writer->writeElement($elementName, $val);
$this->xml .= $writer->outputMemory(true);
/*********************************************************************************
* function channelImage (Optional)
*
* Description: Add an image to the channel.
* Note: Must be called before closing the channel and document
*
* Paramaters: String $title - Channel title
* String $link - link to send to if clicked
* String $url - url to image
* String $width - image width
* String $height - image height
*
* Returns: Void
**********************************************************************************/
function channelImage($title, $link, $url, $width, $height) //All Strings
{
//Channel Image (Optional)}
$writer->startElement('image');
$writer->writeElement('title', $title);
$writer->writeElement('link', $link);
$writer->writeElement('url', $url);
$writer->writeElement('width', $width);
$writer->writeElement('height', $height);
$writer->endElement();
/*********************************************************************************
* function channelCloud (Optional)
*
* Description: Add cloud connectivity settings to the channel
* Note: Must be called before closing the channel and document
*
* Paramaters: String $domain - cloud domain/server
* String $port - cloud port
* String $path - path to feed data
* String $regProcedure
* String $protocol
*
* Returns: Void
**********************************************************************************/
function channelCloud($domain, $port = '80', $path, $regProcedure = 'pingMe', $protocol = 'soap')
{
//Cloud block - Allow registration with a cloud to recieve notification of feed updates}
$writer->startElement('cloud');
$writer->writeAttribute('domain', $domain);
$writer->writeAttribute('port', $port);
$writer->writeAttribute('path', $path);
$writer->writeAttribute('registerProcedure', $regProcedure);
$writer->writeAttribute('protocol', $protocol);
$writer->endElement();
/*********************************************************************************
* function addCategories
*
* Description: Generic function to add categories to the channel and/or items
*
* Paramaters: Array $categories [][] - Multidimensional array of categories
* and optional domain. Array details:
* [x][0] = category name,
* [x][1] = domain (or null)
* Returns: Void
**********************************************************************************/
function addCategories($categories) //
{
//Category block - Can write a single element for 1 category, or a block for multiple:}
foreach ($categories as $cat)
{
$writer->startElement('category');}
if($cat[1] != null) //category has an associated domain
$writer->writeAttribute('domain', $cat[1]);
$writer->text($category);
$writer->endElement();
/*********************************************************************************
* function addItem
*
* Description: Add an item to the feed.
* Note: To add optional elements, true must be passed as the final
* paramater to leave the element open. The closeItem() funtion
* must be called after adding elements to close the item block.
*
* Paramaters: String $title - Channel title
* String $description - Channel description
* String $link - link to channel (unique)
* Boolean $optionalElements - Set to true if optional elements are
* to be added
*
* Returns: Void
**********************************************************************************/
function addItem($title = null, $description = null, $link = null, $optionalElements = false)
{
//Create the xml write object}
$writer = new XMLWriter();
$writer->openMemory();
$writer->setIndent(4);
$writer->startElement("item");
//Start Item-----------------------------------------------------------------------
$writer->writeElement('title', $title);
$writer->writeElement('link', $link);
$writer->writeElement('description', $description);
if (!$optionalElements)
$writer->endElement();
else
$this->itemOpen = true;
//End Item ------------------------------------------------------------------------
$this->xml .= $writer->outputMemory(true);
/*********************************************************************************
* function closeItem
*
* Description: Closes an item element only if the current has been left open.
*
* Paramaters: none
*
* Returns: Void
**********************************************************************************/
function closeItem()
{
if($this->itemOpen)}
{
$this->xml .= '}
';//end item tag with new line
$this->itemOpen = false;
/*********************************************************************************
* private function closeDocument
*
* Description: Closes the Channel and rss elements as well as the document
*
* Paramaters: none
*
* Returns: Void
**********************************************************************************/
private function closeDocument()
{
//Create the xml write object}
$writer = new XMLWriter();
$writer->openMemory();
$writer->setIndent(4);
// Start the xml elements which requiring closing (allow endElement() function to work)
$writer->startElement('rss');
$writer->startElement('channel');
$writer->text('.');
//Flush the current xml to remove start tags, but allow correct elements to be closed.
$writer->flush();
$writer->endElement();
//End channel -------------------------------------------------------------------------
// End rss
$writer->endElement();
//-----------------------------------------------------------------------------------------
//*****************************************************************************************
//End Xml Document
$writer->endDocument();
//$writer->flush();
$this->xml .= $writer->outputMemory(true);
/*********************************************************************************
* function getXML
*
* Description: Returns the rss feed xml
*
* Paramaters: none
*
* Returns: Returns the rss feed xml as a String
**********************************************************************************/
function getXML()
{
$this->closeDocument();}
return $this->xml;
/*********************************************************************************
* function writeToFile
*
* Description: Writes the generated rss xml to a file
*
* Paramaters: String $fileName - Filename to save the rss feed xml
*
* Returns: Void
**********************************************************************************/
function writeToFile($fileName)
{
$this->closeDocument();}
$fh = fopen($fileName, 'w') or die("can't open file");
fwrite($fh, $this->xml);
fclose($fh);
}
?>
Usage Notes:
Optional RSS 2 Channel Elements
To add an additional channel element:
- Call the addElement function, passing the element name and value
as string paramaters, and/or
- Call the channelImage, channelCloud or addCategories functions
-----------------------------------------------------------------------------
language - The language used in the feed
copyright - Copyright notice
managingEditor - Email address for editor of content in the feed
webMaster - Email address for technical issues relating to the feed
pubDate - The latest publish date of the feed (must be > feed pubDate)
date("D, d M Y H:i:s e") - current date formatted appropriately
lastBuildDate - The date content was last updated
generator - The software used to generate the channel (String)
docs - URL to documentation about the rss format used
ttl - Time to live (number of mins to cache before refresh)
rating - PICS rating
textInput - Text input displays with the channel
skipHours - Skip refresh on specific hours
skipDays - Skip refresh on specific days
Optional RSS 2 Item Elements:
Note: All elements of an RSS 2.0 item are optional, however one of the title or
description elements are required. The addItem function of the RSS2Writer class
allows null values for the title, description and link paramaters, but at least one
must contain a value for the RSS 2.0 xml to be valid.
To add an additional element to an item:
- Call the addCategories funcyion to add categories to the item, and/or
1. Pass 'true' as the last paramater when adding an item to leave
the item element open.
2. Call the addElement function, passing the element name and value
as string paramaters
3. IMPORTANT: Close the item element/tag - call the closeItem() function
note: the closeItem() function will only close an item element when
one is open to avoid generating invalid xml.
-----------------------------------------------------------------------------
author - Email address for the author of the article
pubDate - The publish date of the item
date("D, d M Y H:i:s e") - current date formatted appropriately
comments - A url of a page with comments relating to the item
enclosure - Descripbe a media object attached to the item
guid - a unique identifier for the item
source - The rss channel the item came from
-----------------------------------------------------------------------------
Must manualy call closeItem() for an item tag to be
closed after adding additional (optional) elements.
This is not required if $optionalElements is false
Contents of sampleController.php
/*******************************************************************************/
//Include RSS2Writer class file
//--------------------------------
require_once("RSS2Writer.php");
//1. Instantiate new RSS2Writer
//--------------------------------
$rss = new RSS2Writer('Test Title', 'Description goes here', 'http://this.is.the.link');
//2. Add items to the rss feed channel
//--------------------------------
//Example item with NO additional optional elements
$rss->addItem('item 1 title', 'Item 1 description', 'http://link.item.1', null , 'category1', false);
//Example item WITH additional elements (pass true as the last paramater)
$rss->addItem('item 2 title', 'Item 2 description', 'http://link.item.2', null , 'category2', true);
//Add the required elements
$rss->addElement('copyright', '(c) Daniel Soutter 2009');
//IMPORTANT: Close the item element
$rss->closeItem();
//3. Output the results
//--------------------------------
//$rss->writeToFile('rss.xml'); //write the xml output to file
echo $rss->getXML(); //send the xml output to the user/browser (interpreted as an rss feed)
?>
Slightly unrelated, but I've recently started using Exclaimer to manage our exchange email signatures - this includes the ability to add RSS feeds to your outgoing email. So far I have been really impressed with this tool - I highly recommend it.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis Class has now been updated to Version 2. The information provided in this post is now out of date. Please see the Php RSS2Writer Class page for information about the updated version (Version 2.0) of the class, and up-to-date information and usage instructions.
ReplyDeleteUpdated Version (2.0): Php RSS 2 Writer - Download and Usage Instructions