PHP生成RSS文件类实例

这篇文章主要介绍了PHP生成RSS文件类,可实现PHP生成RSS文件的功能,对于网站建设与优化来说具有一定的实用价值,需要的朋友可以参考下

本文实例讲述了PHP生成RSS文件类文件。分享给大家供大家参考。具体如下:

PHP RSS 生成类实例代码如下:

  1. <?php
  2. if (defined('_class_rss_php')) return;
  3. define('_class_rss_php粉丝网',1);
  4. /**
  5. * 使用说明:
  6. * $rss = new rss('redfox','http://phpfensi.com/',"redfox's blog");
  7. * $rss->additem('rss class',"https://www.phpfensi.com","xxx",date());
  8. * $rss->additem(...);
  9. * $rss->savetofile(...);
  10. */
  11. class rss {
  12. //public
  13. $rss_ver = "2.0";
  14. $channel_title = '';
  15. $channel_link = '';
  16. $channel_description = '';
  17. $language = 'zh_cn';
  18. $copyright = '';
  19. $webmaster = '';
  20. $pubdate = '';
  21. $lastbuilddate = '';
  22. $generator = 'redfox rss generator';
  23. $content = '';
  24. $items = array();
  25. function rss($title, $link, $description) {
  26. $this->channel_title = $title;
  27. $this->channel_link = $link;
  28. $this->channel_description = $description;
  29. $this->pubdate = date('y-m-d h:i:s',time());
  30. $this->lastbuilddate = date('y-m-d h:i:s',time());
  31. }
  32. function additem($title, $link, $description ,$pubdate) {
  33. $this->items[] = array('titile' => $title ,
  34. 'link' => $link,
  35. 'description' => $description,
  36. 'pubdate' => $pubdate);
  37. }
  38. function buildrss() {
  39. $s = "<!--l version="1.0" encoding="gb2312"--> ";
  40. // start channel
  41. $s .= " ";
  42. $s .= " "
  43. $s .= "<link />{$this->channel_link} ";
  44. $s .= "{$this->channel_description} ";
  45. $s .= "{$this->language} ";
  46. if (!emptyempty($this->copyright)) {
  47. $s .= "{$this->copyright} ";
  48. }
  49. if (!emptyempty($this->webmaster)) {
  50. $s .= "{$this->webmaster} ";
  51. }
  52. if (!emptyempty($this->pubdate)) {
  53. $s .= "{$this->pubdate} ";
  54. }
  55. if (!emptyempty($this->lastbuilddate)) {
  56. $s .= "{$this->lastbuilddate} ";
  57. }
  58. if (!emptyempty($this->generator)) {
  59. $s .= "{$this->generator} ";
  60. }
  61. // start items
  62. for ($i=0;$iitems),$i++) {
  63. $s .= " ";
  64. $s .= " ";
  65. $s .= "<link />{$this->items[$i]['link']} ";
  66. $s .= "<!--data[{$thi-->items[$i]['description']}]]> ";
  67. $s .= "{$this->items[$i]['pubdate']} ";
  68. $s .= " ";
  69. }
  70. // close channel
  71. $s .= " ";
  72. $this->content = $s;
  73. }
  74. function show() {
  75. if (emptyempty($this->content)) $this->buildrss();
  76. header('content-type:text/xml');
  77. echo($this->content);
  78. }
  79. function savetofile($fname) {
  80. if (emptyempty($this->content)) $this->buildrss();
  81. $handle = fopen($fname, 'wb');
  82. if ($handle === false) return false;
  83. fwrite($handle, $this->content);
  84. fclose($handle);
  85. }
  86. }
  87. ?>

希望本文所述对大家的PHP程序设计有所帮助。