为方便用户定制站点内容而设立的各个RSS频道。浏览者通过订阅不同的RSS(可同时订阅多个网站),就能在不登录网站的情况下获得及时的新闻信息,还可以避免网页上无用的广告和垃圾信息的干扰。使用RSS会为浏览者节省大量的时间,也会成为体现网站人性化设计的一个亮点,提升了网站的档次。其实RSS技术并不太难,如果你的网站信息量较大,完全可以把这一技术运用到自己的站点中来。
RSS是站点与站点之间共享内容的一种简易方式(也称为“聚合内容”),通常被用于新闻和其他按顺序排列的网站,例如Blog网站。网站提供RSS输出,有利于让用户发现网站内容的更新。网站用户可以在客户端借助于类似新闻资讯阅读器等支持RSS的新闻聚合工具软件,在不打开网站内容页面的情况下阅读支持RSS输出的网站内容。
要想为网站创建RSS,首先我们必须对RSS进行深入的了解。RSS是基于XML(可扩展标志语言)的一种形式,并且所有的RSS文件都要遵守万维网联盟(W3C)站点发布的XML 1.0规范。一般来说,RSS文档的最顶层是一个
:网站或栏目的URL;
还可以使用一些如
对于一个动态网站,我们可以用我们的编程语言来自动生成网站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"; '."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"; '."rn";
$rss .= 'channel_title.']]> '."rn";
$rss .= ''.$this->channel_link.''."rn";
$rss .= ''.$this->channel_imgurl.' '."rn";
$rss .= '
}
for ($i = 0; $i items); $i++) {
$rss .= '- '."rn";
'."rn";
$rss .= 'items[$i]['title'].']]> '."rn";
$rss .= ''.$this->items[$i]['link'].''."rn";
$rss .= 'items[$i]['description'].']]> '."rn";
$rss .= ''.$this->items[$i]['pubDate'].' '."rn";;
$rss .= '
}
$rss .= '
return $rss;
}
/**
+----------------------------------------------------------
* 输出RSS的XML到浏览器
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
public function Display()
{
header('Content-Type: text/xml; charset=utf-8');
echo $this->Fetch();
exit;
}
}