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
  // 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("&", "&amp;", $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;
    }
}
 

 

 

 

 

 

 

你可能感兴趣的:(mysql,PHP,xml)