PHP RSS 生成类

同时也可以用网页来解析xml文档,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',"http://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. //开源代码phpfensi.com
  80. function savetofile($fname) {
  81. if (emptyempty($this->content)) $this->buildrss();
  82. $handle = fopen($fname, 'wb');
  83. if ($handle === false) return false;
  84. fwrite($handle, $this->content);
  85. fclose($handle);
  86. }
  87. }
  88. ?>