PHP 實(shí)現(xiàn)RSS訂閱類
?<?phpclass RSS{ /** +---------------------------------------------------------- * RSS頻道名 +---------------------------------------------------------- */ protected $channel_title = ’’; /** +---------------------------------------------------------- * RSS頻道鏈接 +---------------------------------------------------------- */ protected $channel_link = ’’; /** +---------------------------------------------------------- * RSS頻道描述 +---------------------------------------------------------- */ protected $channel_description = ’’; /** +---------------------------------------------------------- * RSS頻道使用的小圖標(biāo)的URL +---------------------------------------------------------- */ protected $channel_imgurl = ’’; /** +---------------------------------------------------------- * RSS頻道所使用的語言 +---------------------------------------------------------- */ protected $language = ’zh_CN’; /** +---------------------------------------------------------- * RSS文檔創(chuàng)建日期,默認(rèn)為今天 +---------------------------------------------------------- */ protected $pubDate = ’’; protected $lastBuildDate = ’’; protected $generator = ’YBlog RSS Generator’; /** +---------------------------------------------------------- * RSS單條信息的數(shù)組 +---------------------------------------------------------- */ protected $items = array(); /** +---------------------------------------------------------- * 構(gòu)造函數(shù) +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $title RSS頻道名 * @param string $link RSS頻道鏈接 * @param string $description RSS頻道描述 * @param string $imgurl RSS頻道圖標(biāo) +---------------------------------------------------------- */ 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()); } /** +---------------------------------------------------------- * 設(shè)置私有變量 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $key 變量名 * @param string $value 變量的值 +---------------------------------------------------------- */ public function Config($key,$value) {$this->{$key} = $value; } /** +---------------------------------------------------------- * 添加RSS項(xiàng) +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $title 日志的標(biāo)題 * @param string $link 日志的鏈接 * @param string $description 日志的摘要 * @param string $pubDate 日志的發(fā)布日期 +---------------------------------------------------------- */ 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; }}
?>
相關(guān)文章:
1. 阿里前端開發(fā)中的規(guī)范要求2. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案3. css進(jìn)階學(xué)習(xí) 選擇符4. UDDI FAQs5. XML入門的常見問題(一)6. html小技巧之td,div標(biāo)簽里內(nèi)容不換行7. PHP字符串前后字符或空格刪除方法介紹8. XML入門精解之結(jié)構(gòu)與語法9. Echarts通過dataset數(shù)據(jù)集實(shí)現(xiàn)創(chuàng)建單軸散點(diǎn)圖10. 概述IE和SQL2k開發(fā)一個(gè)XML聊天程序
