php 一个完全面向对象的RSS/XML类的简单示例

前端之家收集整理的这篇文章主要介绍了php 一个完全面向对象的RSS/XML类的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:

<?PHP
/**
 * RSS/XML类
 *
 * @param 
 * @arrange (512.笔记) jb51.cc
 **/
error_reporting(E_ALL);

MysqL_connect("localhost","root","root") or die (MysqL_error());
MysqL_select_db("oop") or die (MysqL_error());

class RSS {

var $XMLdump;

var $pagetitle;
var $pagelink;
var $pegedescription;
var $pagelanguage;

var $sqlresult;

function setHead($setPagetitle,$setPagelink,$setPegedescription,$setPagelanguage){
$this->pagetitle = $setPagetitle;
$this->pagelink = $setPagelink;
$this->pegedescription = $setPegedescription;
$this->pagelanguage = $setPagelanguage;
}

function getDataFrom($setsql){
$this->sqlresult = MysqL_query($setsql);
}


function RSSHead(){
$this->XMLdump = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<RSS version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom/\">
<channel>
<title>".$this->pagetitle."</title>
<link>".$this->pagelink."</link>
<description>".$this->pegedescription."</description>
<language>".$this->pagelanguage."</language>
<lastBuildDate>".date("r",time())."</lastBuildDate>\n";
}

function RSSItems(){
while($bla = MysqL_fetch_assoc($this->sqlresult)){
$this->XMLdump .= " <item>\n";
$this->XMLdump .= " <title>".$bla['title']."</title>\n";
$this->XMLdump .= " <link>http://bestnewssiteever.com/news/".$bla['id']."/</link>\n";
$this->XMLdump .= " <category>".$bla['category']."</category>\n";
$this->XMLdump .= " <pubDate>".date("r",$bla['pubDate'])."</pubDate>\n";
preg_match_all("/^(?:[^.]*\.){3}/",$bla['content'],$trimedContent);
$this->XMLdump .= " <description>".$trimedContent[0][0]."..</description>\n";
$this->XMLdump .= " </item>\n";
}
}

function RSSFooter(){
$this->XMLdump .= " </channel>
</RSS>";
}

function writeXML(){
$this->RSSHead();
$this->RSSItems();
$this->RSSFooter();
return $this->XMLdump;
}

function saveXML($file){
$fp = fopen($file,"w+");
flock($fp,2);
fwrite($fp,$this->writeXML());
flock($fp,3);
fclose($fp);
}
}

$Bar = new RSS();
$Bar->getDataFrom("SELECT * FROM news ORDER BY pubDate DESC");
$Bar->setHead("TITLE","http://domain.de","DESCRIPTION","en-EN");
$Bar->saveXML("blub.xml");


/*** 来自:编程之家 jb51.cc(jb51.cc) ***/ 
?>

猜你在找的PHP相关文章