php實(shí)現(xiàn)的RSS生成類實(shí)例
本文實(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類的簡(jiǎn)單示例代碼(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)文章
在wamp集成環(huán)境下升級(jí)php版本(實(shí)現(xiàn)方法)
本篇文章是對(duì)在wamp集成環(huán)境下升級(jí)php版本的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07php使用curl代理實(shí)現(xiàn)抓取數(shù)據(jù)的方法
這篇文章主要介紹了php使用curl代理實(shí)現(xiàn)抓取數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了php使用curl實(shí)現(xiàn)代理抓取數(shù)據(jù)的操作技巧,需要的朋友可以參考下2017-02-02web站點(diǎn)獲取用戶IP的安全方法 HTTP_X_FORWARDED_FOR檢驗(yàn)
通過上一篇,獲取用戶Ip地址通用方法常見安全隱患(HTTP_X_FORWARDED_FOR) ,我們已經(jīng)意 識(shí)到直接從http_x_forwarded_for中讀取用戶IP,跟我們直接從一個(gè)get,post值中讀取其實(shí)沒有兩樣。web參數(shù)檢測(cè)里面一個(gè)基本原則:“一切輸入都是有害的”,因此,只要是輸入我們就需要進(jìn)行過濾2013-06-06總結(jié)PHP代碼規(guī)范、流程規(guī)范、git規(guī)范
這篇內(nèi)容我們整理總結(jié)了關(guān)于PHP代碼規(guī)范、流程規(guī)范、git規(guī)范的詳細(xì)內(nèi)容,對(duì)此有興趣的朋友可以學(xué)習(xí)下。2018-06-06php使用pclzip類實(shí)現(xiàn)文件壓縮的方法(附pclzip類下載地址)
這篇文章主要介紹了php使用pclzip類實(shí)現(xiàn)文件壓縮的方法,分析了使用pclzip類的具體步驟與實(shí)現(xiàn)文件壓縮的相關(guān)技巧,并附帶pclzip類文件的下載地址,需要的朋友可以參考下2016-04-04