php rss產生類

來源:互聯網
上載者:User
<?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);
}
}
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.