为自己的网站制作一个rss API接口

大部分是从老师那里学来的,只不过可能有细小的差别

<?php
class rss{
	public $title='rss订阅器';
	public $description='首次执行商城订阅器';
	public $link='http://localhost/shop';
	public $data=array();
	public $dom=null;
	public $root=null;
	public function __construct(){
		$this->dom=new DOMDocument('1.0','utf-8');
		$this->dom->load('rss.xml');
		$this->root=$this->dom->getElementsByTagName('rss')->item(0);
	}
	//create item
	public function createEle($elementName,$textValue){
		$ele=$this->dom->createElement($elementName);
		$text=$this->dom->createTextNode($textValue);
		$ele->appendChild($text);
		return $ele;
		
	}
	//create channel
	public function createChannel(){
		$channel=$this->dom->createElement('channel');
		$channel->appendChild($this->createEle('title', $this->title));
		$channel->appendChild($this->createEle('description', $this->description));
		$channel->appendChild($this->createEle('link',$this->link));
		$this->root->appendChild($channel);
	}
	public function addItem(){
		foreach($this->data as $v){
			$item=$this->dom->createElement('item');
			foreach($v as $key=>$value){
				$item->appendChild($this->createEle($key, $value));
			}
			$this->root->appendChild($item);
		}
	}
	public function show(){
		$this->createChannel();
		$this->addItem();
		header("content-type:text/xml");
		echo $this->dom->saveXML();
	}
}
$conn=mysql_connect("localhost",'root','');
mysql_query('set names utf8',$conn);
mysql_query('use shop');
$sql="select goods_name as title, shop_price as description from ecs_goods";
$res=mysql_query($sql,$conn);
$list=array();
while($row=(mysql_fetch_assoc($res))){
	$list[]=$row;
}
$res=new rss();
$res->data=$list;
$res->show();
?>


rss.xml代码如下


<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/"></rss>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值