RSS类
/**
* Class name: RSS
*/
class RSS {
//public
var $rss_ver = "2.0";
var $channel_title = '';
var $channel_link = '';
var $channel_descrīption = '';
var $language = 'zh_CN';
var $copyright = '';
var $webMaster = '';
var $pubDate = '';
var $lastBuildDate = '';
var $generator = 'RedFox RSS Generator';
var $content = '';
var $items = array();
/**************************************************************************/
// 函数名: RSS
// 功能: 构造函数
// 参数: $title
// $link
// $descrīption
/**************************************************************************/
function RSS($title, $link, $descrīption) {
$this->channel_title = $title;
$this->channel_link = $link;
$this->channel_descrīption = $descrīption;
$this->pubDate = Date('Y-m-d H:i:s',time());
$this->lastBuildDate = Date('Y-m-d H:i:s',time());
}
/**************************************************************************/
// 函数名: AddItem
// 功能: 添加一个节点
// 参数: $title
// $link
// $descrīption $pubDate
/**************************************************************************/
function AddItem($title, $link, $descrīption ,$pubDate) {
$this->items[] = array('title' => $title ,'link' => $link,'descrīption' => $descrīption,'pubDate' => $pubDate);
}
/**************************************************************************/
// 函数名: BuildRSS
// 功能: 生成rss xml文件内容
/**************************************************************************/
function BuildRSS() {
$s = "<?xml version='1.0' encoding='utf-8'?>\n\n";
// start channel
$s .= "\n";
$s .= "
channel_title}]]>\n";$s .= "channel_link}]]>\n";
$s .= "channel_descrīption}]]>\n";
$s .= "{$this->language}\n";
if (!empty($this->copyright)) {
$s .= "copyright}]]>\n";
}
if (!empty($this->webMaster)) {
$s .= "webMaster}]]>\n";
}
if (!empty($this->pubDate)) {
$s .= "{$this->pubDate}\n";
}
if (!empty($this->lastBuildDate)) {
$s .= "{$this->lastBuildDate}\n";
}
if (!empty($this->generator)) {
$s .= "{$this->generator}\n";
}
// start items
for ($i=0;$iitems);$i++) {
$s .= "\n";
$s .= "
items[$i]['title']}]]>\n";$s .= "items[$i]['link']}]]>\n";
$s .= "items[$i]['descrīption']}]]>\n";
$s .= "{$this->items[$i]['pubDate']}\n";
$s .= "\n";
}
// close channel
$s .= "\n";
$this->content = $s;
}
/**************************************************************************/
// 函数名: Show
// 功能: 将产生的rss内容直接打印输出
/**************************************************************************/
function Show() {
if (empty($this->content)) $this->BuildRSS();
echo($this->content);
}
/**************************************************************************/
// 函数名: SaveToFile
// 功能: 将产生的rss内容保存到文件
// 参数: $fname 要保存的文件名
/**************************************************************************/
function SaveToFile($fname) {
$handle = fopen($fname, 'wb');
if ($handle === false) return false;
fwrite($handle, $this->content);
fclose($handle);
}
function getFile($fname) {
$handle = fopen($fname, 'r');
if ($handle === false) return false;
while(!feof($handle)){
echo fgets($handle);
}
fclose($handle);
}
}
?> $result为从数据库中查询出来的值
foreach($result as $row) {
$rss->AddItem('标题','链接地址','描述','发布时间');
//具体参数根据上面的php类调整,这里仅作示例
}
$rss->RSS('','','');
$rss->BuildRSS();
$rss->SaveToFile('rss.xml');
$rss->getFile('rss.xml');