生成rss非常的简单只需要使用rss格式然后生成输出到浏览器就可以了,重点就是要告诉浏览器你是rss格式的数据即可,具体来看个例子。
rss(简易信息聚合也叫聚合内容)是一种描述和同步网站内容的格式。下面的生成rss订阅的代码:
rss xml结构
网站名称http://www.111cn.net/网站描述!rss tutorial网站地址/rssnew rss tutorial on w3schoolxml tutorial网站地址/xmlnew xml tutorial on w3school
rss实例
createitem().'';echo $html;}private function createitem() {//rss item//$data可替换为自己的数据$html = '';//文章数据$data = array('id' => 1,'date' => date('r', time()),'title' => '文章标题','link' => 'http://www.111cn.net', //文章地址'description' => '网站描述');for($i = 0; $i createfeed();exit;?>
rss feed 生成后,如何设置才能给网站添加 rss 呢?并且让 firefox、ie7 或其它 feed 机器人自动发现?很简单,在网页的 head 节添加一个特定的 link 标签即可,如下:
设置 title 为 feed 标题,href 为 feed 地址,一切就 ok 了!
例子2
php程序员教程网http://www.111cn.net/本站是一个php程序员的工作生活笔记!rss tutorial网站地址/rssnew rss tutorial on w3schoolxml tutorial网站地址/xmlnew xml tutorial on w3school
下面分享一段使用 php 动态生成 rss 的代码示例:
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)=$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\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) _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 = 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.= \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); }}
使用示例:
title=页面标题;$rss->link=网址http://;$rss->description=rss标题;while($rowbrs=mysql_fetch_array($brs)){ $item=new feeditem(); $item->title =$rowbrs['subject']; $item->link='http://www.111cn.net/'; $item->description =$rowbrs['description']; $rss->additem($item);}mysql_close($db);$rss->savefeed(rss2.0,rss.xml);