php实现的RSS生成类实例
本文实例讲述了php实现的RSS生成类。分享给大家供大家参考。具体如下:
classRSS { var$title; var$link; var$description; var$language="en-us"; var$pubDate; var$items; var$tags; functionRSS() { $this->items=array(); $this->tags=array(); } functionaddItem($item) { $this->items[]=$item; } functionsetPubDate($when) { if(strtotime($when)==false) $this->pubDate=date("D,dMYH:i:s",$when)."GMT"; else $this->pubDate=date("D,dMYH:i:s",strtotime($when))."GMT"; } functiongetPubDate() { if(empty($this->pubDate)) returndate("D,dMYH:i:s")."GMT"; else return$this->pubDate; } functionaddTag($tag,$value) { $this->tags[$tag]=$value; } functionout() { $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->tagsas$key=>$val)$out.="<$key>$val</$key>\n"; foreach($this->itemsas$item)$out.=$item->out(); $out.="</channel>\n"; $out.=$this->footer(); $out=str_replace("&","&",$out); return$out; } functionserve($contentType="application/xml") { $xml=$this->out(); header("Content-type:$contentType"); echo$xml; } functionheader() { $out='<?xmlversion="1.0"encoding="utf-8"?>'."\n"; $out.='<rssversion="2.0"xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n"; return$out; } functionfooter() { return'</rss>'; } } classRSSItem { var$title; var$link; var$description; var$pubDate; var$guid; var$tags; var$attachment; var$length; var$mimetype; functionRSSItem() { $this->tags=array(); } functionsetPubDate($when) { if(strtotime($when)==false) $this->pubDate=date("D,dMYH:i:s",$when)."GMT"; else $this->pubDate=date("D,dMYH:i:s",strtotime($when))."GMT"; } functiongetPubDate() { if(empty($this->pubDate)) returndate("D,dMYH:i:s")."GMT"; else return$this->pubDate; } functionaddTag($tag,$value) { $this->tags[$tag]=$value; } functionout() { $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.="<enclosureurl='{$this->attachment}'length='{$this->length}'type='{$this->mimetype}'/>"; if(empty($this->guid))$this->guid=$this->link; $out.="<guid>".$this->guid."</guid>\n"; foreach($this->tagsas$key=>$val)$out.="<$key>$val</$key\n>"; $out.="</item>\n"; return$out; } functionenclosure($url,$mimetype,$length) { $this->attachment=$url; $this->mimetype=$mimetype; $this->length=$length; } }
使用示例如下:
$feed=newRSS(); $feed->title="RSSFeedTitle"; $feed->link="http://website.com"; $feed->description="Recentarticlesonyourwebsite."; $db->query($query); $result=$db->result; while($row=mysql_fetch_array($result,MYSQL_ASSOC)) { $item=newRSSItem(); $item->title=$title; $item->link=$link; $item->setPubDate($create_date); $item->description="<![CDATA[$html]]>"; $feed->addItem($item); } echo$feed->serve();
希望本文所述对大家的php程序设计有所帮助。