<?php
class Rss {
/*
* 必须的
* */
public $title = null;
public $link = null;
public $description = null;
public $template = './xml4.xml';
protected $xml = null;
public $array = array();
protected $rss = null;
/*
* 初始化dom
* */
function __construct() {
$this -> xml = new DOMDocument('1.0', 'utf-8');
$this -> xml -> load($this -> template);
$this -> rss = $this -> xml -> getElementsByTagName('rss') -> item(0);
}
/*
* 调用函数把所有的节点都生成在输出
* */
function display() {
$this -> createChannel();
$this -> addItem($this -> array);
header('content-type:text/xml,charset=utf-8');
echo $this -> xml -> saveXML();
}
/*
* 创建唯一的根节点
* */
protected function createChannel() {
$channel = $this -> xml -> createElement('channel');
$channel -> appendChild($this -> createEle('title', $this -> title));
$channel -> appendChild($this -> createEle('link', $this -> link));
$channel -> appendChild($this -> createEle('description', $this -> description));
$this -> rss -> appendChild($channel);
}
/*
* 添加数据
* */
function addItem($array) {
foreach ($array as $v) {
$this -> rss -> appendChild($this -> createItem($v));
}
}
/*
* 构造标签
* */
protected function createEle($name, $value) {
$create = $this -> xml -> createElement($name);
$text = $this -> xml -> createTextNode($value);
$create -> appendChild($text);
return $create;
}
/*
* 创建item
* */
protected function createItem($arr) {
$item = $this -> xml -> createElement('item');
foreach ($arr as $k => $v) {
$item -> appendChild($this -> createEle($k, $v));
}
return $item;
}
}
$pdo = new PDO('mysql:host=127.0.0.1;dbname=other', 'root', 'root');
$sql = 'select id,name,email from email limit 2,3';
$array = $pdo -> query($sql) -> fetchAll(PDO::FETCH_ASSOC);
$Rss = new Rss();
$Rss -> title = 'Boss';
$Rss -> link = 'www.iebshop.com';
$Rss -> description = '这是二次开发';
$Rss -> array = $array;
$Rss -> display();
/*
这是加载xml的模板
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:wfw="http://wellformedweb.org/CommentAPI/" version="2.0"></rss>
*/
rss订阅
最新推荐文章于 2024-04-01 16:37:17 发布