php實(shí)現(xiàn)的RSS生成類實(shí)例
來源:易賢網(wǎng) 閱讀:731 次 日期:2015-04-28 15:33:22
溫馨提示:易賢網(wǎng)小編為您整理了“php實(shí)現(xiàn)的RSS生成類實(shí)例”,方便廣大網(wǎng)友查閱!

php實(shí)現(xiàn)的RSS生成類實(shí)例

這篇文章主要介紹了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=" . "\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 = "";

$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();

希望本文所述對大家的php程序設(shè)計(jì)有所幫助。

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:php實(shí)現(xiàn)的RSS生成類實(shí)例
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)