《PHP編程:php制作基于xml的RSS訂閱源功能示例》要點:
本文介紹了PHP編程:php制作基于xml的RSS訂閱源功能示例,希望對您有用。如果有疑問,可以聯系我們。
本文實例講述了php制作基于xml的RSS訂閱源功能.分享給大家供大家參考,具體如下:PHP實例
首先制作一個 RSS 模板,模板的文件名是 feed.xml,代碼如下:PHP實例
<?xml version="1.0" encoding="utf-8"?> <rss version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/"></rss>
再就是用php文件從數據庫讀取數據并生成 RSS 文件,這里用一個數組模擬從數據庫讀取的數據:PHP實例
<?php class Rss{ protected $dom = null; protected $temp = './feed.xml'; protected $rss = null; protected $title = ''; protected $desc = ''; protected $link = ''; public function __construct(){ $this->title = '物理學'; $this->desc = '現代物理學'; $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'=>'牛頓力學', 'link'=>'1', 'description'=>'牛頓力學' ), array( 'title'=>'相對論', 'link'=>'1', 'description'=>'愛因斯坦的相對論' ) ); $rss = new Rss; $rss->feed($arr); ?>
最后在火狐下效果:PHP實例
PHP實例
PS:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:PHP實例
在線XML/JSON互相轉換工具:
http://tools.jb51.net/code/xmljsonPHP實例
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformatPHP實例
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compressPHP實例
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformatPHP實例
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《PHP錯誤與異常處理方法總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》PHP實例
希望本文所述對大家PHP程序設計有所幫助.PHP實例
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/1837.html