经测试代码如下:
<?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/528791.html