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

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

复制代码 代码如下:
<?php
if (defined(‘_class_rss_php’)) return;
define(‘_class_rss_php教程’,1);
/**

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

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

更多相关文章

  1. 初识html标签
  2. php判断数组元素不为空格实例代码
  3. JavaScript onblur与onfocus事件详解
  4. vue_1
  5. 代码优化-表驱动编程
  6. 数组循环取模及代码实现计算器
  7. php编程0805作业
  8. 记一下网站被攻击经历
  9. webpack

随机推荐

  1. Spring Cloud第四章:熔断器Hystrix
  2. SpringBoot第二十一篇: springboot集成JMS
  3. 计算机领域中的熔断机制
  4. Spring Cloud第五章:服务网关Zuul
  5. 大华网络摄像机SDK Linux系统对接
  6. 安装CentOS
  7. Spring Cloud第六章:配置中心Config
  8. Java代理模式
  9. 2021.1.13
  10. Spring Cloud第七篇:高可用的配置中心