php實(shí)現(xiàn)的RSS生成類實(shí)例
更新時(shí)間:2015年04月23日 09:00:59 作者:不吃皮蛋
這篇文章主要介紹了php實(shí)現(xiàn)的RSS生成類,實(shí)例分析了RSS生成類的原理、定義與使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了php實(shí)現(xiàn)的RSS生成類。分享給大家供大家參考。具體如下:
class RSS
{
var $title;
var $link;
var $description;
var $language = "en-us";
var $pubDate;
var $items;
var $tags;
function RSS()
{
$this->items = array();
$this->tags = array();
}
function addItem($item)
{
$this->items[] = $item;
}
function setPubDate($when)
{
if(strtotime($when) == false)
$this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
else
$this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
}
function getPubDate()
{
if(empty($this->pubDate))
return date("D, d M Y H:i:s ") . "GMT";
else
return $this->pubDate;
}
function addTag($tag, $value)
{
$this->tags[$tag] = $value;
}
function out()
{
$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->tags as $key => $val) $out .= "<$key>$val</$key>\n";
foreach($this->items as $item) $out .= $item->out();
$out .= "</channel>\n";
$out .= $this->footer();
$out = str_replace("&", "&", $out);
return $out;
}
function serve($contentType = "application/xml")
{
$xml = $this->out();
header("Content-type: $contentType");
echo $xml;
}
function header()
{
$out = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
$out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">' . "\n";
return $out;
}
function footer()
{
return '</rss>';
}
}
class RSSItem
{
var $title;
var $link;
var $description;
var $pubDate;
var $guid;
var $tags;
var $attachment;
var $length;
var $mimetype;
function RSSItem()
{
$this->tags = array();
}
function setPubDate($when)
{
if(strtotime($when) == false)
$this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
else
$this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
}
function getPubDate()
{
if(empty($this->pubDate))
return date("D, d M Y H:i:s ") . "GMT";
else
return $this->pubDate;
}
function addTag($tag, $value)
{
$this->tags[$tag] = $value;
}
function out()
{
$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 .= "<enclosure url='{$this->attachment}' length='{$this->length}' type='{$this->mimetype}' />";
if(empty($this->guid)) $this->guid = $this->link;
$out .= "<guid>" . $this->guid . "</guid>\n";
foreach($this->tags as $key => $val) $out .= "<$key>$val</$key\n>";
$out .= "</item>\n";
return $out;
}
function enclosure($url, $mimetype, $length)
{
$this->attachment = $url;
$this->mimetype = $mimetype;
$this->length = $length;
}
}
使用示例如下:
$feed = new RSS();
$feed->title = "RSS Feed Title";
$feed->link = "http://website.com";
$feed->description = "Recent articles on your website.";
$db->query($query);
$result = $db->result;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$item = new RSSItem();
$item->title = $title;
$item->link = $link;
$item->setPubDate($create_date);
$item->description = "<![CDATA[ $html ]]>";
$feed->addItem($item);
}
echo $feed->serve();
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- php XMLWriter類的簡單示例代碼(RSS輸出)
- 用PHP讀取RSS feed的代碼
- php生成RSS訂閱的方法
- PHP 正則表達(dá)式分析RSS
- PHP調(diào)用Twitter的RSS的實(shí)現(xiàn)代碼
- 利用PHP和AJAX創(chuàng)建RSS聚合器的代碼
- PHP生成RSS文件類實(shí)例
- php生成rss類用法實(shí)例
- php實(shí)現(xiàn)將數(shù)組轉(zhuǎn)換為XML的方法
- PHP處理數(shù)組和XML之間的互相轉(zhuǎn)換
- php的SimpleXML方法讀寫XML接口文件實(shí)例解析
- php制作基于xml的RSS訂閱源功能示例
相關(guān)文章
php中使用exec,system等函數(shù)調(diào)用系統(tǒng)命令的方法(不建議使用,可導(dǎo)致安全問題)
PHP作為一種服務(wù)器端的腳本語言,像編寫簡單,或者是復(fù)雜的動(dòng)態(tài)網(wǎng)頁這樣的任務(wù),它完全能夠勝任。但事情不總是如此,有時(shí)為了實(shí)現(xiàn)某個(gè)功能,必須借助于操作系統(tǒng)的外部程序(或者稱之為命令),這樣可以做到事半功倍2012-09-09
php簡單瀏覽目錄內(nèi)容的實(shí)現(xiàn)代碼
本篇文章是對(duì)php簡單瀏覽目錄內(nèi)容的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP按指定鍵值對(duì)二維數(shù)組進(jìn)行排序的方法
這篇文章主要介紹了PHP按指定鍵值對(duì)二維數(shù)組進(jìn)行排序的方法,涉及PHP二維數(shù)組的遍歷及array_multisort函數(shù)的使用技巧,需要的朋友可以參考下2015-12-12

