现在的位置: 首页 > 综合 > 正文

php rss生成类

2012年12月11日 ⁄ 综合 ⁄ 共 3136字 ⁄ 字号 评论关闭
<?php
/**
*   Class name: RSS
*/
if (defined('_CLASS_RSS_PHP')) return;
define('_CLASS_RSS_PHP',1);

class RSS {
//public
var $rss_ver = "2.0";
var $channel_title = '';
var $channel_link = '';
var $channel_description = '';
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
// $description
/**************************************************************************/
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());
}
/**************************************************************************/
// 函数名: AddItem
// 功能: 添加一个节点
// 参数: $title
// $link
// $description   $pubDate
/**************************************************************************/
function AddItem($title, $link, $description ,$pubDate) {
$this->items[] = array('title' => $title ,
'link' => $link,
'description' => $description,
'pubDate' => $pubDate);
}
/**************************************************************************/
// 函数名: BuildRSS
// 功能: 生成rss xml文件内容
/**************************************************************************/
function BuildRSS() {
$s = " <?xml version=\"1.0\" encoding=\"gb2312\" ?>
\n<rss version=\"2.0\">\n";
// start channel
$s .= "<channel>\n";
$s .= "<title><![CDATA[{$this->channel_title}]]></title>\n";
$s .= "<link><![CDATA[{$this->channel_link}]]></link>\n";
$s .= "<description><![CDATA[{$this->channel_description}]]></description>\n";
$s .= "<language>{$this->language}</language>\n";
if (!empty($this->copyright)) {
$s .= "<copyright><![CDATA[{$this->copyright}]]></copyright>\n";
}
if (!empty($this->webMaster)) {
$s .= "<webMaster><![CDATA[{$this->webMaster}]]></webMaster>\n";
}
if (!empty($this->pubDate)) {
$s .= "<pubDate>{$this->pubDate}</pubDate>\n";
}

if (!empty($this->lastBuildDate)) {
$s .= "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>\n";
}

if (!empty($this->generator)) {
$s .= "<generator>{$this->generator}</generator>\n";
}

// start items
for ($i=0;$i<count($this->items);$i++) {
$s .= "<item>\n";
$s .= "<title><![CDATA[{$this->items[$i]['title']}]]></title>\n";
$s .= "<link><![CDATA[{$this->items[$i]['link']}]]></link>\n";
$s .= "<description><![CDATA[{$this->items[$i]['description']}]]></description>\n";
$s .= "<pubDate>{$this->items[$i]['pubDate']}</pubDate>\n";           
$s .= "</item>\n";
}

// close channel
$s .= "</channel>\n</rss>";
$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);
}
}
?>

抱歉!评论已关闭.