PHP RSS/Feed 生成类库(支持RSS 1.0/2.0和ATOM)

通用PHP RSS/Feed 生成类库(支持RSS 1.0/2.0和ATOM)
PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM)

下载 : FeedWriter

 

可生成的RSS版本:

  • RSS 1.0 (which officially obsoleted RSS 0.90)
  • RSS 2.0 (which officially obsoleted RSS 0.91, 0.92, 0.93 and 0.94)
  • ATOM 1.0

功能:

  • 可生成RSS 1.0, RSS 2.0 和ATOM 1.0 feeds
  • 所有生成的Feed可经过验证 feed validator .
  • 支持所有Feed属性.
  • 容易的设置channel 和 feed 条目
  • 为不同的版本使用命名空间.
  • 自动转换日期格式.
  • 为ATOM feeds生成 UUID (通用唯一标识符 Universally Unique Identifier).
  • 支持子标签和子标签属性. (如: image 和 encloser tags)
  • 完全的 PHP5面向对像构造 class structure.
  • 为需要的标签CDATA 编码.
  • 使用差不多的代码生成所有版本的feed

使用范例:

 

Php代码  
  1. <?php   
  2.   // This is a minimum example of using the class   
  3.   include("FeedWriter.php");   
  4.     
  5.   //Creating an instance of FeedWriter class.   
  6.   $TestFeed = new FeedWriter(RSS2);   
  7.     
  8.   //Setting the channel elements   
  9.   //Use wrapper functions for common channel elements   
  10.   $TestFeed->setTitle('Testing & Checking the RSS writer class');   
  11.   $TestFeed->setLink('http://www.ajaxray.com/projects/rss');   
  12.   $TestFeed->setDescription('This is test of creating a RSS 2.0 feed Universal Feed Writer');   
  13.     
  14.   //Image title and link must match with the 'title' and 'link' channel elements for valid RSS 2.0   
  15.   $TestFeed->setImage('Testing the RSS writer class','http://www.ajaxray.com/projects/rss','http://www.rightbrainsolution.com/images/logo.gif');   
  16.     
  17.     //Detriving informations from database addin feeds   
  18.     $db->query($query);   
  19.     $result = $db->result;   
  20.     
  21.     while($row = mysql_fetch_array($result, MYSQL_ASSOC))   
  22.     {   
  23.         //Create an empty FeedItem   
  24.         $newItem = $TestFeed->createNewItem();   
  25.            
  26.         //Add elements to the feed item      
  27.         $newItem->setTitle($row['title']);   
  28.         $newItem->setLink($row['link']);   
  29.         $newItem->setDate($row['create_date']);   
  30.         $newItem->setDescription($row['description']);   
  31.            
  32.         //Now add the feed item   
  33.         $TestFeed->addItem($newItem);   
  34.     }   
  35.     
  36.   //OK. Everything is done. Now genarate the feed.   
  37.   $TestFeed->genarateFeed();   
  38.     
  39. ?>  
 

补充:
另一个生成RSS 2.0的PHP Class   
下载: class_rss_writer.php

 

