当前位置:首页 > PHP教程 > php类库 > 列表

php完美的rss 生成类

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-29 13:54:34 浏览: 评论:0 

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. <?php 
  2. // +---------------------------------------------------------------------- 
  3. // | YBlog 
  4. // +---------------------------------------------------------------------- 
  5. // | Copyright (c) 2008 http://www.111cn.net/nokia/n97/ All rights reserved. 
  6. // +---------------------------------------------------------------------- 
  7. // +---------------------------------------------------------------------- 
  8. // | Author: yhustc <yhustc@gmail.com> 
  9. // +---------------------------------------------------------------------- 
  10. // $Id$ 
  11.  
  12. /** 
  13.  +------------------------------------------------------------------------------ 
  14.  * RSS生成类 
  15.  +------------------------------------------------------------------------------ 
  16.  * @author    yhustc <yhustc@gmail.com> 
  17.  * @version   $Id$ 
  18.  +------------------------------------------------------------------------------ 
  19.  */ 
  20. class RSS 
  21. { 
  22.     /** 
  23.      +---------------------------------------------------------- 
  24.      * RSS频道名 
  25.      +---------------------------------------------------------- 
  26.      * @var string 
  27.      * @access protected 
  28.      +---------------------------------------------------------- 
  29.      */ 
  30.     protected $channel_title = ''; 
  31.     /** 
  32.      +---------------------------------------------------------- 
  33.      * RSS频道链接 
  34.      +---------------------------------------------------------- 
  35.      * @var string 
  36.      * @access protected 
  37.      +---------------------------------------------------------- 
  38.      */ 
  39.     protected $channel_link = ''; 
  40.     /** 
  41.      +---------------------------------------------------------- 
  42.      * RSS频道描述 
  43.      +---------------------------------------------------------- 
  44.      * @var string 
  45.      * @access protected 
  46.      +---------------------------------------------------------- 
  47.      */ 
  48.     protected $channel_description = ''; 
  49.     /** 
  50.      +---------------------------------------------------------- 
  51.      * RSS频道使用的小图标的URL 
  52.      +---------------------------------------------------------- 
  53.      * @var string 
  54.      * @access protected 
  55.      +---------------------------------------------------------- 
  56.      */ 
  57.     protected $channel_imgurl = ''; 
  58.     /** 
  59.      +---------------------------------------------------------- 
  60.      * RSS频道所使用的语言 
  61.      +---------------------------------------------------------- 
  62.      * @var string 
  63.      * @access protected 
  64.      +---------------------------------------------------------- 
  65.      */ 
  66.     protected $language = 'zh_CN'; 
  67.     /** 
  68.      +---------------------------------------------------------- 
  69.      * RSS文档创建日期,默认为今天 
  70.      +---------------------------------------------------------- 
  71.      * @var string 
  72.      * @access protected 
  73.      +---------------------------------------------------------- 
  74.      */ 
  75.     protected $pubDate = ''; 
  76.     protected $lastBuildDate = ''; 
  77.  
  78.     protected $generator = 'YBlog RSS Generator'; 
  79.  
  80.     /** 
  81.      +---------------------------------------------------------- 
  82.      * RSS单条信息的数组 
  83.      +---------------------------------------------------------- 
  84.      * @var string 
  85.      * @access protected 
  86.      +---------------------------------------------------------- 
  87.      */ 
  88.     protected $items = array(); 
  89.  
  90.     /** 
  91.      +---------------------------------------------------------- 
  92.      * 构造函数 
  93.      +---------------------------------------------------------- 
  94.      * @access public  
  95.      +---------------------------------------------------------- 
  96.      * @param string $title  RSS频道名 
  97.      * @param string $link  RSS频道链接 
  98.      * @param string $description  RSS频道描述 
  99.      * @param string $imgurl  RSS频道图标 
  100.      +---------------------------------------------------------- 
  101.      */ 
  102.     public function __construct($title, $link, $description, $imgurl = '') 
  103.     { 
  104.         $this->channel_title = $title; 
  105.         $this->channel_link = $link; 
  106.         $this->channel_description = $description; 
  107.         $this->channel_imgurl = $imgurl; 
  108.         $this->pubDate = Date('Y-m-d H:i:s', time()); 
  109.         $this->lastBuildDate = Date('Y-m-d H:i:s', time()); 
  110.     } 
  111.  
  112.     /** 
  113.      +---------------------------------------------------------- 
  114.      * 设置私有变量 
  115.      +---------------------------------------------------------- 
  116.      * @access public  
  117.      +---------------------------------------------------------- 
  118.      * @param string $key  变量名 
  119.      * @param string $value  变量的值 
  120.      +---------------------------------------------------------- 
  121.      */ 
  122.      public function Config($key,$value) 
  123.      { 
  124.         $this->{$key} = $value; 
  125.      } 
  126.  
  127.     /** 
  128.      +---------------------------------------------------------- 
  129.      * 添加RSS项 
  130.      +---------------------------------------------------------- 
  131.      * @access public  
  132.      +---------------------------------------------------------- 
  133.      * @param string $title  日志的标题 
  134.      * @param string $link  日志的链接 
  135.      * @param string $description  日志的摘要 
  136.      * @param string $pubDate  日志的发布日期 
  137.      +---------------------------------------------------------- 
  138.      */ 
  139.      function AddItem($title, $link, $description, $pubDate) 
  140.      { 
  141.         $this->items[] = array('title' => $title, 'link' => $link, 'description' => $description, 'pubDate' => $pubDate); 
  142.      } 
  143.  
  144.      /** 
  145.      +---------------------------------------------------------- 
  146.      * 输出RSS的XML为字符串 
  147.      +---------------------------------------------------------- 
  148.      * @access public  
  149.      +---------------------------------------------------------- 
  150.      * @return string 
  151.      +---------------------------------------------------------- 
  152.      */ 
  153.     public function Fetch() 
  154.     { 
  155.         $rss = "<?xml version="1.0" encoding="utf-8" ?>rn"; 
  156.         $rss = "<rss version="2.0">rn"; 
  157.         $rss .= "<channel>rn"; 
  158.         $rss .= "<title><![CDATA[{$this->channel_title}]]></title>rn"; 
  159.         $rss .= "<description><![CDATA[{$this->channel_description}]]></description>rn"; 
  160.         $rss .= "<link>{$this->channel_link}</link>rn"; 
  161.         $rss .= "<language>{$this->language}</language>rn"; 
  162.  
  163.         if (!emptyempty($this->pubDate)) 
  164.             $rss .= "<pubDate>{$this->pubDate}</pubDate>rn"; 
  165.         if (!emptyempty($this->lastBuildDate)) 
  166.             $rss .= "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>rn"; 
  167.         if (!emptyempty($this->generator)) 
  168.             $rss .= "<generator>{$this->generator}</generator>rn"; 
  169.  
  170.         $rss .= "<ttl>5</ttl>rn"; 
  171.  
  172.         if (!emptyempty($this->channel_imgurl)) { 
  173.             $rss .= "<image>rn"; 
  174.             $rss .= "<title><![CDATA[{$this->channel_title}]]></title>rn"; 
  175.             $rss .= "<link>{$this->channel_link}</link>rn"; 
  176.             $rss .= "<url>{$this->channel_imgurl}</url>rn"; 
  177.             $rss .= "</image>rn"; 
  178.         } 
  179.  
  180.         for ($i = 0; $i < count($this->items); $i++) { 
  181.             $rss .= "<item>rn"; 
  182.             $rss .= "<title><![CDATA[{$this->items[$i]['title']}]]></title>rn"; 
  183.             $rss .= "<link>{$this->items[$i]['link']}</link>rn"; 
  184.             $rss .= "<description><![CDATA[{$this->items[$i]['description']}]]></description>rn"; 
  185.             $rss .= "<pubDate>{$this->items[$i]['pubDate']}</pubDate>rn"; 
  186.             $rss .= "</item>rn"; 
  187.         } 
  188.  
  189.         $rss .= "</channel>rn</rss>"; 
  190.         return $rss; 
  191.     } 
  192.  
  193.     /** 
  194.      +---------------------------------------------------------- 
  195.      * 输出RSS的XML到浏览器 
  196.      +---------------------------------------------------------- 
  197.      * @access public  
  198.      +---------------------------------------------------------- 
  199.      * @return void 
  200.      +---------------------------------------------------------- 
  201.      */ 
  202.     public function Display() 
  203.     { 
  204.         header("Content-Type: text/xml; charset=utf-8"); 
  205.         echo $this->Fetch();//开源代码phpfensi.com 
  206.         exit; 
  207.     } 
  208. } 
  209. ?> 

Tags: php rss生成类

分享到: