您好,欢迎访问一九零五行业门户网

php生成RSS订阅的方法,phprss订阅_PHP教程

php生成rss订阅的方法,phprss订阅本文实例讲述了php生成rss订阅的方法。分享给大家供大家参考。具体分析如下:
rss(简易信息聚合,也叫聚合内容)是一种描述和同步网站内容的格式。rss可以是以下三个解释的其中一个: really simple syndication;rdf (resource description framework) site summary; rich site summary。但其实这三个解释都是指同一种syndication的技术。rss目前广泛用于网上新闻频道,blog和wiki。使用rss订阅能更快地获取信息,网站提供rss输出,有利于让用户获取网站内容的最新更新。网络用户可以在客户端借助于支持rss的聚合工具软件,在不打开网站内容页面的情况下阅读支持rss输出的网站内容。
从技术上来说一个rss文件就是一段规范的xml数据,该文件一般以rss,xml或者rdf作为后缀,下面是一段 rss 文件的内容示例:
复制代码 代码如下:
帮客之家
http://www.bkjia.com/
帮客之家
rss tutorial
网站地址/rss
new rss tutorial on w3school
xml tutorial
网站地址/xml
new xml tutorial on w3school
下面分享一段使用 php 动态生成 rss 的代码示例:
复制代码 代码如下:
/**
** php 动态生成 rss 类
**/
define(time_zone,);
define(feedcreator_version,www.jb51.net);//您的网址
class feeditem extends htmldescribable{
    var $title,$description,$link;
    var $author,$authoremail,$image,$category,$comments,$guid,$source,$creator;
    var $date;
    var $additionalelements=array();
}
class feedimage extends htmldescribable{
    var $title,$url,$link;
    var $width,$height,$description;
}
class htmldescribable{
    var $descriptionhtmlsyndicated;
    var $descriptiontruncsize;
function getdescription(){
        $descriptionfield=new feedhtmlfield($this->description);
        $descriptionfield->syndicatehtml=$this->descriptionhtmlsyndicated;
        $descriptionfield->truncsize=$this->descriptiontruncsize;
        return $descriptionfield->output();
    }
}
class feedhtmlfield{
    var $rawfieldcontent;
    var $truncsize,$syndicatehtml;
    function feedhtmlfield($parfieldcontent){
        if($parfieldcontent){
            $this->rawfieldcontent=$parfieldcontent;
        }
    }
    function output(){
        if(!$this->rawfieldcontent){
            $result=;
        }    elseif($this->syndicatehtml){
            $result=rawfieldcontent.]]>;
        }else{
            if($this->truncsize and is_int($this->truncsize)){
                $result=feedcreator::itrunc(htmlspecialchars($this->rawfieldcontent),$this->truncsize);
            }else{
                $result=htmlspecialchars($this->rawfieldcontent);
            }
        }
        return $result;
    }
}
class universalfeedcreator extends feedcreator{
    var $_feed;
    function _setformat($format){
        switch (strtoupper($format)){
            case 2.0:
                // fall through
            case rss2.0:
                $this->_feed=new rsscreator20();
                break;
            case 0.91:
                // fall through
            case rss0.91:
                $this->_feed=new rsscreator091();
                break;
            default:
                $this->_feed=new rsscreator091();
                break;
        }
        $vars=get_object_vars($this);
        foreach ($vars as $key => $value){
            // prevent overwriting of properties contenttype,encoding; do not copy _feed itself
            if(!in_array($key, array(_feed,contenttype,encoding))){
                $this->_feed->{$key}=$this->{$key};
            }
        }
    }
function createfeed($format=rss0.91){
        $this->_setformat($format);
        return $this->_feed->createfeed();
    }
function savefeed($format=rss0.91,$filename=,$displaycontents=true){
        $this->_setformat($format);
        $this->_feed->savefeed($filename,$displaycontents);
    }
function usecached($format=rss0.91,$filename=,$timeout=3600){
        $this->_setformat($format);
        $this->_feed->usecached($filename,$timeout);
    }
}
class feedcreator extends htmldescribable{
    var $title,$description,$link;
    var $syndicationurl,$image,$language,$copyright,$pubdate,$lastbuilddate,$editor,$editoremail,$webmaster,$category,$docs,$ttl,$rating,$skiphours,$skipdays;
    var $xslstylesheet=;
    var $items=array();
    var $contenttype=application/xml;
    var $encoding=utf-8;
    var $additionalelements=array();
function additem($item){
        $this->items[]=$item;
    }
function clearitem2null(){
        $this->items=array();
    }
function itrunc($string,$length){
        if(strlen($string)            return $string;
        }
$pos=strrpos($string,.);
        if($pos>=$length-4){
            $string=substr($string,0,$length-4);
            $pos=strrpos($string,.);
        }
        if($pos>=$length*0.4){
            return substr($string,0,$pos+1). ...;
        }
$pos=strrpos($string, );
        if($pos>=$length-4){
            $string=substr($string,0,$length-4);
            $pos=strrpos($string, );
        }
        if($pos>=$length*0.4){
            return substr($string,0,$pos). ...;
        }
return substr($string,0,$length-4). ...;
    }
function _creategeneratorcomment(){
        return \n;
    }
function _createadditionalelements($elements,$indentstring=){
        $ae=;
        if(is_array($elements)){
            foreach($elements as $key => $value){
                $ae.= $indentstring.$value$key>\n;
            }
        }
        return $ae;
    }
function _createstylesheetreferences(){
        $xml=;
        if($this->cssstylesheet) $xml .= cssstylesheet.\ type=\text/css\?>\n;
        if($this->xslstylesheet) $xml .= xslstylesheet.\ type=\text/xsl\?>\n;
        return $xml;
    }
function createfeed(){}
function _generatefilename(){
        $fileinfo=pathinfo($_server[php_self]);
        return substr($fileinfo[basename],0,-(strlen($fileinfo[extension])+1))..xml;
    }
function _redirect($filename){
        header(content-type: .$this->contenttype.; charset=.$this->encoding.; filename=.basename($filename));
        header(content-disposition: inline; filename=.basename($filename));
        readfile($filename,r);
        die();
    }
function usecached($filename=,$timeout=3600){
        $this->_timeout=$timeout;
        if($filename==){
            $filename=$this->_generatefilename();
        }
        if(file_exists($filename) && (time()-filemtime($filename)             $this->_redirect($filename);
        }
    }
function savefeed($filename=,$displaycontents=true){
        if($filename==){
            $filename=$this->_generatefilename();
        }
        $feedfile=fopen($filename,w+);
        if($feedfile){
            fputs($feedfile,$this->createfeed());
            fclose($feedfile);
            if($displaycontents){
                $this->_redirect($filename);
            }
        }else{
            echo
error creating feed file, please check write permissions.
;
        }
    }
}
class feeddate{
    var $unix;
    function feeddate($datestring=){
        if($datestring==) $datestring=date(r);
        if(is_integer($datestring)){
            $this->unix=$datestring;
            return;
        }
        if(preg_match(~(?:(?:mon|tue|wed|thu|fri|sat|sun),\\s+)?(\\d{1,2})\\s+([a-za-z]{3})\\s+(\\d{4})\\s+(\\d{2}):(\\d{2}):(\\d{2})\\s+(.*)~,$datestring,$matches)){
            $months=array(jan=>1,feb=>2,mar=>3,apr=>4,may=>5,jun=>6,jul=>7,aug=>8,sep=>9,oct=>10,nov=>11,dec=>12);
            $this->unix=mktime($matches[4],$matches[5],$matches[6],$months[$matches[2]],$matches[1],$matches[3]);
            if(substr($matches[7],0,1)=='+' or substr($matches[7],0,1)=='-'){
                $tzoffset=(substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
            }else{
                if(strlen($matches[7])==1){
                    $onehour=3600;
                    $ord=ord($matches[7]);
                    if($ord                         $tzoffset=(ord(a) - $ord - 1) * $onehour;
                    } elseif($ord >= ord(m) && $matches[7]!=z){
                        $tzoffset=($ord - ord(m)) * $onehour;
                    } elseif($matches[7]==z){
                        $tzoffset=0;
                    }
                }
                switch ($matches[7]){
                    case ut:
                    case gmt:    $tzoffset=0;
                }
            }
            $this->unix += $tzoffset;
            return;
        }
        if(preg_match(~(\\d{4})-(\\d{2})-(\\d{2})t(\\d{2}):(\\d{2}):(\\d{2})(.*)~,$datestring,$matches)){
            $this->unix=mktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]);
            if(substr($matches[7],0,1)=='+' or substr($matches[7],0,1)=='-'){
                $tzoffset=(substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
            }else{
                if($matches[7]==z){
                    $tzoffset=0;
                }
            }
            $this->unix += $tzoffset;
            return;
        }
        $this->unix=0;
    }
function rfc822(){
        $date=gmdate(y-m-d h:i:s,$this->unix);
        if(time_zone!=) $date .= .str_replace(:,,time_zone);
        return $date;
    }
function iso8601(){
        $date=gmdate(y-m-d h:i:s,$this->unix);
        $date=substr($date,0,22) . ':' . substr($date,-2);
        if(time_zone!=) $date=str_replace(+00:00,time_zone,$date);
        return $date;
    }
function unix(){
        return $this->unix;
    }
}
class rsscreator10 extends feedcreator{
    function createfeed(){
        $feed=encoding.\?>\n;
        $feed.= $this->_creategeneratorcomment();
        if($this->cssstylesheet==){
            $cssstylesheet=http://www.w3.org/2000/08/w3c-synd/style.css;
        }
        $feed.= $this->_createstylesheetreferences();
        $feed.=         $feed.=     xmlns=\http://purl.org/rss/1.0/\\n;
        $feed.=     xmlns:rdf=\http://www.w3.org/1999/02/22-rdf-syntax-ns#\\n;
        $feed.=     xmlns:slash=\http://purl.org/rss/1.0/modules/slash/\\n;
        $feed.=     xmlns:dc=\http://purl.org/dc/elements/1.1/\>\n;
        $feed.=     syndicationurl.\>\n;
        $feed.=         .htmlspecialchars($this->title).\n;
        $feed.=         .htmlspecialchars($this->description).\n;
        $feed.=         .$this->link.\n;
        if($this->image!=null){
            $feed.=         image->url.\ />\n;
        }
        $now=new feeddate();
        $feed.=        .htmlspecialchars($now->iso8601()).\n;
        $feed.=         \n;
        $feed.=             \n;
        for ($i=0;$iitems);$i++){
            $feed.=                 items[$i]->link).\/>\n;
        }
        $feed.=             \n;
        $feed.=         \n;
        $feed.=     \n;
        if($this->image!=null){
            $feed.=     image->url.\>\n;
            $feed.=         .$this->image->title.\n;
            $feed.=         .$this->image->link.\n;
            $feed.=         .$this->image->url.\n;
            $feed.=     \n;
        }
        $feed.= $this->_createadditionalelements($this->additionalelements,    );
for ($i=0;$iitems);$i++){
            $feed.=     items[$i]->link).\>\n;
            //$feed.=         posting\n;
            $feed.=         text/html\n;
            if($this->items[$i]->date!=null){
                $itemdate=new feeddate($this->items[$i]->date);
                $feed.=         .htmlspecialchars($itemdate->iso8601()).\n;
            }
            if($this->items[$i]->source!=){
                $feed.=         .htmlspecialchars($this->items[$i]->source).\n;
            }
            if($this->items[$i]->author!=){
                $feed.=         .htmlspecialchars($this->items[$i]->author).\n;
            }
            $feed.=         .htmlspecialchars(strip_tags(strtr($this->items[$i]->title,\n\r,  ))).\n;
            $feed.=         .htmlspecialchars($this->items[$i]->link).\n;
            $feed.=         .htmlspecialchars($this->items[$i]->description).\n;
            $feed.= $this->_createadditionalelements($this->items[$i]->additionalelements,        );
            $feed.=     \n;
        }
        $feed.= \n;
        return $feed;
    }
}
class rsscreator091 extends feedcreator{
    var $rssversion;
function rsscreator091(){
        $this->_setrssversion(0.91);
        $this->contenttype=application/rss+xml;
    }
function _setrssversion($version){
        $this->rssversion=$version;
    }
function createfeed(){
        $feed=encoding.\?>\n;
        $feed.= $this->_creategeneratorcomment();
        $feed.= $this->_createstylesheetreferences();
        $feed.= rssversion.\>\n;
        $feed.=     \n;
        $feed.=         .feedcreator::itrunc(htmlspecialchars($this->title),100).\n;
        $this->descriptiontruncsize=500;
        $feed.=         .$this->getdescription().\n;
        $feed.=         .$this->link.\n;
        $now=new feeddate();
        $feed.=         .htmlspecialchars($now->rfc822()).\n;
        $feed.=         .feedcreator_version.\n;
if($this->image!=null){
            $feed.=         \n;
            $feed.=             .$this->image->url.\n;
            $feed.=             .feedcreator::itrunc(htmlspecialchars($this->image->title),100).\n;
            $feed.=             .$this->image->link.\n;
            if($this->image->width!=){
                $feed.=             .$this->image->width.\n;
            }
            if($this->image->height!=){
                $feed.=             .$this->image->height.\n;
            }
            if($this->image->description!=){
                $feed.=             .$this->image->getdescription().\n;
            }
            $feed.=         \n;
        }
        if($this->language!=){
            $feed.=         .$this->language.\n;
        }
        if($this->copyright!=){
            $feed.=         .feedcreator::itrunc(htmlspecialchars($this->copyright),100).\n;
        }
        if($this->editor!=){
            $feed.=         .feedcreator::itrunc(htmlspecialchars($this->editor),100).\n;
        }
        if($this->webmaster!=){
            $feed.=         .feedcreator::itrunc(htmlspecialchars($this->webmaster),100).\n;
        }
        if($this->pubdate!=){
            $pubdate=new feeddate($this->pubdate);
            $feed.=         .htmlspecialchars($pubdate->rfc822()).\n;
        }
        if($this->category!=){
            $feed.=         .htmlspecialchars($this->category).\n;
        }
        if($this->docs!=){
            $feed.=         .feedcreator::itrunc(htmlspecialchars($this->docs),500).\n;
        }
        if($this->ttl!=){
            $feed.=         .htmlspecialchars($this->ttl).\n;
        }
        if($this->rating!=){
            $feed.=         .feedcreator::itrunc(htmlspecialchars($this->rating),500).\n;
        }
        if($this->skiphours!=){
            $feed.=         .htmlspecialchars($this->skiphours).\n;
        }
        if($this->skipdays!=){
            $feed.=         .htmlspecialchars($this->skipdays).\n;
        }
        $feed.= $this->_createadditionalelements($this->additionalelements,    );
for ($i=0;$iitems);$i++){
            $feed.=         \n;
            $feed.=             .feedcreator::itrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100).\n;
            $feed.=             .htmlspecialchars($this->items[$i]->link).\n;
            $feed.=             .$this->items[$i]->getdescription().\n;
if($this->items[$i]->author!=){
                $feed.=             .htmlspecialchars($this->items[$i]->author).\n;
            }
            /*
             // on hold
             if($this->items[$i]->source!=){
             $feed.=             .htmlspecialchars($this->items[$i]->source).\n;
             }
             */
            if($this->items[$i]->category!=){
                $feed.=             .htmlspecialchars($this->items[$i]->category).\n;
            }
            if($this->items[$i]->comments!=){
                $feed.=             .htmlspecialchars($this->items[$i]->comments).\n;
            }
            if($this->items[$i]->date!=){
                $itemdate=new feeddate($this->items[$i]->date);
                $feed.=             .htmlspecialchars($itemdate->rfc822()).\n;
            }
            if($this->items[$i]->guid!=){
                $feed.=             .htmlspecialchars($this->items[$i]->guid).\n;
            }
            $feed.= $this->_createadditionalelements($this->items[$i]->additionalelements,        );
            $feed.=         \n;
        }
        $feed.=     \n;
        $feed.= \n;
        return $feed;
    }
}
class rsscreator20 extends rsscreator091{
function rsscreator20(){
        parent::_setrssversion(2.0);
    }
}
使用示例:
复制代码 代码如下:
header('content-type:text/html; charset=utf-8');
$db=mysql_connect('127.0.0.1','root','123456');
mysql_query(set names utf8);
mysql_select_db('dbname',$db);
$brs=mysql_query('select * from article order by add_time desc limit 0,10',$db);
$rss=new universalfeedcreator();
$rss->title=页面标题;
$rss->link=网址http://;
$rss->description=rss标题;
while($rowbrs=mysql_fetch_array($brs)){
    $item=new feeditem();
    $item->title =$rowbrs['subject'];
    $item->link='http://www.bkjia.com/';
    $item->description =$rowbrs['description'];
    $rss->additem($item);
}
mysql_close($db);
$rss->savefeed(rss2.0,rss.xml);
希望本文所述对大家的php程序设计有所帮助。
http://www.bkjia.com/phpjc/958257.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/958257.htmltecharticlephp生成rss订阅的方法,phprss订阅 本文实例讲述了php生成rss订阅的方法。分享给大家供大家参考。具体分析如下: rss(简易信息聚合,也叫...
其它类似信息

推荐信息