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