Home >Backend Development >PHP Tutorial >PHP perfect RSS generation class_PHP tutorial

PHP perfect RSS generation class_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:10:51870browse

The RSS subscription function can be found on many websites, but there are also many. The following code is written by myself, and a PHP class is used in it: RSS.class.php. It feels very convenient and I don’t dare to keep it to myself, so I will share it with everyone.

The code is as follows Copy code
 代码如下 复制代码

include_once("class/RSS.class.php");//引入RSS PHP类
$RSS= new RSS("名称","地址","描述","RSS频道图标");
$RSS->AddItem("日志的标题","日志的地址","日志的摘要","日志的发布日期");
$RSS->Display();//输出RSS内容

include_once("class/RSS.class.php");//Introducing the RSS PHP class
$RSS= new RSS("Name","Address","Description","RSS Channel icon");
$RSS->AddItem("Title of the log", "Address of the log", "Summary of the log", "Publish date of the log");
$RSS-> ;Display();//Output RSS content


All codes are as follows:

 代码如下 复制代码
// +----------------------------------------------------------------------
// | YBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.hzhuti.com/nokia/n97/ All rights reserved.
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | Author: yhustc
// +----------------------------------------------------------------------
// $Id$
 
/**
+------------------------------------------------- --------------------------------
* RSS generation class
+------- -------------------------------------------------- ---------------------
* @author yhustc
* @version $Id$
+- -------------------------------------------------- --------------------------
*/
class RSS
{
    /**
+------------------------------------------------- ------------
* RSS channel name
+--------------------------- ----------------------------------
* @var string
* @access protected
+- -------------------------------------------------- -------
*/
    protected $channel_title = "';
    /**
+------------------------------------------------- ------------
* RSS channel link
+--------------------------- ----------------------------------
* @var string
* @access protected
+- -------------------------------------------------- -------
*/
    protected $channel_link = "';
    /**
+------------------------------------------------- ------------
* RSS channel description
+--------------------------- ----------------------------------
* @var string
* @access protected
+- -------------------------------------------------- -------
*/
    protected $channel_description = '';
    /**
+------------------------------------------------- ------------
* The URL of the small icon used by the RSS channel
+---------------------- ------------------------------------
* @var string
* @access protected
+------------------------------------------------- ------------
*/
    protected $channel_imgurl = '';
    /**
+------------------------------------------------- ------------
* Language used by RSS channel
+--------------------- ----------------------------------
* @var string
* @access protected
+------------------------------------------------ ----------
*/
    protected $language = 'zh_CN';
    /**
+------------------------------------------------- ------------
* RSS document creation date, default is today
+---------------------- ------------------------------------
* @var string
* @access protected
+------------------------------------------------- ------------
*/
    protected $pubDate = '';
    protected $lastBuildDate = '';
 
    protected $generator = 'YBlog RSS Generator';
 
    /**
     +----------------------------------------------------------
     * RSS单条信息的数组
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $items = array();
 
    /**
+------------------------------------------------- ------------
* Constructor
+---------------------------- ----------------------------------
* @access public
+--------- --------------------------------------------------
* @param string $title RSS channel name
* @param string $link RSS channel link
* @param string $description RSS channel description
* @param string $imgurl RSS channel icon
+------------------------------------------------- ---------
*/
    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());
    }
 
    /**
+------------------------------------------------- ------------
* Set private variables
+--------------------------- ----------------------------------
* @access public
+-------- --------------------------------------------------
* @param string $key Variable name
* @param string $value Variable value
+------------------------ ----------------------------------
*/
     public function Config($key,$value)
     {
        $this->{$key} = $value;
     }
 
    /**
+------------------------------------------------- ------------
* Add RSS item
+--------------------------- ----------------------------------
* @access public
+-------- --------------------------------------------------
* @param string $title The title of the log
* @param string $link The link of the log
* @param string $description The summary of the log
* @param string $pubDate The publishing date of the log
+-------------------------------------------------- ----------
*/
     function AddItem($title, $link, $description, $pubDate)
     {
        $this->items[] = array('title' => $title, 'link' => $link, 'description' => $description, 'pubDate' => $pubDate);
     }
 
     /**
+------------------------------------------------- ------------
* Output RSS XML as string
+----------------------- ----------------------------------
* @access public
+---- -------------------------------------------------- ----
* @return string
+----------------------------------- -----------------------
*/
    public function Fetch()
    {
        $rss = "rn";
        $rss = "rn";
        $rss .= "rn";
        $rss .= "<![CDATA[{$this->channel_title}]]>rn";
        $rss .= "channel_description}]]>rn";
        $rss .= "{$this->channel_link}rn";
        $rss .= "{$this->language}rn";
 
        if (!empty($this->pubDate))
            $rss .= "{$this->pubDate}rn";
        if (!empty($this->lastBuildDate))
            $rss .= "{$this->lastBuildDate}rn";
        if (!empty($this->generator))
            $rss .= "{$this->generator}rn";
 
        $rss .= "5rn";
 
        if (!empty($this->channel_imgurl)) {
            $rss .= "rn";
            $rss .= "<![CDATA[{$this->channel_title}]]>rn";
            $rss .= "{$this->channel_link}rn";
            $rss .= "{$this->channel_imgurl}rn";
            $rss .= "rn";
        }
 
        for ($i = 0; $i < count($this->items); $i++) {
            $rss .= "rn";
            $rss .= "<![CDATA[{$this->items[$i]['title']}]]>rn";
            $rss .= "{$this->items[$i]['link']}rn";
            $rss .= "items[$i]['description']}]]>rn";
            $rss .= "{$this->items[$i]['pubDate']}rn";
            $rss .= "
rn";
        }
 
        $rss .= "
rn
";
        return $rss;
    }
 
    /**
+------------------------------------------------- ------------
* Output RSS XML to the browser
+----------------------- ----------------------------------
* @access public
+---- -------------------------------------------------- ----
* @return void
+----------------------------------- -----------------------
*/
    public function Display()
    {
        header("Content-Type: text/xml; charset=utf-8");
        echo $this->Fetch();
        exit;
    }
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444698.htmlTechArticleRSS subscription function can be found on many websites, but there are many. The following code is written by myself and is used in it. A PHP class: RSS.class.php, which feels very convenient. I don’t dare to keep it to myself, so I just took it...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn