程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php完美的rss 生成類

php完美的rss 生成類

編輯:關於PHP編程

RSS訂閱功能,在很多網站都可以有但也有很多,下面代碼是自己寫的,其中使用到了一個PHP類:RSS.class.php,感覺非常方便,不敢獨享,特拿出來跟大家分享。  代碼如下 復制代碼

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


全部代碼如下:

 代碼如下 復制代碼 <?php
// +----------------------------------------------------------------------
// | YBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.hzhuti.com/nokia/n97/ All rights reserved.
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | Author: yhustc <[email protected]>
// +----------------------------------------------------------------------
// $Id$
 
/**
 +------------------------------------------------------------------------------
 * RSS生成類
 +------------------------------------------------------------------------------
 * @author    yhustc <[email protected]>
 * @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;
    }
}
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved