Home > Backend Development > PHP Tutorial > 网站添加RSS功能

网站添加RSS功能

WBOY
Release: 2016-06-20 13:05:01
Original
1291 people have browsed it

为方便用户定制站点内容而设立的各个RSS频道。浏览者通过订阅不同的RSS(可同时订阅多个网站),就能在不登录网站的情况下获得及时的新闻信息,还可以避免网页上无用的广告和垃圾信息的干扰。使用RSS会为浏览者节省大量的时间,也会成为体现网站人性化设计的一个亮点,提升了网站的档次。其实RSS技术并不太难,如果你的网站信息量较大,完全可以把这一技术运用到自己的站点中来。

一、什么是RSS

  RSS是站点与站点之间共享内容的一种简易方式(也称为“聚合内容”),通常被用于新闻和其他按顺序排列的网站,例如Blog网站。网站提供RSS输出,有利于让用户发现网站内容的更新。网站用户可以在客户端借助于类似新闻资讯阅读器等支持RSS的新闻聚合工具软件,在不打开网站内容页面的情况下阅读支持RSS输出的网站内容。

要想为网站创建RSS,首先我们必须对RSS进行深入的了解。RSS是基于XML(可扩展标志语言)的一种形式,并且所有的RSS文件都要遵守万维网联盟(W3C)站点发布的XML 1.0规范。一般来说,RSS文档的最顶层是一个元素作为根元素,元素有一个强制属性version,用于指定当前RSS文档的版本,目前常用的RSS版本是2.0。元素下的子元素是唯一的一个元素,它包含了关于该网站或栏目的信息和内容,在下必备的语句有三个:

:网站或栏目的名称,一般与网站或栏目的页面title一致;<p><link>:网站或栏目的URL;</p> <p><description>:对网站或栏目的简要描述。</description></p> <p>还可以使用一些如<language>(语言)、<copyright>(版权声明)等可选语句来丰富<channel>内容,具体的新闻提要就要依靠<item>来体现了。一般一条新闻就是一个<item>,<item>下至少要存在一个<title>或<description>,其他语句可以根据需要进行选择。</description>

二、RSS制作

对于一个动态网站,我们可以用我们的编程语言来自动生成网站rss订阅输出,下面是用CodeIgniter框架生成rss输出类代码:

class CI_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 = 'SCutePHP 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($par = array())
{
$this->channel_title = $par[0];
$this->channel_link = $par[1];
$this->channel_description = $par[2];
$this->channel_imgurl = $par[3];
$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 = ''."rn";
$rss .= ''."rn";
$rss .= ''."rn";
$rss .= '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 .= '5'."rn";
if (!empty($this->channel_imgurl)) {
$rss .= ''."rn";
$rss .= 'channel_title.']]>'."rn";
$rss .= ''.$this->channel_link.''."rn";
$rss .= ''.$this->channel_imgurl.''."rn";
$rss .= ''."rn";
}
for ($i = 0; $i items); $i++) {
$rss .= ''."rn";
$rss .= '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;
}
/**
+----------------------------------------------------------
* 输出RSS的XML到浏览器
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
public function Display()
{
header('Content-Type: text/xml; charset=utf-8');
echo $this->Fetch();
exit;
}
}


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template