VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 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. <?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.      * 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 = "<?xml version="1.0" encoding="utf-8" ?>rn"
  155.         $rss = "<rss version="2.0">rn"
  156.         $rss .= "<channel>rn"
  157.         $rss .= "<title><![CDATA[{$this->channel_title}]]></title>rn"
  158.         $rss .= "<description><![CDATA[{$this->channel_description}]]></description>rn"
  159.         $rss .= "<link>{$this->channel_link}</link>rn"
  160.         $rss .= "<language>{$this->language}</language>rn"
  161.  
  162.         if (!emptyempty($this->pubDate)) 
  163.             $rss .= "<pubDate>{$this->pubDate}</pubDate>rn"
  164.         if (!emptyempty($this->lastBuildDate)) 
  165.             $rss .= "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>rn"
  166.         if (!emptyempty($this->generator)) 
  167.             $rss .= "<generator>{$this->generator}</generator>rn"
  168.  
  169.         $rss .= "<ttl>5</ttl>rn"
  170.  
  171.         if (!emptyempty($this->channel_imgurl)) { 
  172.             $rss .= "<image>rn"
  173.             $rss .= "<title><![CDATA[{$this->channel_title}]]></title>rn"
  174.             $rss .= "<link>{$this->channel_link}</link>rn"
  175.             $rss .= "<url>{$this->channel_imgurl}</url>rn"
  176.             $rss .= "</image>rn"
  177.         } 
  178.  
  179.         for ($i = 0; $i < count($this->items); $i++) { 
  180.             $rss .= "<item>rn"
  181.             $rss .= "<title><![CDATA[{$this->items[$i]['title']}]]></title>rn"
  182.             $rss .= "<link>{$this->items[$i]['link']}</link>rn"
  183.             $rss .= "<description><![CDATA[{$this->items[$i]['description']}]]></description>rn"
  184.             $rss .= "<pubDate>{$this->items[$i]['pubDate']}</pubDate>rn"
  185.             $rss .= "</item>rn"
  186.         } 
  187.  
  188.         $rss .= "</channel>rn</rss>"
  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.  

出处:http://www.phpfensi.com/php/20140829/4977.html


相关教程