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


Posted in PHP onFebruary 08, 2017

本文实例讲述了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);
?>

最后在火狐下效果:

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

PHP 相关文章推荐
discuz Passport 通行证 整合笔记
Jun 30 PHP
PHP 开发环境配置(测试开发环境)
Apr 28 PHP
php include加载文件两种方式效率比较
Aug 08 PHP
PHP中通过语义URL防止网站被攻击的方法分享
Sep 08 PHP
ThinkPHP缓存方法S()概述
Jun 13 PHP
ThinkPHP2.0读取MSSQL提示Incorrect syntax near the keyword 'AS'的解决方法
Jun 25 PHP
PHP连接和操作MySQL数据库基础教程
Sep 29 PHP
php实现求相对时间函数
Jun 15 PHP
10款实用的PHP开源工具
Oct 23 PHP
PHP内存缓存功能memcached示例
Oct 19 PHP
php 从指定数字中获取随机组合的简单方法(推荐)
Apr 05 PHP
Laravel框架运行出错提示RuntimeException No application encryption key has been specified.解决方法
Apr 02 PHP
PHP图片裁剪与缩放示例(无损裁剪图片)
Feb 08 #PHP
php实现XML和数组的相互转化功能示例
Feb 08 #PHP
PHP 获取指定地区的天气实例代码
Feb 08 #PHP
PHP使用DOM和simplexml读取xml文档的方法示例
Feb 08 #PHP
PHP判断数组是否为空的常用方法(五种方法)
Feb 08 #PHP
PHP基于DOM创建xml文档的方法示例
Feb 08 #PHP
PHP输出XML格式数据的方法总结
Feb 08 #PHP
You might like
php数组合并的二种方法
2014/03/21 PHP
YII模块实现绑定二级域名的方法
2014/07/09 PHP
PHP 搜索查询功能实现
2016/11/29 PHP
php求数组全排列,元素所有组合的方法总结
2017/03/14 PHP
PHP排序算法之基数排序(Radix Sort)实例详解
2018/04/21 PHP
Mootools 1.2教程 定时器和哈希简介
2009/09/15 Javascript
JQuery操作三大控件(下拉,单选,复选)的方法
2013/08/06 Javascript
Jquery取得iframe下内容的方法
2013/11/18 Javascript
JS计算网页停留时间代码
2014/04/28 Javascript
js 动态修改css文件用到了cssRule
2014/08/20 Javascript
javascript获取文档坐标和视口坐标
2015/05/26 Javascript
学习JavaScript设计模式之装饰者模式
2016/01/19 Javascript
Angular 2 利用Router事件和Title实现动态页面标题的方法
2017/08/23 Javascript
vue+jquery+lodash实现滑动时顶部悬浮固定效果
2018/04/28 jQuery
在angularJs中进行数据遍历的2种方法
2018/10/08 Javascript
微信打开网址添加在浏览器中打开提示的办法
2019/05/20 Javascript
VUE 组件转换为微信小程序组件的方法
2019/11/06 Javascript
vue中的 $slot 获取插槽的节点实例
2019/11/12 Javascript
微信域名检测接口调用演示步骤(含PHP、Python)
2019/12/08 Javascript
vue中的双向数据绑定原理与常见操作技巧详解
2020/03/16 Javascript
JavaScript实现刮刮乐效果
2020/11/01 Javascript
JavaScript仿京东轮播图效果
2021/02/25 Javascript
[42:27]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#2Fnatic VS OG第三局
2016/03/05 DOTA
[02:51]2018年度DOTA2最佳中单位选手-完美盛典
2018/12/17 DOTA
Python多进程编程技术实例分析
2014/09/16 Python
python爬虫之爬取百度音乐的实现方法
2019/08/24 Python
解决Tensorflow sess.run导致的内存溢出问题
2020/02/05 Python
Python 找出出现次数超过数组长度一半的元素实例
2020/05/11 Python
使用Numpy对特征中的异常值进行替换及条件替换方式
2020/06/08 Python
python requests库的使用
2021/01/06 Python
python中PyQuery库用法分享
2021/01/15 Python
Answear匈牙利:来自全球200多个知名时尚品牌
2017/04/21 全球购物
世界上最大的专业美容用品零售商:Sally Beauty
2017/07/02 全球购物
教师创先争优承诺书
2015/04/27 职场文书
Python制作一个随机抽奖小工具的实现
2021/07/07 Python
django项目、vue项目部署云服务器的详细过程
2022/07/23 Servers