Rss.class.php 8.2 KB

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