php完美的rss 產生類

來源:互聯網
上載者:User
 代碼如下 複製代碼

include_once("class/RSS.class.php");//引入RSS PHP類
$RSS= new RSS("名稱","地址","描述","RSS頻道表徵圖");
$RSS->AddItem("日誌的標題","日誌的地址","日誌的摘要","日誌的發布日期");
$RSS->Display();//輸出RSS內容


全部代碼如下:

 代碼如下 複製代碼
<?php
// +----------------------------------------------------------------------
// | YBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.111cn.net/nokia/n97/ All rights reserved.
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | Author: yhustc <yhustc@gmail.com>
// +----------------------------------------------------------------------
// $Id$
 
/**
 +------------------------------------------------------------------------------
 * RSS產生類
 +------------------------------------------------------------------------------
 * @author    yhustc <yhustc@gmail.com>
 * @version   $Id$
 +------------------------------------------------------------------------------
 */
class RSS
{
    /**
     +----------------------------------------------------------
     * RSS頻道名
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $channel_title = '';
    /**
     +----------------------------------------------------------
     * RSS頻道連結
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $channel_link = '';
    /**
     +----------------------------------------------------------
     * RSS頻道描述
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $channel_description = '';
    /**
     +----------------------------------------------------------
     * RSS頻道使用的小表徵圖的URL
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $channel_imgurl = '';
    /**
     +----------------------------------------------------------
     * RSS頻道所使用的語言
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $language = 'zh_CN';
    /**
     +----------------------------------------------------------
     * RSS文檔建立日期,預設為今天
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $pubDate = '';
    protected $lastBuildDate = '';
 
    protected $generator = 'YBlog RSS Generator';
 
    /**
     +----------------------------------------------------------
     * RSS單條資訊的數組
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    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;
    }
}
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.