zoukankan      html  css  js  c++  java
  • PHP商城RSS订阅源类开发详解(原创)

    一、RSS简介  
          RSS(ReallySimple Syndication-简易供稿),是一种网页内容联合格式(web contentsydication format)。RSS是XML的一种。所有的RSS文档都遵循XML 1.0规范,该规范发布在W3C网站上。
     
          在一个RSS文档的开头是一个<rss>节点和一个规定的属性version,该属性规定了该文档将以RSS的哪个版本表示。如果该文档以这个规范来表示,那么它的version属性就必须等于2.0。
          在<rss>节点的下一级是一个独立的<channel>节点,该节点包含关于channel的信息和内容。
          内容信息使用< item>节点表示,< item>的子节点有title、link、author、pubDate、description。 
     
            rss:每个RSSFeed,都有而且只能有一个rss标签,作为顶层元素
            channel:在RSS标签下,必须有且只能有一个channel标签
            item:可以出现多个item,每个item,描述一条日志信息
            title:日志的标题
            link:日志的URL访问地址
            author:日志的作者
            pubDate:日志的发布日期
            description:日志的内容 
    注:具体的语法内容,可以参考 http://wenku.baidu.com/view/bcb222eeaeaad1f346933f64.html



    二、RSS格式参考模板

     1 <?xml version="1.0" encoding="utf-8" ?> 
     2 
     3 <rss version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/">
     4 
     5         <channel>
     6 
     7                     <title>标题></title>
     8 
     9                     <link>链接地址</link>
    10 
    11                     <description>描述</description>
    12 
    13                     <language>描述语言</language>
    14 
    15                     <copyright>版本</copyright>
    16 
    17                     <pubDate>21 Oct 2008 01:43:15 GMT</pubDate>
    18 
    19                     < item>
    20 
    21                                 <title>日志的标题1</title>
    22 
    23                                 <link>日志的URL访问地址</link>
    24 
    25                                 <author>日志的作者</author>
    26 
    27                                 <pubDate>日志的发布日期</pubDate>
    28 
    29                                 <description>日志的内容</description>
    30 
    31                     </item>        
    32 
    33                     < item>
    34 
    35                                 <title>日志的标题2</title>
    36 
    37                                 <link>日志的URL访问地址</link>
    38 
    39                                 <author>日志的作者</author>
    40 
    41                                  <pubDate>日志的发布日期</pubDate>
    42 
    43                                 <description>日志的内容</description>
    44 
    45                     </item>
    46 
    47         </channel>
    48 
    49 </rss>

     三、RSS错误分析

            有时,我们的RSS代码写的是正确的,即单独运行时有用,可是一旦与PHP结合,便会报以下错误,这个问题困扰了我一个上午:
     图片
    据我经验了解,由于我们编写代码的习惯,我们写PHP程序是,经常会使用稳妥的办法,使用echo xxx,来调试代码,这既是此处报错的原因,可能是因为RSS的代码前面不得有任何的输出(echo xxx);    
    同时,php文件的<?php 前不得有空行或者输出内容
                                                        ------此处仅代表本人个人 根据经验总结的思想,还望前辈们不吝赐教

    四、PHP商城 RS订阅源实战

    (1)首先我们编写一个RS模板  template.xml,如下:

    <?xml version="1.0" encoding="utf-8"?>

    <rss version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/"></rss>

    图片


    (2):编写好模板,我们就可以正式开发RS订阅源类了   feed_class.php,代码如下:

     
     

    (3)好了,此时,我们完成了 RSS模板 和 RSS类的开发,开启Apache 服务器,在浏览器上运行我们的feed_class.php,结果图如下:
    正常的结果如下: 
     
    图片


    在未安装RSS阅读器上,显示的是xml文件代码,结果去下:比如说GOOGLE的Chrome(解决的办法很简单,安装RSS阅读器插件即可)

    图片

     1 <?php
     2  
     3 //连接数据库,动态生成RSS feed
     4 //连接数据库,取得最新的10条商品,输出XML feed
     5  
     6 class feed_class{
     7  
     8         public $title = '';                 //channel的title
     9         public $link  = '';                //channel的link
    10         public $description = '';     //channel的description
    11         public $itemlist = array();
    12  
    13         public $template = '';        //xml模板
    14         protected $dom = null;
    15         protected $rss = null;
    16         protected $channel = null;
    17  
    18         public function __construct(){
    19             $this->dom = new DomDocument("1.0","utf-8");
    20             $this->dom->load('./template.xml');
    21             $this->dom->formatOutput = true;
    22             $this->rss = $this->dom->getElementsByTagName('rss')->item(0);
    23         }
    24  
    25     //调用createItem,把所有的item节点生成,再输出
    26         public function display(){
    27             $this->createChannel();
    28             $this->addItem($this->itemlist);
    29             header('content-type: text/xml');
    30             echo $this->dom->saveXML();
    31             //$this->dom->save('01.xml');        //如需将代码保存,则打开此语句
    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             $this->channel = $channel;
    41             $this->rss->appendChild($channel);
    42         }
    43  
    44         //封装addItem方法,把所有的商品增加到RSS中去
    45         //$list是商品列表,是二维数组,每一行都是一个商品
    46         public function addItem($list){
    47             foreach($list as $goods){
    48                    $this->channel->appendChild($this->createItem($goods));
    49             }
    50         }
    51  
    52         //封装一个类用于造Item
    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             return $item;
    59         }
    60  
    61         //快速创建节点  类似:<name>lover雪</name>
    62         public 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 //连接数据库
    73 $conn = mysql_connect('localhost','root','');
    74  
    75  
    76 mysql_query('set names utf8',$conn);
    77 mysql_query('use boolshop1');
    78 
    79 
    80  
    81 $sql = "select goods_name as title,goods_brief as description from goods order by add_time asc limit 10";
    82 $rs = mysql_query($sql,$conn);
    83  
    84  
    85 $list = array();
    86 while($row = mysql_fetch_assoc($rs)){
    87     $list[] = $row;
    88 }
    89  
    90 $feed1 = new feed_class();
    91 $feed1->title = "布尔商城";
    92 $feed1->link = "http://127.0.0.1/boolshop/";
    93 $feed1->description = "这个一个好商城";
    94 $feed1->itemlist = $list;
    95  
    96 $feed1->display();
    97  
    98 ?>


    五、总结

        RSS 决不是一个完美的格式,但是它现在已经非常流行,并得到广泛的支持。要成为一个固定的规范,RSS需要很长的一段时间。
        编写RSS的xml文件时,由于浏览器的严格解析模式,一旦出现一点点错误,都不会显示任何效果,此时需要足够的耐心去找到错误,并且修正它,
        须特别注意,在RSS代码前不得有任何的文字或者其他信息输出,一旦把这点忘记了,你才会明白,这个错误查找起来是有多么痛苦(因为本人就是犯了这个错误,花了一个上午不断的在调试,过程很痛苦,因为代码根本就没错,也就是说根本就没法查错,有点类似感慨是接触编程时的逻辑错误,没法查)。





  • 相关阅读:
    C# 多线程 弹出模态MessageBox的一种方法
    Selection.Delete Shift:=xlUp
    Selection.Delete Shift:=xlUp
    C# 源码
    C# 源码
    C# string格式的日期时间字符串转为DateTime类型的方法
    C# string格式的日期时间字符串转为DateTime类型的方法
    在Excel VBA中,单元格的.interior.color的值是什么格式的?
    在Excel VBA中,单元格的.interior.color的值是什么格式的?
    C# 如果分配给命令的连接位于本地挂起事务中,ExecuteNonQuery 要求命令拥有事务。命令的 Transaction 属性尚未初始化
  • 原文地址:https://www.cnblogs.com/lihaiyan/p/4274367.html
Copyright © 2011-2022 走看看