PHP生成RSS文件类实例
本文实例讲述了PHP生成RSS文件类文件。分享给大家供大家参考。具体如下:
PHPRSS生成类实例代码如下:
<?php
if(defined('_class_rss_php'))return;
define('_class_rss_php教程',1);
/**
* 使用说明:
* $rss=newrss('redfox','http://jb51.net/',"redfox'sblog");
* $rss->additem('rssclass',"https://www.nhooo.com","xxx",date());
* $rss->additem(...);
* $rss->savetofile(...);
*/
classrss{
//public
$rss_ver="2.0";
$channel_title='';
$channel_link='';
$channel_description='';
$language='zh_cn';
$copyright='';
$webmaster='';
$pubdate='';
$lastbuilddate='';
$generator='redfoxrssgenerator';
$content='';
$items=array();
functionrss($title,$link,$description){
$this->channel_title=$title;
$this->channel_link=$link;
$this->channel_description=$description;
$this->pubdate=date('y-m-dh:i:s',time());
$this->lastbuilddate=date('y-m-dh:i:s',time());
}
functionadditem($title,$link,$description,$pubdate){
$this->items[]=array('titile'=>$title,
'link'=>$link,
'description'=>$description,
'pubdate'=>$pubdate);
}
functionbuildrss(){
$s="<!--lversion="1.0"encoding="gb2312"-->";
//startchannel
$s.="";
$s.=""
$s.="<link/>{$this->channel_link}";
$s.="{$this->channel_description}";
$s.="{$this->language}";
if(!emptyempty($this->copyright)){
$s.="{$this->copyright}";
}
if(!emptyempty($this->webmaster)){
$s.="{$this->webmaster}";
}
if(!emptyempty($this->pubdate)){
$s.="{$this->pubdate}";
}
if(!emptyempty($this->lastbuilddate)){
$s.="{$this->lastbuilddate}";
}
if(!emptyempty($this->generator)){
$s.="{$this->generator}";
}
//startitems
for($i=0;$iitems),$i++){
$s.="";
$s.="";
$s.="<link/>{$this->items[$i]['link']}";
$s.="<!--data[{$thi-->items[$i]['description']}]]>";
$s.="{$this->items[$i]['pubdate']}";
$s.="";
}
//closechannel
$s.="";
$this->content=$s;
}
functionshow(){
if(emptyempty($this->content))$this->buildrss();
header('content-type:text/xml');
echo($this->content);
}
functionsavetofile($fname){
if(emptyempty($this->content))$this->buildrss();
$handle=fopen($fname,'wb');
if($handle===false) returnfalse;
fwrite($handle,$this->content);
fclose($handle);
}
}
?>
希望本文所述对大家的PHP程序设计有所帮助。