php/**//* * @(#)rss2gen.inc.php (beta) 2005/2/19 * * exblog rss2 生成类,实现了rss2规范中的大部分内容。 * * 使用方法请见本类底部(rss2gen类使用注释)信息。 * * 本程序为免费程序,源代码使用者可任意更改(请勿用于商业用途),但请保留本版权信息! * * @author hexuzhong <[email protected]> * @version beta-2, 2005-03-11 * @since exblog-1.3.0 * */ class rss2gen { var $rss_header; //rss文档头,存放一个<rss>根元素及其version属性。 var $item; // var $items; var $channel_header; //频道的头部信息,存放频道名称、url、描述、语言、版权等等。 //--------------- 以下元素是可选的频道子元素 --------------// var $channel_language; //频道使用的语言种类,例如en-us、zh-cn等,放便聚集器组织中一语言的站点。 var $channel_copyright; //频道内容的版权声明。 var $channel_managingeditor; //对该频道内容负责的个人的email地址 var $channel_webmaster; //对该频道的技术支持负责的个人的email地址 var $channel_pubdate; //该频道内容的公布日期 var $channel_lastbuilddate; //上次频道内容更改的时间 var $channel_category; //说明频道属于哪一个或多个分类 var $channel_generator = "<generator>exblog rss2 builder</generator>"; //用于生成该频道的程序 var $channel_docs; //rss文件所使用格式的说明文档所在的url var $channel_cloud; //允许进程注册为“cloud”,频道更新时通知它 var $channel_ttl; //ttl 代表存活时间,存活时间是一个数字,表示提要在刷新之前缓冲的分钟数 var $channel_image; //指定一个能在频道中显示的gif、jpeg 或png 图像 //var $channel_rating; //关于该频道的 pics 评价 //var $channel_textinput; //定义可与频道一起显示的输入框 //var $channel_skiphours; //告诉聚集器哪些小时的更新可以忽略 //var $channel_skipdays; //告诉聚集器那一天的更新可以忽略 var $channel; //整个rss2文档信息,用与生成整个页面。 var $halt_on_error = true; /**//** * 构造函数,其参数皆为rss2必须的频道子元素,如果不填,则为默认内容。 * * @param string $title 频道的名称,频道的title应该和web站点的title尽量一致。 * @param string $link 与该频道关联的web站点的url。 * @param string $description 对频道的一段简单描述,如介绍频道是做什么的。 * @access public */ function rss2gen() { $this->fillheader(); } /**//** * 生成rss文档头,必须以一个<rss>元素作为根元素,其有一个强制属性version,指定当前文档遵守的rss版本。 * @access private */ function fillheader() { $this->rss_header = "
<?xml version="1.0" encoding="gb2312"?>
rn"; $this->rss_header .= " <rss version="2.0">rn"; } /**//** * 设置频道头,其中三个元素为必须的。 * @param string $title 频道的名称,频道的title应该和web站点的title尽量一致。 * @param string $link 与该频道关联的web站点或者站点区域的url。 * @param string $description 对频道的一段简单描述,简要介绍频道是做什么的。 */ function setchannel($title = "exblog", $link = "http://www.exblog.org", $description = "exblog") { $this->channel_header = " <title><![cdata[".$title."]]></title>n"; $this->channel_header .= " <link>".$link."</link>rn"; $this->channel_header .= " <description><![cdata[".$description."]]></description>rn"; } function halt($msg) { if ($this->halt_on_error) { $this->haltmsg($msg); } die("exblog rss2 exit"); } function haltmsg($msg) { sprintf("<b>exblog rss2 builder error: %s</b><br>rn", $msg); } /**//** * 设置该rss文档的语言,默认为简体中文。 * @access public */ function setlanguage($lang = "zh_cn") { $this->channel_language = " <language>".$lang."</language>rn"; } /**//** * 频道内容的版权声明 * @access public */ function setcopyright($copyright = "exblog") { $this->channel_copyright = " <copyright>".$copyright."</copyright>rn"; } /**//** * 对该频道内容负责的个人的email地址 * @access public */ function setmanagingeditor($email = null) { if (empty($email)) { $this->halt("如果你设置了该频道内容负责的个人的email地址,请填写她。"); } $this->channel_managingeditor = " <managingeditor>".$email."</managingeditor>rn"; } /**//** * 对该频道的技术支持负责的个人的email地址 * @access public */ function setwebmaster($email = null) { if (empty($email)) { $this->halt("如果你设置了该频道的技术支持负责的个人的email地址,请填写她。 :)"); } $this->channel_webmaster = " <webmaster>".$email."</webmaster>rn"; } /**//** * 该频道内容的公布日期。 * @access public */ function setpubdate($date = null) { if (empty($date)) { $date = date("y/m/d"); $this->channel_pubdate = " <pubdate>".$date."</pubdate>rn"; } $this->channel_pubdate = " <pubdate>".$date."</pubdate>rn"; } /**//** * 上次频道内容更改的时间 * @access public */ function setlastbuilddate($date = null) { if (empty($date)) { $date = date("y/m/d"); $this->channel_lastbuilddate = " <lastbuilddate>".$date."</lastbuilddate>rn"; } $this->channel_lastbuilddate = " <lastbuilddate>".$date."</lastbuilddate>rn"; } /**//** * 说明频道属于哪一个或多个分类 * @access public */ function setcategory($category) { $this->channel_category = " <category>".$category."</category>rn"; } /**//** * rss文件所使用格式的说明文档所在的url * @access public */ function setdocs($url) { $this->channel_docs = " <docs>".$url."</docs>rn"; } /**//** * ttl 代表存活时间,存活时间是一个数字,表示提要在刷新之前缓冲的分钟数 * @access public */ function setttl($minute) { $this->channel_ttl = " <ttl>".$minute."</ttl>rn"; } /**//** * 指定一个能在频道中显示的gif、jpeg 或png 图像 * * @param string $url 必需,是表示该频道的 gif、jpeg 或 png 图像的url * @param string $title 必需,是图象的描述。当频道以 html 呈现时,用作 html <image> 标签的 alt 属性。 * @param string $link 必需,是站点的 url。如果频道以 html 呈现,该图像作为到这个站点的链接。 * @param integer $width 表示图象的像素宽,必须和 $height 一同设置,否则不会显示此属性。 * @param integer $height 表示图象的像素高,必须和 $width 一同设置,否则不会显示此属性。 * @param string $description 围绕着该图像形成的链接元素的 title 属性。 * * @access public */ function setchannelimage($url, $title, $link, $width = null, $height = null, $description = null) { if (!isset($url)) $this->halt("如果你要设置图片,该图像地址属性必须填写。"); if (!isset($title)) $this->halt("如果你要设置图片,该图像描述属性必须填写。"); if (!isset($link)) $this->halt("如果你要设置图片,该图像链接属性必须填写。这个url一般和图像地址属性相同"); $this->channel_image = " <image>rn"; $this->channel_image .= " <url>".$url."</url>rn"; $this->channel_image .= " <title><![cdata[".$title."]]></title>rn"; $this->channel_image .= " <link>".$link."</link>rn"; //如果设置了图像宽、高则添加此属性。 if (!empty($width) && !empty($height)) { $this->channel_image .= " <width>".$width."</width>rn"; $this->channel_image .= " <height>".$height."</height>rn"; } if (!empty($description)) $this->channel_image .= " <description><![cdata[".$description."]]></description>rn"; $this->channel_image .= "</image>rn"; } /**//** * 添加一条item到channel中,虽然item的子元素都是可选的,但至少要存在一个title或description * * @param string $title 项(item)的标题 * @param string $description 项(item)的大纲 * @param string $link 项(item)的url * @param string $author 项(item)作者的email 地址 * @param string $category 包括项(item)的一个或多个分类 * @param string $comments 关于项(item)的注释页的url * @param string $enclosure 支持和该项(item)有关的媒体对象 * @param string $guid 唯一与该项(item)联系在一起的永久性链接 * @param string $pubdate 该项(item)是什么时候发布的 * @param string $source 该项(item)来自哪个rss 频道,当把项(item)聚合在一起时非常有用 * * @access public */ function additem($title = null, $description = null, $link = null, $author = null, $category = null, $comments = null, $enclosure = null, $guid = null, $pubdate = null, $source = null) { //判断是否至少添加了title或description if (empty($title) && empty($description)) { $this->halt("item:请至少设置title或description属性。"); } else { $this->item = " <item>rn"; if (!empty($title)) $this->item .= " <title><![cdata[".$title."]]></title>rn"; if (!empty($link)) $this->item .= " <link>".$link."</link>rn"; if (!empty($description)) $this->item .= " <description><![cdata[".$description."]]></description>rn"; if (!empty($author)) $this->item .= " <author><![cdata[".$author."]]></author>rn"; if (!empty($category)) $this->item .= " <category><![cdata[".$category."]]></category>rn"; if (!empty($comments)) $this->item .= " <comments><![cdata[".$comments."]]></comments>rn"; if (!empty($enclosure)) $this->item .= " <enclosure><![cdata[".$enclosure."]]></enclosure>rn"; if (!empty($guid)) $this->item .= " <guid>".$guid."</guid>rn"; if (!empty($pubdate)) $this->item .= " <pubdate>".$pubdate."</pubdate>rn"; if (!empty($source)) $this->item .= " <source><![cdata[".$source."]]></source>rn"; $this->item .= "</item>rn"; } $this->items .= $this->item; } /**//** * 输出 rss2 文档 */ function builder() { $this->stuff(); header("content-type:application/xml"); print $this->channel; } /**//** * 兼容老版本rss生成方法,去掉了写文件功能。 * */ function buildrssfeed($param = null) { $this->builder(); } /**//** * 合成整个rss2文档 * */ function stuff() { $this->channel = $this->rss_header; $this->channel .= $this->channel_header; if (isset($this->channel_language)) $this->channel .= $this->channel_language; if (isset($this->channel_copyright)) $this->channel .= $this->channel_copyright; if (isset($this->channel_managingeditor)) $this->channel .= $this->channel_managingeditor; if (isset($this->channel_webmaster)) $this->channel .= $this->channel_webmaster; if (isset($this->channel_pubdate)) $this->channel .= $this->channel_pubdate; if (isset($this->channel_lastbuilddate)) $this->channel .= $this->channel_lastbuilddate; if (isset($this->channel_category)) $this->channel .= $this->channel_category; if (isset($this->channel_docs)) $this->channel .= $this->channel_docs; if (isset($this->channel_cloud)) $this->channel .= $this->channel_cloud; if (isset($this->channel_ttl)) $this->channel .= $this->channel_ttl; if (isset($this->channel_image)) $this->channel .= $this->channel_image; $this->channel .= $this->channel_generator; $this->channel .= $this->items; $this->channel .= "</rss>rn"; } } /**//******************************* rss2gen 类使用注释 *******************/ /**//* $rss2gen = new rss2gen(); $rss2gen->setchannel("exblog222rss", "http://elliott.fengling.net", "elliott rss2gen"); //以下部分为可添加信息 //$rss2gen->setlanguage("zh-cn"); //$rss2gen->setcopyright("exblog @"); //$rss2gen->setmanagingeditor("[email protected]"); //$rss2gen->setwebmaster("[email protected]"); //$rss2gen->setpubdate(); //$rss2gen->setlastbuilddate(); //$rss2gen->setcategory("none"); //$rss2gen->setdocs("http://www.exblog.org/rss_docs.html"); //$rss2gen->setttl(600); //$rss2gen->setchannelimage("/xrssfile/2006-9/24/200692413947513.gif", "demo~~ :)", "http://www.exblog.org"); //------------------------ //---添加一条记录到rss文档中,至少需要一个参数,title或description $rss2gen->additem("item1", "item1's description", "http://www.exblog.org/?play=1", "elliott", "默认分类", "描述呀描述。", "支持和该项(item)有关的媒体对象", "唯一与该项(item)联系在一起的永久性链接", "2005-03-12", "该项(item)来自哪个rss 频道,当把项(item)聚合在一起时非常有用"); $rss2gen->additem("item2", "item2's description"); $rss2gen->additem("item3"); $rss2gen->builder(); //兼容老版本中方法 //$rss2gen->buildrssfeed(); */
新闻热点
疑难解答