PHP RSS generated class instance code is as follows:
code is as follows: <?php
if (defined (' _class_rss_php ')) return;
define (' _class_rss_php tutorial ', 1);
/**
* Use instructions:
* $rss = new RSS (' Redfox ', ' http://jb51.net/', ' redfox ' blog ');
* $rss->additem (' RSS class ', ' http://www.jb51.net ', ' xxx ', date ());
* $rss->additem (...);
* $rss->savetofile (...);
*/
class RSS {
//public
$rss _ver = "2.0";
$channel _title = ';
$channel _link = ';
$channel _description = ';
$language = ' ZH_CN ';
$copyright = ';
$webmaster = ';
$pubdate = ';
$lastbuilddate = ';
$generator = ' redfox rss generator ';
$content = ';
$items = Array ();
function rss ($title, $link, $description) {
$this->channel_title = $title;
$this->channel_link = $link;
$this->channel_description = $description;
$this->pubdate = Date (' y-m-d h:i:s ', Time ());
$this->lastbuilddate = Date (' y-m-d h:i:s ', Time ());
}
function additem ($title, $link, $description, $pubdate) {
$this->items[] = Array (' Titile ' => $title,
' link ' => $link,
' description ' => $description,
' pubdate ' => $pubdate);
}
function Buildrss () {
$s = "<!--l version=" 1.0 "encoding=" gb2312 "-->";
Start Channel
$s. = "";
$s. = ""
$s. = "<link/>{$this->channel_link}";
$s. = "{$this->channel_description}";
$s. = "{$this->language}";
if (!emptyempty ($this->copyright)) {
$s. = "{$this->copyright}";
}
if (!emptyempty ($this->webmaster)) {
$s. = "{$this->webmaster}";
}
if (!emptyempty ($this->pubdate)) {
$s. = "{$this->pubdate}";
}
if (!emptyempty ($this->lastbuilddate)) {
$s. = "{$this->lastbuilddate}";
}
if (!emptyempty ($this->generator)) {
$s. = "{$this->generator}";
}
//Start Items
for ($i =0 $iitems), $i + +) {
$s. = "";
$s. = "";
$s. = "<link/>{$this->items[$i [' Link ']}";
$s. = "<!--data[{$thi-->items[$i] [' description ']}]]> '];
$s. = "{$this->items[$i] [' pubdate ']}";
$s. = "";
}
//Close channel
$s. = "";
$this->content = $s;
}
function Show () {
if (Emptyempty ($this->content)) $this->buildrss ();
header (' Content-type:text/xml ');
Echo ($this->content);
}
function SaveToFile ($fname) {
if (Emptyempty ($this->content)) $this->buildrss ();
$handle = fopen ($fname, ' WB ');
if ($handle = = false) return false;
fwrite ($handle, $this->content);
Fclose ($handle);
}
}
?>