This example describes the PHP generated RSS file class files. Share to everyone for your reference. Specifically as follows:
PHP RSS generated class instance code is as follows:
Copy Code code as follows:
<?php
if (defined (' _class_rss_php ')) return;
Define (' _class_rss_php tutorial ', 1);
/**
* Instructions for use:
* $rss = new RSS (' Redfox ', ' http://jb51.net/', Redfox ' s 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);
}
}
?>
I hope this article will help you with your PHP program design.