国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > PHP > 正文

php實(shí)現(xiàn)的RSS生成類實(shí)例

2024-05-04 23:34:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這篇文章主要介紹了php實(shí)現(xiàn)的RSS生成類,實(shí)例分析了RSS生成類的原理、定義與使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了php實(shí)現(xiàn)的RSS生成類。分享給大家供大家參考。具體如下:

 

 
  1. class RSS 
  2. var $title
  3. var $link
  4. var $description
  5. var $language = "en-us"
  6. var $pubDate
  7. var $items
  8. var $tags
  9. function RSS() 
  10. $this->items = array(); 
  11. $this->tags = array(); 
  12. function addItem($item
  13. $this->items[] = $item
  14. function setPubDate($when
  15. if(strtotime($when) == false) 
  16. $this->pubDate = date("D, d M Y H:i:s "$when) . "GMT"
  17. else 
  18. $this->pubDate = date("D, d M Y H:i:s "strtotime($when)) . "GMT"
  19. function getPubDate() 
  20. if(emptyempty($this->pubDate)) 
  21. return date("D, d M Y H:i:s ") . "GMT"
  22. else 
  23. return $this->pubDate; 
  24. function addTag($tag$value
  25. $this->tags[$tag] = $value
  26. function out() 
  27. $out = $this->header(); 
  28. $out .= "<channel>/n"
  29. $out .= "<title>" . $this->title . "</title>/n"
  30. $out .= "<link>" . $this->link . "</link>/n"
  31. $out .= "<description>" . $this->description . "</description>/n"
  32. $out .= "<language>" . $this->language . "</language>/n"
  33. $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>/n"
  34. foreach($this->tags as $key => $val$out .= "<$key>$val</$key>/n"
  35. foreach($this->items as $item$out .= $item->out(); 
  36. $out .= "</channel>/n"
  37. $out .= $this->footer(); 
  38. $out = str_replace("&""&"$out); 
  39. return $out
  40. function serve($contentType = "application/xml"
  41. $xml = $this->out(); 
  42. header("Content-type: $contentType"); 
  43. echo $xml
  44. function header() 
  45. $out = '<?xml version="1.0" encoding="utf-8"?>' . "/n"
  46. $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">' . "/n"
  47. return $out
  48. function footer() 
  49. return '</rss>'
  50. class RSSItem 
  51. var $title
  52. var $link
  53. var $description
  54. var $pubDate
  55. var $guid
  56. var $tags
  57. var $attachment
  58. var $length
  59. var $mimetype
  60. function RSSItem() 
  61. {  
  62. $this->tags = array(); 
  63. function setPubDate($when
  64. if(strtotime($when) == false) 
  65. $this->pubDate = date("D, d M Y H:i:s "$when) . "GMT"
  66. else 
  67. $this->pubDate = date("D, d M Y H:i:s "strtotime($when)) . "GMT"
  68. function getPubDate() 
  69. if(emptyempty($this->pubDate)) 
  70. return date("D, d M Y H:i:s ") . "GMT"
  71. else 
  72. return $this->pubDate; 
  73. function addTag($tag$value
  74. $this->tags[$tag] = $value
  75. function out() 
  76. $out .= "<item>/n"
  77. $out .= "<title>" . $this->title . "</title>/n"
  78. $out .= "<link>" . $this->link . "</link>/n"
  79. $out .= "<description>" . $this->description . "</description>/n"
  80. $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>/n"
  81. if($this->attachment != ""
  82. $out .= "<enclosure url='{$this->attachment}' length='{$this->length}' type='{$this->mimetype}' />"
  83. if(emptyempty($this->guid)) $this->guid = $this->link; 
  84. $out .= "<guid>" . $this->guid . "</guid>/n"
  85.  
  86. foreach($this->tags as $key => $val$out .= "<$key>$val</$key/n>"
  87. $out .= "</item>/n"
  88. return $out
  89. function enclosure($url$mimetype$length
  90. $this->attachment = $url
  91. $this->mimetype = $mimetype
  92. $this->length = $length

使用示例如下:

 

 
  1. $feed = new RSS(); 
  2. $feed->title = "RSS Feed Title"
  3. $feed->link = "http://website.com"
  4. $feed->description = "Recent articles on your website."
  5. $db->query($query); 
  6. $result = $db->result; 
  7. while($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
  8. $item = new RSSItem(); 
  9. $item->title = $title
  10. $item->link = $link
  11. $item->setPubDate($create_date);  
  12. $item->description = "<![CDATA[ $html ]]>"
  13. $feed->addItem($item); 
  14. echo $feed->serve(); 

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新昌县| 阳新县| 宁夏| 浏阳市| 德庆县| 桂阳县| 伽师县| 富锦市| 治县。| 勃利县| 阿克| 瑞丽市| 贞丰县| 江津市| 女性| 中超| 东明县| 迁西县| 南召县| 历史| 淮滨县| 白玉县| 武强县| 浏阳市| 渑池县| 绥中县| 玉溪市| 扎囊县| 城口县| 信丰县| 兰溪市| 墨玉县| 高要市| 新乐市| 勐海县| 平和县| 余江县| 醴陵市| 东台市| 漠河县| 军事|