CHANGES: NEWS DOWNLOAD: Stable 2.0 (August 29, 2005): ContentFeeder-2.0.tar.gz ANONYMOUS CVS: (leave password empty) cvs -d :pserver:anonymous@cvs.phpinsider.com:/export/CVS login cvs -d :pserver:anonymous@cvs.phpinsider.com:/export/CVS checkout ContentFeeder NAME: ContentFeeder - A library used for assembling content feeds. RSS 2.0 is currently the only format supported, but can easily be extended to handle more. AUTHOR: Monte Ohrt <monte [AT] ohrt [DOT] com> LATEST VERSION: 2.0 - August 29, 2005 SYNOPSIS: require('ContentFeeder.class.php'); $feed =& new ContentFeeder_RSS2; $feed->setStylesheet('http://www.foobar.com/style.css','css'); $feed->addNamespace('dc', 'http://purl.org/dc/elements/1.1/'); $feed->setElement('title', 'FOOBAR CONTENT FEED'); $feed->setElement('link', 'http://www.foobar.com/'); $feed->setElement('description', 'my development site'); $feed->setElement('dc:author', 'by Joe Bloe'); $feed->setElementAttr('enclosure', 'url', 'http://www.foobar.com/'); $feed->setElementAttr('enclosure', 'length', '1234'); $feed->setElementAttr('enclosure', 'type', 'audio/mpeg'); $image =& new ContentFeederImage; $image->setElement('url', 'http://www.foobar.com/logo.gif'); $image->setElement('title', 'Our Logo'); $image->setElement('link', 'http://www.foobar.com/'); $feed->setImage($image); $item =& new ContentFeederItem; $item->setElement('title', 'Item title'); $item->setElement('link', 'Item link'); $item->setElement('description', 'Item description'); // ensure description does not conflict with XML $item->setElementEscapeType('description', 'cdata'); $item->setElement('author', 'Item author'); $item->setElement('category', 'Item category'); $item->setElement('comments', 'Item comments'); $feed->addItem($item); $item =& new ContentFeederItem; $item->setElement('title', 'Item title 2'); $item->setElement('link', 'Item link 2'); $item->setElement('description', 'Item description 2'); // ensure description does not conflict with XML $item->setElementEscapeType('description', 'cdata'); $item->setElement('author', 'Item author 2'); $item->setElement('category', 'Item category 2'); $item->setElement('comments', 'Item comments 2'); $feed->addItem($item); $feed->display(); OUTPUT: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="http://www.foobar.com/style.css" type="text/css"?> <rss version="2.0" xmlns="http://backend.userland.com/rss2" xmlns:dc="http://purl.org/dc/elements/1.1/"> <channel> <title>FOOBAR CONTENT FEED</title> <link>http://www.foobar.com/</link> <description>my development site</description> <pubDate>Wed, 8 Sep 2004 16:55:09 -0500</pubDate> <generator>ContentFeeder 1.0</generator> <image> <url>http://www.foobar.com/logo.gif</url> <title>Our Logo</title> <link>http://www.foobar.com/</link> </image> <item> <title>Item title</title> <link>Item link</link> <description><![CDATA[Item description]]></description> <author>Item author</author> <category>Item category</category> <comments>Item comments</comments> </item> <item> <title>Item title 2</title> <link>Item link 2</link> <description><![CDATA[Item description 2]]></description> <author>Item author 2</author> <category>Item category 2</category> <comments>Item comments 2</comments> </item> <dc:author>by Joe Bloe</dc:author> <enclosure url="http://www.foobar.com/" length="1234" type="audio/mpeg"/> </channel> </rss> DESCRIPTION: ContentFeeder - A library used for assembling content feeds. RSS 2.0 is currently the only format supported, but can easily be extended to handle more. ContentFeeder_RSS2 is the class name to instantiate to create RSS 2.0 feeds. Example: require('ContentFeeder.class.php'); $rss =& new ContentFeeder_RSS2; BASE CLASS METHODS: ------------------- setElement($element, $value) ---------------------------- Sets the value for the given element name. Example: $rss->setElement('link', 'http://www.foo.com'); setElementType($element, $type) ------------------------------- Sets the data type for the element. currently only 'date' is a valid element type, which will cause the output of the data to be formatted as a date specified by setDateFormat(). By default the RSS 2.0 'pubDate' and 'lastBuildDate' fields are set to type 'date'. Example: $rss->setElementType('pubDate', 'date'); setElementEscape($element, $enable = true) ------------------------------------------ Sets the escapement behavior on an element. This will escape the value suitable for XML. By default all elements are XML-escaped. NOTE: the default XML escapement will remove invalid unicode characters, use the 'cdata' escapement for preservation. Example: $rss->setElementEscape('foobar', false); setElementEscapeType($element, $type) ------------------------------------- Sets the escapement type on an element. This can be one of 'xml', 'cdata' or 'html'. Default escapement type is 'xml'. NOTE: the default XML escapement will remove invalid unicode characters, use the 'cdata' escapement for preservation. Example: $rss->setElementEscapeType('foobar', 'cdata'); setElementStripTags($element, $enable = true, $excluded_tags = null) -------------------------------------------------------------------- Sets the strip tag behavior on an element. When true, HTML tags are stripped from the contents. You can optionally pass a list of excluded tags. Example: $rss->setElementStripTags('description', true, '<p><br>'); setElementTruncate($element, $enable = true, $truncate_length = 255, $truncate_suffix = ' ...', $truncate_word_boundary = true) ------------------------------------------------------------------------------------------------------------------------------- Sets the truncation behavior on an element. When true, the contents will get truncated to 255 charactors followed by ' ...'. You can optionally pass the number of characters to trucate to, the suffix text and whether or not word boundaries are respected. Example: $rss->setElementTruncate('description', true, 100, ' ...', true); setElementDateFormat($element, $format) Sets the date format for a given element of type 'date'. By default, dates are formatted according to RFC 2822, or set manually with setDateFormat(). Date formats must be given in PHP date() format. Example: $rss->setElementDateFormat('pubDate', 'Y m d'); setElementAttr($element, $attr_name, $attr_value) ------------------------------------------------- Sets attributes for an element. Example: setElementAttr('description', 'foo', 'bar') XML OUTPUT: <description foo="bar">...</description> setCharset($charset) -------------------- Sets the character set for the feed. By default this is set to UTF-8 (NOTE: the default was ISO-8859-1 prior to ContentFeeder 2.0.) Example: $rss->setCharset('ISO-8859-1'); setDefaultEscapeType($type) --------------------------- Sets the default element escapement type. By default, this is set to 'none'. It might be useful to set this to 'cdata' to ensure all content does not collide with XML. Otherwise, you can set them individually with setElementEscapeType(). Example: $rss->setDefaultEscapeType('cdata'); enableCache() ------------- This enables caching of the feed output. disableCache() -------------- This disables caching of the feed output. setCacheTTL($ttl) ----------------- This sets the time-to-live for the cached content. Once this has expired, the cache is refreshed with new content. setCacheID($id) --------------- This sets the cache id, useful for multiple feeds. setCacheDir($dir) ----------------- This determines the directory where cache files will be written to. This directory must be writable by the dedicated server user. setDateFormat($format) ---------------------- Sets the default date format for elements of type 'date'. By default, dates are formatted according to RFC 2822. Date formats must be given in PHP date() format. Example: $rss->setDateFormat('Y m d'); enableHTTPHeaders() ------------------- This enables HTTP content headers in the output. This is true by default. Example: $rss->enableHTTPHeaders(); disableHTTPHeaders() -------------------- This disnables HTTP content headers in the output. Example: $rss->disableHTTPHeaders(); enableEmptyElements() ------------------- This enables tag generation for empty elements Example: $rss->enableEmptyElements(); disableEmptyElements() ------------------- This disables tag generation for empty elements (default) Example: $rss->disableEmptyElements(); addNamespace($name, $value) --------------------------- This adds an additional namespace to the feed. Example: $rss->addNamespace('dc', 'http://purl.org/dc/elements/1.1/'); setStylesheet($location, $type = 'xsl') --------------------------------------- This sets a stylesheet for the feed. This can be 'xsl' or 'css'. Example: $rss->setStylesheet('http://www.foo.com/style.css', 'css'); fetch() ------- Fetches the feed and returns it. Example: $output = $rss->fetch(); display() --------- Displays the feed to standard output. Example: $rss->display(); RSS 2.0 SPECIFIC CLASS METHODS: ------------------------------- setImage(&$value) Used to set the image values for the feed. Example: $image =& new ContentFeederImage; $image->setElement('url', 'http://www.foobar.com/logo.gif'); $image->setElement('title', 'Our Logo'); $image->setElement('link', 'http://www.foobar.com/'); $feed->setImage($image); setTextInput(&$value) Used to set the text in put parameters for the feed. (Not sure what this is for, but it's in the spec.) Example: $textinput =& new ContentFeedertextinput; $textinput->setElement('title', 'my title'); $textinput->setElement('description', 'my desc'); $textinput->setElement('name', 'my name'); $textinput->setElement('link', 'http://www.foobar.com/'); $feed->setTextInput($textinput); addItem(&$value) Used for adding an item to the feed. You can add as many items as you like, they will be listed one after another in the output. Example: $item =& new ContentFeederItem; $item->setElement('title', 'Item title'); $item->setElement('link', 'Item link'); $item->setElement('description', 'Item description'); // ensure description text does not collide with XML $item->setElementEscapeType('description', 'cdata'); $item->setElement('author', 'Item author'); $item->setElement('category', 'Item category'); $item->setElement('comments', 'Item comments'); // ensure comment text does not collide with XML $item->setElementEscapeType('comments', 'cdata'); $feed->addItem($item); Any questions, drop me a line. Monte Ohrt <monte [AT] ohrt [DOT] com> OTHER PROJECTS: View Monte's other projects