rss订阅

<?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>
*/ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值