php制作基于xml的RSS訂閱源功能示例
本文實(shí)例講述了php制作基于xml的RSS訂閱源功能。分享給大家供大家參考,具體如下:
首先制作一個(gè) RSS 模板,模板的文件名是 feed.xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?> <rss version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/"></rss>
再就是用php文件從數(shù)據(jù)庫讀取數(shù)據(jù)并生成 RSS 文件,這里用一個(gè)數(shù)組模擬從數(shù)據(jù)庫讀取的數(shù)據(jù):
<?php
class Rss{
protected $dom = null;
protected $temp = './feed.xml';
protected $rss = null;
protected $title = '';
protected $desc = '';
protected $link = '';
public function __construct(){
$this->title = '物理學(xué)';
$this->desc = '現(xiàn)代物理學(xué)';
$this->link = 'http://mysql/rss.php';
$this->dom = new DOMDocument('1.0','utf-8');
$this->dom->load($this->temp);
$this->rss = $this->dom->getElementsByTagName('rss')->item(0);
}
public function feed($arr){
$this->createChannel();
$channel = $this->dom->getElementsByTagName('channel')->item(0);
foreach ($arr as $v){
$channel->appendChild($this->createItem($v));
}
header('content-type:text/xml');
echo $this->dom->savexml();
}
protected 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->desc));
$this->rss->appendChild($channel);
}
protected function createItem($arr){
$item = $this->dom->createElement('item');
foreach($arr as $k => $v){
$item->appendChild($this->createEle($k,$v));
}
return $item;
}
protected function createEle($name,$value){
$e=$this->dom->createElement($name);
$t=$this->dom->createTextNode($value);
$e->appendChild($t);
return $e;
}
}
$arr = array(
array(
'title'=>'牛頓力學(xué)',
'link'=>'1',
'description'=>'牛頓力學(xué)'
),
array(
'title'=>'相對(duì)論',
'link'=>'1',
'description'=>'愛因斯坦的相對(duì)論'
)
);
$rss = new Rss;
$rss->feed($arr);
?>
最后在火狐下效果:

PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP針對(duì)XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP錯(cuò)誤與異常處理方法總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php實(shí)現(xiàn)XML和數(shù)組的相互轉(zhuǎn)化功能示例
- PHP使用DOM和simplexml讀取xml文檔的方法示例
- PHP基于DOM創(chuàng)建xml文檔的方法示例
- PHP輸出XML格式數(shù)據(jù)的方法總結(jié)
- php基于dom實(shí)現(xiàn)讀取圖書xml格式數(shù)據(jù)的方法
- php基于dom實(shí)現(xiàn)的圖書xml格式數(shù)據(jù)示例
- php實(shí)現(xiàn)xml轉(zhuǎn)換數(shù)組的方法示例
- PHP實(shí)現(xiàn)的XML操作類【XML Library】
- PHP數(shù)組生成XML格式數(shù)據(jù)的封裝類實(shí)例
- PHP對(duì)XML內(nèi)容進(jìn)行修改和刪除實(shí)例代碼
- php實(shí)現(xiàn)的XML操作(讀取)封裝類完整實(shí)例
相關(guān)文章
Android ProgressBar進(jìn)度條和ProgressDialog進(jìn)度框的展示DEMO
本篇文章是對(duì)Android中ProgressBar進(jìn)度條和ProgressDialog進(jìn)度框的展示DEMO進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php函數(shù)傳值的引用傳遞注意事項(xiàng)分析
這篇文章主要介紹了php函數(shù)傳值的引用傳遞注意事項(xiàng),涉及php配置文件的設(shè)置及數(shù)組函數(shù)的使用技巧,需要的朋友可以參考下2016-06-06
jquery+php+ajax顯示上傳進(jìn)度的多圖片上傳并生成縮略圖代碼
這篇文章主要介紹了jquery+php+ajax顯示上傳進(jìn)度的多圖片上傳并生成縮略圖代碼,可實(shí)現(xiàn)帶有進(jìn)度條效果的多圖上傳功能與生成縮略圖功能,是web開發(fā)中非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10
Win2003下IIS+PHP+MySQL+Zend配置步驟詳解
Win2003下IIS+PHP+MySQL+Zend配置步驟詳解...2007-05-05
編寫安全 PHP應(yīng)用程序的七個(gè)習(xí)慣深入分析
本篇文章是對(duì)編寫安全-PHP應(yīng)用程序的七個(gè)習(xí)慣進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

