php专区

 首页 > php专区 > PHP应用 > php类库 > php完美的rss 生成类 - php类库

php完美的rss 生成类 - php类库

分享到:
【字体:
导读:
          RSS订阅功能,在很多网站都可以有但也有很多,下面代码是自己写的,其中使用到了一个PHP类:RSS.class.php,感觉非常方便,不敢独享,特拿出来跟大家分享.类的调用代码如下:include_once(class/RSS.c...

php完美的rss 生成类

RSS订阅功能,在很多网站都可以有但也有很多,下面代码是自己写的,其中使用到了一个PHP类:RSS.class.php,感觉非常方便,不敢独享,特拿出来跟大家分享.

类的调用代码如下:

  1. include_once("class/RSS.class.php");//引入RSS PHP类 
  2. $RSSnew RSS("名称","地址","描述","RSS频道图标"); 
  3. $RSS->AddItem("日志的标题","日志的地址","日志的摘要","日志的发布日期"); 
  4. $RSS->Display();//输出RSS内容 

全部代码如下:

  1. // +---------------------------------------------------------------------- 
  2. // | YBlog 
  3. // +---------------------------------------------------------------------- 
  4. // | Copyright (c) 2008 http://www.111cn.net/nokia/n97/ All rights reserved. 
  5. // +---------------------------------------------------------------------- 
  6. // +---------------------------------------------------------------------- 
  7. // | Author: yhustc  
  8. // +---------------------------------------------------------------------- 
  9. // $Id$ 
  10.  
  11. /** 
  12.  +------------------------------------------------------------------------------ 
  13.  * RSS生成类 
  14.  +------------------------------------------------------------------------------ 
  15.  * @author    yhustc  
  16.  * @version   $Id$ 
  17.  +------------------------------------------------------------------------------ 
  18.  */ 
  19. class RSS 
  20.     /** 
  21.      +---------------------------------------------------------- 
  22.      * RSS频道名 
  23.      +---------------------------------------------------------- 
  24.      * @var string 
  25.      * @access protected 
  26.      +---------------------------------------------------------- 
  27.      */ 
  28.     protected $channel_title = ''
  29.     /** 
  30.      +---------------------------------------------------------- 
  31.      * RSS频道链接 
  32.      +---------------------------------------------------------- 
  33.      * @var string 
  34.      * @access protected 
  35.      +---------------------------------------------------------- 
  36.      */ 
  37.     protected $channel_link = ''
  38.     /** 
  39.      +---------------------------------------------------------- 
  40.      * RSS频道描述 
  41.      +---------------------------------------------------------- 
  42.      * @var string 
  43.      * @access protected 
  44.      +---------------------------------------------------------- 
  45.      */ 
  46.     protected $channel_description = ''
  47.     /** 
  48.      +---------------------------------------------------------- 
  49.      * RSS频道使用的小图标的URL 
  50.      +---------------------------------------------------------- 
  51.      * @var string 
  52.      * @access protected 
  53.      +---------------------------------------------------------- 
  54.      */ 
  55.     protected $channel_imgurl = ''
  56.     /** 
  57.      +---------------------------------------------------------- 
  58.      * RSS频道所使用的语言 
  59.      +---------------------------------------------------------- 
  60.      * @var string 
  61.      * @access protected 
  62.      +---------------------------------------------------------- 
  63.      */ 
  64.     protected $language = 'zh_CN'
  65.     /** 
  66.      +---------------------------------------------------------- 
  67.      * RSS文档创建日期,默认为今天 
  68.      +---------------------------------------------------------- 
  69.      * @var string 
  70.      * @access protected 
  71.      +---------------------------------------------------------- 
  72.      */ 
  73.     protected $pubDate = ''
  74.     protected $lastBuildDate = ''
  75.  
  76.     protected $generator = 'YBlog RSS Generator'
  77.  
  78.     /** 
  79.      +---------------------------------------------------------- 
  80.      * RSS单条信息的数组 
  81.      +---------------------------------------------------------- 
  82.      * @var string 
  83.      * @access protected 
  84.      +---------------------------------------------------------- 
  85.      */ 
  86.     protected $items = array(); 
  87.  
  88.     /** 
  89.      +---------------------------------------------------------- 
  90.      * 构造函数 
  91.      +---------------------------------------------------------- 
  92.      * @access public  
  93.      +---------------------------------------------------------- 
  94.      * @param string $title  RSS频道名 
  95.      * @param string $link  RSS频道链接 
  96.      * @param string $description  RSS频道描述 
  97.      * @param string $imgurl  RSS频道图标 
  98.      +---------------------------------------------------------- 
  99.      */ 
  100.     public function __construct($title$link$description$imgurl = ''
  101.     { 
  102.         $this->channel_title = $title
  103.         $this->channel_link = $link
  104.         $this->channel_description = $description
  105.         $this->channel_imgurl = $imgurl
  106.         $this->pubDate = Date('Y-m-d H:i:s', time()); 
  107.         $this->lastBuildDate = Date('Y-m-d H:i:s', time()); 
  108.     } 
  109.  
  110.     /** 
  111.      +---------------------------------------------------------- 
  112.      * 设置私有变量 
  113.      +---------------------------------------------------------- 
  114.      * @access public  
  115.      +---------------------------------------------------------- 
  116.      * @param string $key  变量名 
  117.      * @param string $value  变量的值 
  118.      +---------------------------------------------------------- 
  119.      */ 
  120.      public function Config($key,$value
  121.      { 
  122.         $this->{$key} = $value
  123.      } 
  124.  
  125.     /** 
  126.      +---------------------------------------------------------- 
  127.      * 添加RSS项 
  128.      +---------------------------------------------------------- 
  129.      * @access public  
  130.      +---------------------------------------------------------- 
  131.      * @param string $title  日志的标题 
  132.      * @param string $link  日志的链接 
  133.      * @param string $description  日志的摘要 
  134.      * @param string $pubDate  日志的发布日期 
  135.      +---------------------------------------------------------- 
  136.      */ 
  137.      function AddItem($title$link$description$pubDate
  138.      { 
  139.         $this->items[] = array('title' => $title'link' => $link'description' => $description'pubDate' => $pubDate); 
  140.      } 
  141.  
  142.      /** 
  143.      +---------------------------------------------------------- 
  144.      * 输出RSS的XML为字符串 
  145.      +---------------------------------------------------------- 
  146.      * @access public  
  147.      +---------------------------------------------------------- 
  148.      * @return string 
  149.      +---------------------------------------------------------- 
  150.      */ 
  151.     public function Fetch() 
  152.     { 
  153.         $rss = "1.0" encoding="utf-8" ?>rn"
  154.         $rss = "2.0">rn"
  155.         $rss .= "rn"
  156.         $rss .= "<![CDATA[{$this->channel_title}]]>rn"
  157.         $rss .= "channel_description}]]>rn"
  158.         $rss .= "{$this->channel_link}rn"
  159.         $rss .= "{$this->language}rn"
  160.  
  161.         if (!emptyempty($this->pubDate)) 
  162.             $rss .= "{$this->pubDate}rn"
  163.         if (!emptyempty($this->lastBuildDate)) 
  164.             $rss .= "{$this->lastBuildDate}rn"
  165.         if (!emptyempty($this->generator)) 
  166.             $rss .= "{$this->generator}rn"
  167.  
  168.         $rss .= "5rn"
  169.  
  170.         if (!emptyempty($this->channel_imgurl)) { 
  171.             $rss .= "rn"
  172.             $rss .= "<![CDATA[{$this->channel_title}]]>rn"
  173.             $rss .= "{$this->channel_link}rn"
  174.             $rss .= "{$this->channel_imgurl}rn"
  175.             $rss .= "rn"
  176.         } 
  177.  
  178.         for ($i = 0; $i < count($this->items); $i++) { 
  179.             $rss .= "rn"
  180.             $rss .= "<![CDATA[{$this->items[$i]['title']}]]>rn"
  181.             $rss .= "{$this->items[$i]['link']}rn"
  182.             $rss .= "items[$i]['description']}]]>rn"
  183.             $rss .= "{$this->items[$i]['pubDate']}rn"
  184.             $rss .= "rn"
  185.         } 
  186.  
  187.         $rss .= "rn"
  188.         return $rss
  189.     } 
  190.  
  191.     /** 
  192.      +---------------------------------------------------------- 
  193.      * 输出RSS的XML到浏览器 
  194.      +---------------------------------------------------------- 
  195.      * @access public  
  196.      +---------------------------------------------------------- 
  197.      * @return void 
  198.      +---------------------------------------------------------- 
  199.      */ 
  200.     public function Display() 
  201.     { 
  202.         header("Content-Type: text/xml; charset=utf-8"); 
  203.         echo $this->Fetch();//开源代码phpfensi.com 
  204.         exit
  205.     } 
  206. ?> 
分享到:
PHP获取汉字的拼音字母实现程序 - php类...
PHP获取汉字的拼音字母实现程序 一个完整的php获取汉字拼音字母的实现程序,有需要的朋友可参考一下,php实例代码如下:  
php _autoload自动加载类与机制分析 - ph...
php _autoload自动加载类与机制分析 在使用PHP的OO模式开发系统时,通常大家习惯上将每个类的实现都存放在一个单独的文件里,这样会很容易实现对类进行复用,同时将来维护时也很便利,这也是OO设计的基本思想之一,在PHP5之前,如果需要使用一个类,只需要直接使用include/require将其包含进来即可. ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……