The examples in this article describe the RSS-generated classes for PHP implementations. Share to everyone for your reference. as follows:
Class RSS {var $title;
var $link;
var $description;
var $language = "en-US";
var $pubDate;
var $items;
var $tags;
function RSS () {$this->items = array ();
$this->tags = Array ();
function AddItem ($item) {$this->items[] = $item; The function setpubdate ($when) {if (Strtotime ($when) = False) $this->pubdate = Date ("D, D M Y h:i:s", $when).
"GMT"; else $this->pubdate = Date ("D, D M Y h:i:s", Strtotime ($when)). "
GMT "; function Getpubdate () {if (Empty ($this->pubdate)) return date ("D, D M Y h:i:s"). "
GMT ";
else return $this->pubdate;
function Addtag ($tag, $value) {$this->tags[$tag] = $value;
function out () {$out = $this->header ();
$out. = "<channel>\n"; $out. = "<title>". $this->title.
"</title>\n"; $out. = "<link>". $this->link.
"</link>\n"; $out. = "<description>". $this->description.
"</description>\n"; $out. = "<language>" . $this->language.
"</language>\n"; $out. = "<pubDate>". $this->getpubdate ().
"</pubdate>\n";
foreach ($this->tags as $key => $val) $out. = "< $key > $val </$key >\n";
foreach ($this->items as $item) $out. = $item->out ();
$out. = "</channel>\n";
$out. = $this->footer ();
$out = Str_replace ("&", "&", $out);
return $out;
The function serve ($contentType = "Application/xml") {$xml = $this->out ();
Header ("Content-type: $contentType");
Echo $xml; The function header () {$out = ' <?xml version= ' 1.0 ' encoding= ' utf-8 '?> '.
"\ n"; $out. = ' <rss version= ' 2.0 ' xmlns:dc= ' http://purl.org/dc/elements/1.1/' > '.
"\ n";
return $out;
function footer () {return ' </rss> ';
} class RssItem {var $title;
var $link;
var $description;
var $pubDate;
var $guid;
var $tags;
var $attachment;
var $length;
var $mimetype;
function RssItem () {$this->tags = array (); } function SetpubdAte ($when) {if (Strtotime ($when) = = False) $this->pubdate = Date ("D, D M Y h:i:s", $when). "
GMT "; else $this->pubdate = Date ("D, D M Y h:i:s", Strtotime ($when)). "
GMT "; function Getpubdate () {if (Empty ($this->pubdate)) return date ("D, D M Y h:i:s"). "
GMT ";
else return $this->pubdate;
function Addtag ($tag, $value) {$this->tags[$tag] = $value;
function out () {$out. = "<item>\n"; $out. = "<title>". $this->title.
"</title>\n"; $out. = "<link>". $this->link.
"</link>\n"; $out. = "<description>". $this->description.
"</description>\n"; $out. = "<pubDate>". $this->getpubdate ().
"</pubdate>\n"; if ($this->attachment!= "") $out. = "<enclosure url= ' {$this->attachment} ' length= ' {$this->length} ' type= ' {
$this->mimetype} '/> ';
if (Empty ($this->guid)) $this->guid = $this->link; $out. = "<guid>". $this->guid. ";/guid>\n ";
foreach ($this->tags as $key => $val) $out. = "< $key > $val </$key \n>";
$out. = "</item>\n";
return $out;
Function Enclosure ($url, $mimetype, $length) {$this->attachment = $url;
$this->mimetype = $mimetype;
$this->length = $length; }
}
Use the example below:
$feed = new RSS ();
$feed->title = "RSS feed title";
$feed->link = "http://website.com";
$feed->description = "Recent articles on your website.";
$db->query ($query);
$result = $db->result;
while ($row = Mysql_fetch_array ($result, MYSQL_ASSOC))
{
$item = new RssItem ();
$item->title = $title;
$item->link = $link;
$item->setpubdate ($create _date);
$item->description = "<! [cdata[$html]]> ";
$feed->additem ($item);
}
echo $feed->serve ();
I hope this article will help you with your PHP program design.