Creating RSS 2.0 Feeds - Creating RSS 2.0 with PHP
(Page 3 of 4 )
Great RSS 2.0 support for PHP is to be found in the feedcreator.class by Kai Blankenhorn at http://www.bitfolge.de. Unlike the previous section's XML::RSS, feedcreator.class can only create RSS feeds; it can't parse them. No matter: it's very good at that indeed.
As illustrated in Example 4-10, the function for each feed element is named after the element, so it behaves pretty much as you would expect.
Example 4-10. A PHP script using FeedCreator that produces RSS 2.0
<?php
include("feedcreator.class.php");
$rss = new UniversalFeedCreator();
$rss->title = "Example Feed";
$rss->description = "This is the feed description";
$rss->link = "http://www.example.com/";
// Image section
$image = new FeedImage();
$image->title = "example logo";
$image->url = "http://www.example.com/images/logo.gif"; $image->link = "http://www.example.com"; $image->description = "Visit Example.com!"; $rss->image = $image;
// Item Loop
$item = new FeedItem();
$item->title = "Entry one";
$item->link = http://www.example.com/entryone;
$item->description = "This is the content of the first entry";
$item->author = "Ben Hammersley";
$rss->addItem($item);
// End Item Loop
?>
This is a very simple script. As you can see from the resulting feed, Example 4-11, it produces only one item. We'll be using it for more complicated things later on in the book, so in the meantime, once we've passed over the example output, we'll take a quick look at some of the special features.
Example 4-11. An RSS 2.0 feed created with PHP
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- generator="FeedCreator 1.7.1" -->
<rss version="2.0">
<channel>
<title>Example Feed</title>
<description>This is the feed description</description>
<link>http://www.example.com/</link>
<lastBuildDate>Thu, 16 Sep 2004 20:16:22 +0100</lastBuildDate>
<generator>FeedCreator 1.7.1</generator>
<image>
<url>http://www.example.com/ images/logo.gif</url>
<title>example logo</title>
<link>http://www.example.com </link>
<description>Visit Example.com!</description>
</image>
<item>
<title>Entry one</title>
<link>http://www.example.com/ entryone</link>
<description>This is the content of the first entry</description>
<author>Ben Hammersley</author>
</item>
</channel>
</rss>
Next: Caching and saving >>
More XML Tutorials Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Developing Feeds with RSS and Atom, written by Ben Hammersley (O'Reilly; ISBN: 0596008813). Check it out today at your favorite bookstore. Buy this book now.
|
|