PHP生成RSS文件類實(shí)例
更新時(shí)間:2014年12月05日 12:08:10 投稿:shichen2014
這篇文章主要介紹了PHP生成RSS文件類,可實(shí)現(xiàn)PHP生成RSS文件的功能,對(duì)于網(wǎng)站建設(shè)與優(yōu)化來(lái)說(shuō)具有一定的實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了PHP生成RSS文件類文件。分享給大家供大家參考。具體如下:
PHP RSS 生成類實(shí)例代碼如下:
復(fù)制代碼 代碼如下:
<?php
if (defined('_class_rss_php')) return;
define('_class_rss_php教程',1);
/**
* 使用說(shuō)明:
* $rss = new rss('redfox','http://jb51.net/',"redfox's blog");
* $rss->additem('rss class',"http://chabaoo.cn","xxx",date());
* $rss->additem(...);
* $rss->savetofile(...);
*/
class rss {
//public
$rss_ver = "2.0";
$channel_title = '';
$channel_link = '';
$channel_description = '';
$language = 'zh_cn';
$copyright = '';
$webmaster = '';
$pubdate = '';
$lastbuilddate = '';
$generator = 'redfox rss generator';
$content = '';
$items = array();
function rss($title, $link, $description) {
$this->channel_title = $title;
$this->channel_link = $link;
$this->channel_description = $description;
$this->pubdate = date('y-m-d h:i:s',time());
$this->lastbuilddate = date('y-m-d h:i:s',time());
}
function additem($title, $link, $description ,$pubdate) {
$this->items[] = array('titile' => $title ,
'link' => $link,
'description' => $description,
'pubdate' => $pubdate);
}
function buildrss() {
$s = "<!--l version="1.0" encoding="gb2312"--> ";
// start channel
$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} ";
}
// start items
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 .= " ";
}
// close channel
$s .= " ";
$this->content = $s;
}
function show() {
if (emptyempty($this->content)) $this->buildrss();
header('content-type:text/xml');
echo($this->content);
}
function savetofile($fname) {
if (emptyempty($this->content)) $this->buildrss();
$handle = fopen($fname, 'wb');
if ($handle === false) return false;
fwrite($handle, $this->content);
fclose($handle);
}
}
?>
if (defined('_class_rss_php')) return;
define('_class_rss_php教程',1);
/**
* 使用說(shuō)明:
* $rss = new rss('redfox','http://jb51.net/',"redfox's blog");
* $rss->additem('rss class',"http://chabaoo.cn","xxx",date());
* $rss->additem(...);
* $rss->savetofile(...);
*/
class rss {
//public
$rss_ver = "2.0";
$channel_title = '';
$channel_link = '';
$channel_description = '';
$language = 'zh_cn';
$copyright = '';
$webmaster = '';
$pubdate = '';
$lastbuilddate = '';
$generator = 'redfox rss generator';
$content = '';
$items = array();
function rss($title, $link, $description) {
$this->channel_title = $title;
$this->channel_link = $link;
$this->channel_description = $description;
$this->pubdate = date('y-m-d h:i:s',time());
$this->lastbuilddate = date('y-m-d h:i:s',time());
}
function additem($title, $link, $description ,$pubdate) {
$this->items[] = array('titile' => $title ,
'link' => $link,
'description' => $description,
'pubdate' => $pubdate);
}
function buildrss() {
$s = "<!--l version="1.0" encoding="gb2312"--> ";
// start channel
$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} ";
}
// start items
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 .= " ";
}
// close channel
$s .= " ";
$this->content = $s;
}
function show() {
if (emptyempty($this->content)) $this->buildrss();
header('content-type:text/xml');
echo($this->content);
}
function savetofile($fname) {
if (emptyempty($this->content)) $this->buildrss();
$handle = fopen($fname, 'wb');
if ($handle === false) return false;
fwrite($handle, $this->content);
fclose($handle);
}
}
?>
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP測(cè)試框架PHPUnit組織測(cè)試操作示例
這篇文章主要介紹了PHP測(cè)試框架PHPUnit組織測(cè)試,結(jié)合實(shí)例形式分析了PHPUnit組織測(cè)試具體步驟、相關(guān)命令與操作技巧,需要的朋友可以參考下2018-05-05php實(shí)現(xiàn)mysql連接池效果實(shí)現(xiàn)代碼
這篇文章主要介紹了php代碼實(shí)現(xiàn)mysql連接池效果,需要的朋友可以參考下2018-01-01PHP中return 和 exit 、break和contiue 區(qū)別與用法
return、break和contiue是語(yǔ)言結(jié)構(gòu),就如同if語(yǔ)句之類的,但是exit卻是個(gè)函數(shù)2012-04-04PHP設(shè)置一邊執(zhí)行一邊輸出結(jié)果的代碼
這篇文章主要介紹了PHP中設(shè)置一邊執(zhí)行一邊輸出結(jié)果的實(shí)現(xiàn)代碼,需要的朋友可以參考下2013-09-09