Php代码  
  1. <?php   
  2.     
  3. /**  
  4.  * 使用范例:  
  5.  * ==============================================================  
  6.     $feed = new RSS();  
  7.     $feed->title       = "RSS Feed Title";  
  8.     $feed->link        = "http://www.newyork.com/";  
  9.     $feed->description = "Recent articles on newyork.com.";  
  10.    
  11.     $db->query($query);  
  12.     $result = $db->result;  
  13.     while($row = mysql_fetch_array($result, MYSQL_ASSOC))  
  14.     {  
  15.         $item = new RSSItem();  
  16.         $item->title = $title;  
  17.         $item->link  = $link;  
  18.         $item->setPubDate($create_date);  
  19.         $item->description = "<![CDATA[ $html ]]>";  
  20.         $feed->addItem($item);  
  21.     }  
  22.     echo $feed->serve();  
  23.  * ==============================================================  
  24.  */  
  25.     
  26. class RSS   
  27. {   
  28.     var $title;   
  29.     var $link;   
  30.     var $description;   
  31.     var $language = "en-us";   
  32.     var $pubDate;   
  33.     var $items;   
  34.     var $tags;   
  35.     
  36.     function RSS() {   
  37.         $this->items = array();   
  38.         $this->tags  = array();   
  39.     }   
  40.     
  41.     function addItem($item) {   
  42.         $this->items[] = $item;   
  43.     }   
  44.     
  45.     function setPubDate($when) {   
  46.         if(strtotime($when) == false)   
  47.             $this->pubDate = date("D, d M Y H:i:s "$when) . "GMT";   
  48.         else  
  49.             $this->pubDate = date("D, d M Y H:i:s "strtotime($when)) . "GMT";   
  50.     }   
  51.     
  52.     function getPubDate() {   
  53.         if(emptyempty($this->pubDate))   
  54.             return date("D, d M Y H:i:s ") . "GMT";   
  55.         else  
  56.             return $this->pubDate;   
  57.     }   
  58.     
  59.     function addTag($tag$value) {   
  60.         $this->tags[$tag] = $value;   
  61.     }   
  62.     
  63.     function out() {   
  64.         $out  = $this->header();   
  65.         $out .= "<channel>\n";   
  66.         $out .= "<title>" . $this->title . "</title>\n";   
  67.         $out .= "<link>" . $this->link . "</link>\n";   
  68.         $out .= "<description>" . $this->description . "</description>\n";   
  69.         $out .= "<language>" . $this->language . "</language>\n";   
  70.         $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";   
  71.     
  72.         foreach($this->tags as $key => $val)   
  73.             $out .= "<$key>$val</$key>\n";   
  74.         foreach($this->items as $item)   
  75.             $out .= $item->out();   
  76.     
  77.         $out .= "</channel>\n";              
  78.         $out .= $this->footer();   
  79.     
  80.         $out = str_replace("&""&amp;"$out);   
  81.         return $out;   
  82.     }   
  83.        
  84.     function serve($contentType = "application/xml") {   
  85.         $xml = $this->out();   
  86.         header("Content-type: $contentType");   
  87.         echo $xml;   
  88.     }   
  89.     
  90.     function header() {   
  91.         $out  = '<?xml version="1.0" encoding="utf-8"?>' . "\n";   
  92.         $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">' . "\n";   
  93.         return $out;   
  94.     }   
  95.     
  96.     function footer() {   
  97.         return '</rss>';   
  98.     }   
  99. }   
  100.     
  101. class RSSItem   
  102. {   
  103.     var $title;   
  104.     var $link;   
  105.     var $description;   
  106.     var $pubDate;   
  107.     var $guid;   
  108.     var $tags;   
  109.     var $attachment;   
  110.     var $length;   
  111.     var $mimetype;   
  112.     
  113.     function RSSItem() {   
  114.         $this->tags = array();   
  115.     }   
  116.     
  117.     function setPubDate($when) {   
  118.         if(strtotime($when) == false)   
  119.             $this->pubDate = date("D, d M Y H:i:s "$when) . "GMT";   
  120.         else  
  121.             $this->pubDate = date("D, d M Y H:i:s "strtotime($when)) . "GMT";   
  122.     }   
  123.     
  124.     function getPubDate() {   
  125.         if(emptyempty($this->pubDate))   
  126.             return date("D, d M Y H:i:s ") . "GMT";   
  127.         else  
  128.             return $this->pubDate;   
  129.     }   
  130.     
  131.     function addTag($tag$value) {   
  132.         $this->tags[$tag] = $value;   
  133.     }   
  134.     
  135.     function out() {   
  136.         $out .= "<item>\n";   
  137.         $out .= "<title>" . $this->title . "</title>\n";   
  138.         $out .= "<link>" . $this->link . "</link>\n";   
  139.         $out .= "<description>" . $this->description . "</description>\n";   
  140.         $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";   
  141.     
  142.         if($this->attachment != "")   
  143.             $out .= "<enclosure url='{$this->attachment}' length='{$this->length}' type='{$this->mimetype}' />";   
  144.     
  145.         if(emptyempty($this->guid)) $this->guid = $this->link;   
  146.         $out .= "<guid>" . $this->guid . "</guid>\n";   
  147.     
  148.         foreach($this->tags as $key => $val$out .= "<$key>$val</$key\n>";   
  149.         $out .= "</item>\n";   
  150.         return $out;   
  151.     }   
  152.     
  153.     function enclosure($url$mimetype$length) {   
  154.         $this->attachment = $url;   
  155.         $this->mimetype   = $mimetype;   
  156.         $this->length     = $length;   
  157.     }   
  158. }  
 

 

 

 

 

 

 

时间:2011-12-06 | 分类:技术文摘 | 标签: PHP  RSS-Feed  
评论列表
暂无评论
发表评论
昵称
邮箱
内容