php完美的rss 生成类

RSS订阅功能,在很多网站都可以有但也有很多,下面代码是自己写的,其中使用到了一个PHP类:RSS.class.php,感觉非常方便,不敢独享,特拿出来跟大家分享.

类的调用代码如下:

  1. include_once("class/RSS.class.php");//引入RSS PHP类
  2. $RSS= new RSS("名称","地址","描述","RSS频道图标");
  3. $RSS->AddItem("日志的标题","日志的地址","日志的摘要","日志的发布日期");
  4. $RSS->Display();//输出RSS内容

全部代码如下:

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | YBlog
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2008 http://www.111cn.net/nokia/n97/ All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // +----------------------------------------------------------------------
  8. // | Author: yhustc <yhustc@gmail.com>
  9. // +----------------------------------------------------------------------
  10. // $Id$
  11. /**
  12. +------------------------------------------------------------------------------
  13. * RSS生成类
  14. +------------------------------------------------------------------------------
  15. * @author yhustc <yhustc@gmail.com>
  16. * @version $Id$
  17. +------------------------------------------------------------------------------
  18. */
  19. class RSS
  20. {
  21. /**
  22. +----------------------------------------------------------
  23. * RSS频道名
  24. +----------------------------------------------------------
  25. * @var string
  26. * @access protected
  27. +----------------------------------------------------------
  28. */
  29. protected $channel_title = '';
  30. /**
  31. +----------------------------------------------------------
  32. * RSS频道链接
  33. +----------------------------------------------------------
  34. * @var string
  35. * @access protected
  36. +----------------------------------------------------------
  37. */
  38. protected $channel_link = '';
  39. /**
  40. +----------------------------------------------------------
  41. * RSS频道描述
  42. +----------------------------------------------------------
  43. * @var string
  44. * @access protected
  45. +----------------------------------------------------------
  46. */
  47. protected $channel_description = '';
  48. /**
  49. +----------------------------------------------------------
  50. * RSS频道使用的小图标的URL
  51. +----------------------------------------------------------
  52. * @var string
  53. * @access protected
  54. +----------------------------------------------------------
  55. */
  56. protected $channel_imgurl = '';
  57. /**
  58. +----------------------------------------------------------
  59. * RSS频道所使用的语言
  60. +----------------------------------------------------------
  61. * @var string
  62. * @access protected
  63. +----------------------------------------------------------
  64. */
  65. protected $language = 'zh_CN';
  66. /**
  67. +----------------------------------------------------------
  68. * RSS文档创建日期,默认为今天
  69. +----------------------------------------------------------
  70. * @var string
  71. * @access protected
  72. +----------------------------------------------------------
  73. */
  74. protected $pubDate = '';
  75. protected $lastBuildDate = '';
  76. protected $generator = 'YBlog RSS Generator';
  77. /**
  78. +----------------------------------------------------------
  79. * RSS单条信息的数组
  80. +----------------------------------------------------------
  81. * @var string
  82. * @access protected
  83. +----------------------------------------------------------
  84. */
  85. protected $items = array();
  86. /**
  87. +----------------------------------------------------------
  88. * 构造函数
  89. +----------------------------------------------------------
  90. * @access public
  91. +----------------------------------------------------------
  92. * @param string $title RSS频道名
  93. * @param string $link RSS频道链接
  94. * @param string $description RSS频道描述
  95. * @param string $imgurl RSS频道图标
  96. +----------------------------------------------------------
  97. */
  98. public function __construct($title, $link, $description, $imgurl = '')
  99. {
  100. $this->channel_title = $title;
  101. $this->channel_link = $link;
  102. $this->channel_description = $description;
  103. $this->channel_imgurl = $imgurl;
  104. $this->pubDate = Date('Y-m-d H:i:s', time());
  105. $this->lastBuildDate = Date('Y-m-d H:i:s', time());
  106. }
  107. /**
  108. +----------------------------------------------------------
  109. * 设置私有变量
  110. +----------------------------------------------------------
  111. * @access public
  112. +----------------------------------------------------------
  113. * @param string $key 变量名
  114. * @param string $value 变量的值
  115. +----------------------------------------------------------
  116. */
  117. public function Config($key,$value)
  118. {
  119. $this->{$key} = $value;
  120. }
  121. /**
  122. +----------------------------------------------------------
  123. * 添加RSS项
  124. +----------------------------------------------------------
  125. * @access public
  126. +----------------------------------------------------------
  127. * @param string $title 日志的标题
  128. * @param string $link 日志的链接
  129. * @param string $description 日志的摘要
  130. * @param string $pubDate 日志的发布日期
  131. +----------------------------------------------------------
  132. */
  133. function AddItem($title, $link, $description, $pubDate)
  134. {
  135. $this->items[] = array('title' => $title, 'link' => $link, 'description' => $description, 'pubDate' => $pubDate);
  136. }
  137. /**
  138. +----------------------------------------------------------
  139. * 输出RSS的XML为字符串
  140. +----------------------------------------------------------
  141. * @access public
  142. +----------------------------------------------------------
  143. * @return string
  144. +----------------------------------------------------------
  145. */
  146. public function Fetch()
  147. {
  148. $rss = "<?xml version="1.0" encoding="utf-8" ?>rn";
  149. $rss = "<rss version="2.0">rn";
  150. $rss .= "<channel>rn";
  151. $rss .= "<title><![CDATA[{$this->channel_title}]]></title>rn";
  152. $rss .= "<description><![CDATA[{$this->channel_description}]]></description>rn";
  153. $rss .= "<link>{$this->channel_link}</link>rn";
  154. $rss .= "<language>{$this->language}</language>rn";
  155. if (!emptyempty($this->pubDate))
  156. $rss .= "<pubDate>{$this->pubDate}</pubDate>rn";
  157. if (!emptyempty($this->lastBuildDate))
  158. $rss .= "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>rn";
  159. if (!emptyempty($this->generator))
  160. $rss .= "<generator>{$this->generator}</generator>rn";
  161. $rss .= "<ttl>5</ttl>rn";
  162. if (!emptyempty($this->channel_imgurl)) {
  163. $rss .= "<image>rn";
  164. $rss .= "<title><![CDATA[{$this->channel_title}]]></title>rn";
  165. $rss .= "<link>{$this->channel_link}</link>rn";
  166. $rss .= "<url>{$this->channel_imgurl}</url>rn";
  167. $rss .= "</image>rn";
  168. }
  169. for ($i = 0; $i < count($this->items); $i++) {
  170. $rss .= "<item>rn";
  171. $rss .= "<title><![CDATA[{$this->items[$i]['title']}]]></title>rn";
  172. $rss .= "<link>{$this->items[$i]['link']}</link>rn";
  173. $rss .= "<description><![CDATA[{$this->items[$i]['description']}]]></description>rn";
  174. $rss .= "<pubDate>{$this->items[$i]['pubDate']}</pubDate>rn";
  175. $rss .= "</item>rn";
  176. }
  177. $rss .= "</channel>rn</rss>";
  178. return $rss;
  179. }
  180. /**
  181. +----------------------------------------------------------
  182. * 输出RSS的XML到浏览器
  183. +----------------------------------------------------------
  184. * @access public
  185. +----------------------------------------------------------
  186. * @return void
  187. +----------------------------------------------------------
  188. */
  189. public function Display()
  190. {
  191. header("Content-Type: text/xml; charset=utf-8");
  192. echo $this->Fetch();//开源代码phpfensi.com
  193. exit;
  194. }
  195. }
  196. ?>