Home >Backend Development >PHP Tutorial >PHP perfect RSS generation class_PHP tutorial
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 | ||||
$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 = " $rss .= " $rss .= " $rss .= " $rss .= "{$this->channel_link}rn"; $rss .= " if (!empty($this->pubDate)) $rss .= " if (!empty($this->lastBuildDate)) $rss .= " if (!empty($this->generator)) $rss .= " $rss .= " if (!empty($this->channel_imgurl)) { $rss .= " $rss .= " $rss .= "{$this->channel_link}rn"; $rss .= " $rss .= " } for ($i = 0; $i < count($this->items); $i++) { $rss .= " $rss .= " $rss .= "{$this->items[$i]['link']}rn"; $rss .= " $rss .= " $rss .= " } $rss .= " 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; } } ?> |