PHP RSS/Feed类库

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

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

 

可 生成的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

使用范例:

Java代码   收藏代码
  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

Java代码   收藏代码
  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(empty($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(empty($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(empty($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. }  

SimplePie是一个非常简单、实用的Syndication数据处理工具包。使用SimplePie ,可以快速的分析阅读RSS或Atom格 式数据。之前接触的更多是MagpieRSS,SimplePie在对RSS或Atom的数据处理能力上毫不逊色于MagpieRSS,同时 SimplePie拥有了比MagpieRSS更多的实用方法和属性,这可以帮助你快速的构建一个RSS阅读器或RSS数据处理模块。

Java代码   收藏代码
  1. <?php  
  2. include ('simplepie/simplepie.inc');  
  3.   
  4. $simplepie=new SimplePie();  
  5. $simplepie->set_feed_url('http://ggggqqqqihc.yo2.cn/feed');  
  6. $simplepie->init();  
  7. echo '<h1>'.$simplepie->get_title().'</h1>';  
  8. foreach ($simplepie->get_items() as $item){  
  9.     echo '<h2><a href="'.$item->get_permalink().'">'.$item->get_title().'</a></h2>';  
  10.     echo '<p>'.$item->get_content().'</p>';  
  11. }  
  12. ?>  

获取到数据之后,调用 SimplePie 提供的实用方法,就很容易组装成一个个人的RSS阅读器了。

SimplePie 设计的一个很大的不合理之处是将 items 的排序方法内置在 init 方法中,这样想使用原生数据就需通过其他方式来实现了。

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
9月前
|
XML 存储 安全
Excel电子表格的PHP类库:PHP_XLSXWriter(大数据量报表、后台运行、浏览器下载)
Excel电子表格的PHP类库:PHP_XLSXWriter(大数据量报表、后台运行、浏览器下载)
158 0
|
9月前
|
SQL 缓存 关系型数据库
php开发实战分析(7):mysql类库操作的高级使用
php开发实战分析(7):mysql类库操作的高级使用
127 0
|
9月前
|
SQL 安全 关系型数据库
php开发实战分析(8):mysql类库的操作高级使用API开发(字段操作、权限分配、日期格式、跨表查询,数据分表)
php开发实战分析(8):mysql类库的操作高级使用API开发(字段操作、权限分配、日期格式、跨表查询,数据分表)
299 0
|
9月前
|
PHP vr&ar
PHP二维码生成类库phpqrcode.php生成二维码
PHP二维码生成类库phpqrcode.php生成二维码
123 0
|
PHP
PHP引入endroid/qr-code类库生成指定二维码
PHP引入endroid/qr-code类库生成指定二维码
239 0
|
JavaScript PHP 容器
***php解析html类库simple_html_dom
下载地址:https://github.com/samacs/simple_html_dom 一直以来使用php解析html文档树都是一个难题。Simple HTML DOM parser 帮我们很好地解决了这个问题。
1881 0
|
Web App开发 PHP
PHP Snoopy类库
snoopy是一个php类,用来模仿web浏览器的功能,它能完成获取网页内容和发送表单的任务。 官方网站 http://snoopy.sourceforge.net/ Snoopy可以完成以下功能: 1、抓取网页的内容 2、抓取网页的文字(去掉HTML代码) 3、抓取网页的超链接 4、支持代理...
1123 0
|
XML JavaScript 前端开发
PHP 实例 - AJAX RSS 阅读器
RSS 阅读器用于阅读 RSS Feed。 AJAX RSS 阅读器 在下面的实例中,我们将演示一个 RSS 阅读器,通过它,来自 RSS 的内容在网页不进行刷新的情况下被载入: 实例解释 - HTML 页面 当用户在上面的下拉列表中选择某个 RSS-feed 时,会执行名为 "showRSS()" 的函数。
1303 0