通用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
使用范例:
- <?php
- // This is a minimum example of using the class
- include("FeedWriter.php");
- //Creating an instance of FeedWriter class.
- $TestFeed = new FeedWriter(RSS2);
- //Setting the channel elements
- //Use wrapper functions for common channel elements
- $TestFeed->setTitle('Testing & Checking the RSS writer class');
- $TestFeed->setLink('http://www.ajaxray.com/projects/rss');
- $TestFeed->setDescription('This is test of creating a RSS 2.0 feed Universal Feed Writer');
- //Image title and link must match with the 'title' and 'link' channel elements for valid RSS 2.0
- $TestFeed->setImage('Testing the RSS writer class','http://www.ajaxray.com/projects/rss','http://www.rightbrainsolution.com/images/logo.gif');
- //Detriving informations from database addin feeds
- $db->query($query);
- $result = $db->result;
- while($row = mysql_fetch_array($result, MYSQL_ASSOC))
- {
- //Create an empty FeedItem
- $newItem = $TestFeed->createNewItem();
- //Add elements to the feed item
- $newItem->setTitle($row['title']);
- $newItem->setLink($row['link']);
- $newItem->setDate($row['create_date']);
- $newItem->setDescription($row['description']);
- //Now add the feed item
- $TestFeed->addItem($newItem);
- }
- //OK. Everything is done. Now genarate the feed.
- $TestFeed->genarateFeed();
- ?>
补充:
另一个生成RSS 2.0的PHP Class 下载: class_rss_writer.php
- <?php
- /**
- * 使用范例:
- * ==============================================================
- $feed = new RSS();
- $feed->title = "RSS Feed Title";
- $feed->link = "http://www.newyork.com/";
- $feed->description = "Recent articles on newyork.com.";
- $db->query($query);
- $result = $db->result;
- while($row = mysql_fetch_array($result, MYSQL_ASSOC))
- {
- $item = new RSSItem();
- $item->title = $title;
- $item->link = $link;
- $item->setPubDate($create_date);
- $item->description = "<![CDATA[ $html ]]>";
- $feed->addItem($item);
- }
- echo $feed->serve();
- * ==============================================================
- */
- class RSS
- {
- var $title;
- var $link;
- var $description;
- var $language = "en-us";
- var $pubDate;
- var $items;
- var $tags;
- function RSS() {
- $this->items = array();
- $this->tags = array();
- }
- function addItem($item) {
- $this->items[] = $item;
- }
- function setPubDate($when) {
- if(strtotime($when) == false)
- $this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
- else
- $this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
- }
- function getPubDate() {
- if(empty($this->pubDate))
- return date("D, d M Y H:i:s ") . "GMT";
- else
- return $this->pubDate;
- }
- function addTag($tag, $value) {
- $this->tags[$tag] = $value;
- }
- function out() {
- $out = $this->header();
- $out .= "<channel>\n";
- $out .= "<title>" . $this->title . "</title>\n";
- $out .= "<link>" . $this->link . "</link>\n";
- $out .= "<description>" . $this->description . "</description>\n";
- $out .= "<language>" . $this->language . "</language>\n";
- $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";
- foreach($this->tags as $key => $val)
- $out .= "<$key>$val</$key>\n";
- foreach($this->items as $item)
- $out .= $item->out();
- $out .= "</channel>\n";
- $out .= $this->footer();
- $out = str_replace("&", "&", $out);
- return $out;
- }
- function serve($contentType = "application/xml") {
- $xml = $this->out();
- header("Content-type: $contentType");
- echo $xml;
- }
- function header() {
- $out = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
- $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">' . "\n";
- return $out;
- }
- function footer() {
- return '</rss>';
- }
- }
- class RSSItem
- {
- var $title;
- var $link;
- var $description;
- var $pubDate;
- var $guid;
- var $tags;
- var $attachment;
- var $length;
- var $mimetype;
- function RSSItem() {
- $this->tags = array();
- }
- function setPubDate($when) {
- if(strtotime($when) == false)
- $this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
- else
- $this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
- }
- function getPubDate() {
- if(empty($this->pubDate))
- return date("D, d M Y H:i:s ") . "GMT";
- else
- return $this->pubDate;
- }
- function addTag($tag, $value) {
- $this->tags[$tag] = $value;
- }
- function out() {
- $out .= "<item>\n";
- $out .= "<title>" . $this->title . "</title>\n";
- $out .= "<link>" . $this->link . "</link>\n";
- $out .= "<description>" . $this->description . "</description>\n";
- $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";
- if($this->attachment != "")
- $out .= "<enclosure url='{$this->attachment}' length='{$this->length}' type='{$this->mimetype}' />";
- if(empty($this->guid)) $this->guid = $this->link;
- $out .= "<guid>" . $this->guid . "</guid>\n";
- foreach($this->tags as $key => $val) $out .= "<$key>$val</$key\n>";
- $out .= "</item>\n";
- return $out;
- }
- function enclosure($url, $mimetype, $length) {
- $this->attachment = $url;
- $this->mimetype = $mimetype;
- $this->length = $length;
- }
- }
SimplePie是一个非常简单、实用的Syndication数据处理工具包。使用SimplePie ,可以快速的分析阅读RSS或Atom格 式数据。之前接触的更多是MagpieRSS,SimplePie在对RSS或Atom的数据处理能力上毫不逊色于MagpieRSS,同时 SimplePie拥有了比MagpieRSS更多的实用方法和属性,这可以帮助你快速的构建一个RSS阅读器或RSS数据处理模块。
- <?php
- include ('simplepie/simplepie.inc');
- $simplepie=new SimplePie();
- $simplepie->set_feed_url('http://ggggqqqqihc.yo2.cn/feed');
- $simplepie->init();
- echo '<h1>'.$simplepie->get_title().'</h1>';
- foreach ($simplepie->get_items() as $item){
- echo '<h2><a href="'.$item->get_permalink().'">'.$item->get_title().'</a></h2>';
- echo '<p>'.$item->get_content().'</p>';
- }
- ?>
获取到数据之后,调用 SimplePie 提供的实用方法,就很容易组装成一个个人的RSS阅读器了。
SimplePie 设计的一个很大的不合理之处是将 items 的排序方法内置在 init 方法中,这样想使用原生数据就需通过其他方式来实现了。