php制作基于xml的RSS订阅源功能示例

yipeiwu_com5年前PHP代码库

本文实例讲述了php制作基于xml的RSS订阅源功能。分享给大家供大家参考,具体如下:

首先制作一个 RSS 模板,模板的文件名是 feed.xml,代码如下:

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

再就是用php文件从数据库读取数据并生成 RSS 文件,这里用一个数组模拟从数据库读取的数据:

<?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);
?>

最后在火狐下效果:

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具:
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

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP针对XML文件操作技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

for循环连续求和、九九乘法表代码

for循环的经典例子就是连续求和了:1+2+3+……+100,讲了一个多小时,还是有同学不会。做程序得有思想,有的同学一直敲键盘,也没搞出来。在做这个求和之前,我们要思考一下,求和其实就...

php简单构造json多维数组的方法示例

本文实例讲述了php简单构造json多维数组的方法。分享给大家供大家参考,具体如下: php构造json多维数组其实很简单 下面列举了将mysqli的查询结果数组序列化为json的php...

WordPress中获取页面链接和标题的相关PHP函数用法解析

get_permalink()(获取文章或页面链接) get_permalink() 用来根据固定连接返回文章或者页面的链接。在获取链接时 get_permalink() 函数需要知道要...

php数组函数序列之each() - 获取数组当前内部指针所指向元素的键名和键值,并将指针移到下一位

each()定义和用法 each() 函数生成一个由数组当前内部指针所指向的元素的键名和键值组成的数组,并把内部指针向前移动。 返回的数组中包括的四个元素:键名为 0,1,key 和 v...

php实现压缩合并js的方法【附demo源码下载】

本文实例讲述了php实现压缩合并js的方法。分享给大家供大家参考,具体如下: test.php文件如下: require_once('jsmin.php'); $files = gl...