PHP 實現RSS訂閱類
?<?phpclass RSS{ /** +---------------------------------------------------------- * RSS頻道名 +---------------------------------------------------------- */ protected $channel_title = ’’; /** +---------------------------------------------------------- * RSS頻道鏈接 +---------------------------------------------------------- */ protected $channel_link = ’’; /** +---------------------------------------------------------- * RSS頻道描述 +---------------------------------------------------------- */ protected $channel_description = ’’; /** +---------------------------------------------------------- * RSS頻道使用的小圖標的URL +---------------------------------------------------------- */ protected $channel_imgurl = ’’; /** +---------------------------------------------------------- * RSS頻道所使用的語言 +---------------------------------------------------------- */ protected $language = ’zh_CN’; /** +---------------------------------------------------------- * RSS文檔創建日期,默認為今天 +---------------------------------------------------------- */ protected $pubDate = ’’; protected $lastBuildDate = ’’; protected $generator = ’YBlog RSS Generator’; /** +---------------------------------------------------------- * RSS單條信息的數組 +---------------------------------------------------------- */ protected $items = array(); /** +---------------------------------------------------------- * 構造函數 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $title RSS頻道名 * @param string $link RSS頻道鏈接 * @param string $description RSS頻道描述 * @param string $imgurl RSS頻道圖標 +---------------------------------------------------------- */ public function __construct($title, $link, $description, $imgurl = ’’) {$this->channel_title = $title;$this->channel_link = $link;$this->channel_description = $description;$this->channel_imgurl = $imgurl;$this->pubDate = Date(’Y-m-d H:i:s’, time());$this->lastBuildDate = Date(’Y-m-d H:i:s’, time()); } /** +---------------------------------------------------------- * 設置私有變量 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $key 變量名 * @param string $value 變量的值 +---------------------------------------------------------- */ public function Config($key,$value) {$this->{$key} = $value; } /** +---------------------------------------------------------- * 添加RSS項 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $title 日志的標題 * @param string $link 日志的鏈接 * @param string $description 日志的摘要 * @param string $pubDate 日志的發布日期 +---------------------------------------------------------- */ function AddItem($title, $link, $description, $pubDate) {$this->items[] = array(’title’ => $title, ’link’ => $link, ’description’ => $description, ’pubDate’ => $pubDate); } /** +---------------------------------------------------------- * 輸出RSS的XML為字符串 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @return string +---------------------------------------------------------- */ public function Fetch() {$rss .= '<?xml version='1.0' encoding='utf-8' ?>rn';$rss .= '<rss version='2.0'>rn';$rss .= '<channel>rn';$rss .= '<title><![CDATA[{$this->channel_title}]]></title>rn';$rss .= '<description><![CDATA[{$this->channel_description}]]></description>rn';$rss .= '<link>{$this->channel_link}</link>rn';$rss .= '<language>{$this->language}</language>rn'; if (!empty($this->pubDate)) $rss .= '<pubDate>{$this->pubDate}</pubDate>rn';if (!empty($this->lastBuildDate)) $rss .= '<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>rn';if (!empty($this->generator)) $rss .= '<generator>{$this->generator}</generator>rn'; $rss .= '<ttl>5</ttl>rn'; if (!empty($this->channel_imgurl)) { $rss .= '<image>rn'; $rss .= '<title><![CDATA[{$this->channel_title}]]></title>rn'; $rss .= '<link>{$this->channel_link}</link>rn'; $rss .= '<url>{$this->channel_imgurl}</url>rn'; $rss .= '</image>rn';} for ($i = 0; $i < count($this->items); $i++) { $rss .= '<item>rn'; $rss .= '<title><![CDATA[{$this->items[$i][’title’]}]]></title>rn'; $rss .= '<link>{$this->items[$i][’link’]}</link>rn'; $rss .= '<description><![CDATA[{$this->items[$i][’description’]}]]></description>rn'; $rss .= '<pubDate>{$this->items[$i][’pubDate’]}</pubDate>rn'; $rss .= '</item>rn';} $rss .= '</channel>rn</rss>';return $rss; } /** +---------------------------------------------------------- * 輸出RSS的XML到瀏覽器 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @return void +---------------------------------------------------------- */ public function Display() {header('Content-Type: text/xml; charset=utf-8');echo $this->Fetch();exit; }}
?>
相關文章:
