天天看点

PHP RSS/Feed类库

通用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属性.

容易的设置channel 和 feed 条目

为不同的版本使用命名空间.

自 动转换日期格式.

为atom feeds生成 uuid (通用唯一标识符 universally unique identifier).

支持子标签和子标签属性. (如: image 和 encloser tags)

完全的 php5面向对像构造 class structure.

为需要的标签cdata 编码.

使用差不多的代码生成所有 版本的feed

使用范例:

PHP RSS/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();  

?>  

补充:

PHP RSS/Feed类库

/** 

 * 使用范例: 

 * ============================================================== 

    $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";  

            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";  

    function footer() {  

        return '</rss>';  

}  

class rssitem  

    var $guid;  

    var $attachment;  

    var $length;  

    var $mimetype;  

    function rssitem() {  

        $this->tags = array();  

        $out .= "<item>\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";  

    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 RSS/Feed类库

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 方法中,这样想使用原生数据就需通过其他方式来实现了。