XML制作RSS源

前端之家收集整理的这篇文章主要介绍了XML制作RSS源前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

什么是RSS源?看到这片文章的人相信都知道,自己博客首页不就是一个吗?


好吧,先来一个简单点的,直接就是死代码:具体怎样使用就看RSS使用标准吧!

  1. <?xml version = "1.0" encoding = "utf-8"?>
  2. <RSS version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/">
  3. <channel>
  4. <title>
  5. 优惠信息
  6. </title>
  7. <link>
  8. http://localhost/test/01.PHP
  9. </link>
  10. <description>
  11. 这里有最新优惠信息
  12. </description>
  13. </channel>
  14. <item>
  15. <title>
  16. 优惠信息1
  17. </title>
  18. <description>
  19. 这里有最新优惠信息1
  20. </description>
  21. </item>
  22. <item>
  23. <title>
  24. 优惠信息1
  25. </title>
  26. <description>
  27. 这里有最新优惠信息1
  28. </description>
  29. </item>
  30. </RSS>

效果:再火狐浏览器和ie显示正常,再google上显示不太好



这个简答实现了,来个复杂的吧,动态生成RSS

RSS模板

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- RSS输出模板 用PHP动态制造channel,和item -->
  3. <RSS version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/"></RSS>

动态生成
  1. <?PHP
  2. /***
  3. ====笔记部分====
  4. 连接数据库,动态生成RSS Feed
  5.  
  6. 数据库,取最新的10条商品,输出XML Feed
  7. ***/
  8.  
  9.  
  10. class Feed {
  11. public $title = ''; // channel的title
  12. public $link = ''; // channel的link
  13. public $description = ''; // channel的descrition;
  14. public $items = array();
  15.  
  16. public $template = './Feed.xml'; //xml模板
  17. protected $dom = null;
  18. protected $RSS = null;
  19.  
  20. public function __construct() {
  21. $this->dom = new DomDocument('1.0','utf-8');
  22. $this->dom->load($this->template);
  23. $this->RSS = $this->dom->getElementsByTagName('RSS')->item(0);
  24. }
  25.  
  26. // 调用createItem,把所有的item节点都生成,再输出
  27. public function display() {
  28. $this->createChannel();
  29. $this->addItem($this->items);
  30. header('content-type: text/xml');
  31. echo $this->dom->savexml();
  32. }
  33.  
  34. // 封装createChannel方法,用来创建RSS的唯一且必须的channel节点
  35. protected function createChannel() {
  36. $channel = $this->dom->createElement('channel');
  37. $channel->appendChild($this->createEle('title',$this->title));
  38. $channel->appendChild($this->createEle('link',$this->link));
  39. $channel->appendChild($this->createEle('description',$this->description));
  40.  
  41. $this->RSS->appendChild($channel);
  42. }
  43.  
  44. // 封装addItem方法,把所有的商品增加RSS里面去
  45. // $list是商品列表,是二维数据,每一行是一个商品
  46. protected function addItem($list) {
  47. foreach($list as $goods) {
  48. $this->RSS->appendChild($this->createItem($goods));
  49. }
  50. }
  51.  
  52. // 封装一个方法,用来造item 比如link description等等
  53. protected function createItem($arr) {
  54. $item = $this->dom->createElement('item');
  55. foreach($arr as $k=>$v) {
  56. $item->appendChild($this->createEle($k,$v));
  57. }
  58.  
  59. return $item;
  60. }
  61. // 封装一个方法,直接创建开如 <ele>some text</ele>这样的节点
  62. protected function createEle($name,$value) {
  63. $ele = $this->dom->createElement($name);
  64. $text = $this->dom->createTextNode($value);
  65. $ele->appendChild($text);
  66.  
  67. return $ele;
  68. }
  69. }
  70.  
  71.  
  72. $conn = MysqL_connect('localhost','root','');
  73. MysqL_query('set names utf8',$conn);
  74. MysqL_query('use test');
  75.  
  76. $sql = 'select ver as title,content as description from smth order by id desc limit 8';
  77.  
  78. $rs = MysqL_query($sql,$conn);
  79.  
  80. $list = array();
  81. while($row = MysqL_fetch_assoc($rs)) {
  82. $list[] = $row;
  83. }
  84.  
  85.  
  86. $Feed = new Feed();
  87. $Feed->title = '布尔商城';
  88. $Feed->link = 'http://localhost/bool';
  89. $Feed->description = '这是商城的优惠信息集合';
  90. $Feed->items = $list;
  91. $Feed->display();

生成效果,并查看源码:

猜你在找的XML相关文章