在项目中用到的rss 2.0版本的php生成类。
只需要根据数据结构吧数据输入进去就能生成一个xml文件,并通过函数输出。
/** * rss生成模块 * @author jerryli (@lijian@dzs.mobi) * @see 协议说明 rss规范遵循 2.0 * http://jyjsjd.iteye.com/blog/1543483 * http://rayleeya.iteye.com/blog/372802 */final class crssbuilder{ /** * 本地字符集 * @var string */ const local_charset = 'gbk'; /** * 语言 * @var string */ const language = 'zh-cn'; /** * rss所有者 * @var string */ const generator = 'www.csdn.net'; /** * 板块通道 * @var array */ private $_achannel = null; /** * 构造函数 * @param string $achannel 频道属性 *
array('title'=>'频道标题', 'description'=>'频道摘要', 'link'=>'本频道url地址') */ public function __construct($achannel){ header('content-type:text/xml; charset=utf-8'); //必须输出标准的xml头 $this->_achannel = array('channel'=>$achannel, 'items'=>array()); } /** * 析构函数 */ public function __destruct(){ } /** * 在通道内添加子项目 * @param int $ichannelid 通道id(即:addchannel()) * array('title'=>'标题','link'=>'文章的url地址', 'description'=>'文章摘要','category'=>'栏目名称','utc_timestemp'=>'发布时间') * @return int item的数量 */ public function additems($aparam){ $this->_achannel['items'][] = $aparam; return count($this->_achannel['items']); } /** * 将内容序列化成rss的xml结构数据字符串 * @return string | null */ public function getserialize(){ static $stemplatechannel = null; static $stemplateitem = null; if (empty($stemplatechannel) || empty($stemplateitem)){ $atmp = array(); $atmp[] = ' '; $atmp[] = ' '. self::language .''; $atmp[] = ' '. self::generator .''; $atmp[] = ' '. (60 * 24) .''; //更新时间为24小时 $atmp[] = ' '. gmdate(d, d m y 23:59:59, time()) .' gmt'; //当前rss最后发布的时间 $atmp[] = ' '; $atmp[] = ' '; $atmp[] = ' '; $atmp[] = '{@item}'; $atmp[] = ' '; $stemplatechannel = implode(\n, $atmp); unset($atmp); $atmp = array(); $atmp[] = ' '; $atmp[] = ' '; $atmp[] = ' {@item_link}'; $atmp[] = ' '; $atmp[] = ' '; $atmp[] = ' '; $atmp[] = ' {@item_pubdate}'; //文章发布时间 $atmp[] = ' '; $stemplateitem = implode(\n, $atmp); unset($atmp); } if (!empty($this->_achannel)){ $atmpitem = array(); foreach ($this->_achannel['items'] as $aitem){ //内层遍历item $aparam = array( '{@item_title}'=> self::convert_encoding($aitem['title'], self::local_charset), '{@item_link}'=> $aitem['link'], '{@item_description}'=> self::convert_encoding($aitem['description'], self::local_charset), '{@item_category}'=> self::convert_encoding($aitem['category'], self::local_charset), '{@item_pubdate}'=> gmdate(d, d m y 23:59:59, $aitem['utc_timestemp']) .' gmt', ); $atmpitem[] = strtr($stemplateitem, $aparam); unset($aparam);$aparam=null; } $aparam = array( '{@channel_title}'=> self::convert_encoding($this->_achannel['channel']['title'], self::local_charset), '{@channel_link}'=> $this->_achannel['channel']['link'], '{@channel_description}'=> self::convert_encoding($this->_achannel['channel']['description'], self::local_charset), '{@item}'=> implode(\n, $atmpitem), ); $aoutbuf = array(); $aoutbuf[] = ''; $aoutbuf[] = ''; $aoutbuf[] = strtr($stemplatechannel, $aparam);; unset($aparam);$aparam=null; $aoutbuf[] = ''; return implode(\n, $aoutbuf); }else{ return null; } } /** * 对变量内容的进行字符编码转换 * @param string $sincharset 转换前的字符集 * @param string $soutcharset 转换后的字符集 * @param string | array $mixd 待转换的变量(数组或字符串) * @return string | array 完成转换后的结果 */ static public function convert_encoding(& $mixd, $sincharset, $soutcharset='utf-8') { if ($sincharset === $soutcharset) //字符集相同时不转换 return $mixd; if (is_array($mixd)) { $tmp = array(); foreach ($mixd as $key => $val) { $tmp[$key] = self::convert_encoding($sincharset, $soutcharset, $val); } return $tmp; } else { //字符集相同时不转换 return mb_convert_encoding($mixd, $soutcharset, $sincharset); } }}
使用的时候将里面的几个常量修改成你需要的参数,然后如下述用就能输出标准的rss代码。本程序使用 gbk的本地环境字符集,如果你的本地环境是utf-8,请修改这个条代码 local_charset = 'gbk';
/*测试用例*/$achannel = array('title'=>'每周精选','description'=>'二手车最新新闻','link'=>'http://www.chemao.com.cn/baike-gonglue.html');$o = new crssbuilder($achannel);$aitem = array( 'title'=>'迎接baby ,上车猫网甜蜜选购心仪polo', 'link'=>'http://www.chemao.com.cn/baike/cwpolo.html', 'description'=>'我和老公相恋于大学,都是北方人。因为家里适合自己的就业机会少,毕业后两人一起来到杭州发展。拼搏了两年,去年7月我们终于结束了5年的爱情长跑,在西子湖畔与亲友一起见证了我们的爱情。', 'category'=>'每周精选', 'utc_timestemp'=>time());$o->additems($aitem);$aitem = array( 'title'=>'为满足空间需求,买个二手mpv可还行!', 'link'=>'http://www.chemao.com.cn/baike/esmpv.html', 'description'=>'随着二胎政策与全民创业的逐步兴起,路面上跑的mpv越来越多,此类车型的强大用途已经让很多人爱不释手。而金九银十的到来不仅令新车优惠越来越大,二手车价格也在急剧跳水,这其中mpv的价格更是”一贬再贬“,对于想入手这类车型的网友来说,现在下手是绝好时机!下面,下面就为大家推荐几款二手车市场常见的热门mpv。', 'category'=>'每周精选', 'utc_timestemp'=>time());$o->additems($aitem);echo $o->getserialize();
以上就介绍了rss 20 php生成类,快读生成需要xml文件,包括了创业方面的内容,希望对php教程有兴趣的朋友有所帮助。