在玩部落格的時候,有這樣的一個功能,就是RSS訂閱,今天我也實作一個制作RSS供應源的功能。點選RSS訂閱按鈕,可以看到如下的内容。

其實,這個功能不是很難。隻要按照RSS的規範來,無非也就是寫一個特殊的XML檔案,如何特殊了?因為我們要按照它的規範來定義節點的名稱。
特殊點:
(1)有且僅有一個rss節點标簽
(2)有且僅有一個channel節點标簽,channel标簽中必須有一下子節點:title、link、description
(3)可以有多個Item标簽,item标簽中必須有一下子節點:title、description
請參考閱讀rss2.0規範。
1、一個簡單的RSS訂閱源内容
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/">
<!--一個RSS閱讀源-->
<channel>
<title>我的商城</title>
<link>http://www.webbc.win</link>
<description>我自己做的商城</description>
<item>
<title>三星Note 7</title>
<description>曲屏,牛逼</description>
</item>
<item>
<title>IPhone 7</title>
<description>兇!</description>
</item>
</channel>
</rss>
用浏覽器打開此檔案,會看到如下效果。
我這裡是直接把XML檔案中的内容給寫死了,CSDN部落格肯定不是這樣的,因為它的内容是動态的,是需要從資料庫中讀取的,這個操作想必對于每個php程式員都是最基本的技能,下面我實作一個從資料庫中讀取來制作一個動态内容的RSS供應源。
2、從資料庫中讀取内容制作RSS供應源
代碼:
<?php
/**
* 從資料庫中讀取資訊,制作出RSS供應源
* @author webbc
*/
class RSS {
public $title = '';//channel的title
public $link = '';//channel的link
public $description = '';//channel的description
public $dom = null;//dom節點對象
public $rss = null;//rss節點對象
public $list = array();//item的數組資訊
public $template = './rss_template.xml';//rss制作的模闆檔案
//構造方法
public function __construct(){
$this->dom = new DOMDocument('1.0','utf-8');//建立dom對象
$this->dom->load($this->template);//加載模闆檔案内容
$this->rss = $this->dom->getElementsByTagName('rss')->item();//找到rss節點内容
}
//在浏覽器上顯示rss源内容
public function display(){
$this->createChannel();//建立channel資訊
$this->rss->appendChild($this->createChannel());//把channel節點資訊添加到rss節點上
//循環周遊List數組中的内容,将多個item節點資訊綁定到rss節點上
foreach ($this->list as $k => $v) {
$this->rss->appendChild($this->createItem($v));
}
//輸出到浏覽器:
header('content-type:text/xml;charset=utf-8');
echo $this->dom->saveXML();
}
//建立channel
private function createChannel(){
$channel = $this->dom->createElement('channel');
$channel->appendChild($this->createEle('title',$this->title));
$channel->appendChild($this->createEle('link',$this->link));
$channel->appendChild($this->createEle('description',$this->description));
return $channel;
}
//建立channel的item條目
private function createItem($arr){
$item = $this->dom->createElement('item');
foreach ($arr as $k => $v) {
$item->appendChild($this->createEle($k,$v));
}
return $item;
}
//建立一個節點,内容如下,<ele>some text</ele>
private function createEle($name,$value){
$text = $this->dom->createTextNode($value);
$ele = $this->dom->createElement($name);
$ele->appendChild($text);
return $ele;
}
}
//建立rss類對象,然後設定channel的tilte、link、description資訊
$rss = new RSS();
$rss->title = '我的商城,我做主!';
$rss->link = 'http://wwww.webbc.win';
$rss->description = '有很多的優惠資訊,不要錯誤哈';
//讀取資料庫
$conn = mysql_connect('localhost','root','1234');
mysql_query('use shop');
mysql_query('set names utf8');
$sql = 'select goods_name as title,shop_price as description from goods';//這裡我将貨物的名稱作為title,貨物的價格作為描述資訊
$res = mysql_query($sql);
$list = array();
while($row = mysql_fetch_assoc($res)){
$list[] = $row;
}
$rss->list = $list;
$rss->display();//顯示XML顯示
?>
RSS供應源的模闆檔案内容(rss_template.xml):
<?xml version="1.0" encoding="UTF-8"?>
<!--RSS源制作的模闆-->
<rss version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/"></rss>
資料庫表内容:
運作結果圖:
思路:
首先是寫了一個RSS的類。然後建立這個類的對象,設定channel的tiltle、link、description的的資訊,接着讀取資料庫,從中取得二維數組,注意,這個二維數組将作為item标簽的内容,最後調用display方法将顯示到浏覽器上。display方法調用了createChannel方法建立channel資訊的方法、并将channel節點資訊添加到rss節點上,然後循環調用createItem方法添加了多個item節點資訊。createChannel方法和createItem方法都使用了createEle方法。
思考:
當我們點選了csdn部落格中的這個rss訂閱按鈕,實際上伺服器進行了哪些操作?其實,簡單點說,不就是和我們上面的操作是一樣的嗎?都是從資料庫中讀取資訊,然後拼接成特殊的xml檔案,然後輸出到使用者浏覽器中。隻不過是人家的資料庫操作比我的複雜點,原理其實一樣。希望能幫到大家!