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 相关文章推荐
在apache下限制每个虚拟主机的并发数!!!!
Oct 09 PHP
使用PHP 5.0创建图形的巧妙方法
Oct 12 PHP
php设计模式 Prototype (原型模式)代码
Jun 26 PHP
解析CI的AJAX分页 另类实现方法
Jun 27 PHP
PHP闭包实例解析
Sep 08 PHP
PHP中使用Imagick实现各种图片效果实例
Jan 21 PHP
PHP实现HTML页面静态化的方法
Nov 04 PHP
PHP实现加密文本文件并限制特定页面的存取的效果
Oct 21 PHP
利用PHP判断是手机移动端还是PC端访问的函数示例
Dec 14 PHP
thinkPHP框架实现的简单计算器示例
Dec 07 PHP
laravel自定义分页的实现案例offset()和limit()
Oct 15 PHP
基于PHP实现解密或加密Cloudflar邮箱保护
Jun 24 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 输出简单动态WAP页面
2009/06/09 PHP
全局记录程序片段的运行时间 正确找到程序逻辑耗时多的断点
2011/01/06 PHP
header与缓冲区之间的深层次分析
2016/07/30 PHP
PHP设计模式之观察者模式定义与用法示例
2018/08/04 PHP
laravel model模型定义实现开启自动管理时间created_at,updated_at
2019/10/17 PHP
PHP实现随机发扑克牌
2020/04/22 PHP
js保存当前路径(cookies记录)
2010/12/14 Javascript
js字母大小写转换实现方法总结
2013/11/13 Javascript
js 动态为textbox添加下拉框数据源的方法
2014/04/24 Javascript
JS实现从连接中获取youtube的key实例
2015/07/02 Javascript
jQuery使用$.ajax进行即时验证实例详解
2015/12/11 Javascript
全面解析jQuery $(document).ready()和JavaScript onload事件
2016/06/08 Javascript
jquery根据一个值来选中select下的option实例代码
2016/08/29 Javascript
Node.js操作redis实现添加查询功能
2017/05/25 Javascript
基于 Vue.js 之 iView UI 框架非工程化实践记录(推荐)
2017/11/21 Javascript
python处理圆角图片、圆形图片的例子
2014/04/25 Python
Python实现HTTP协议下的文件下载方法总结
2016/04/20 Python
Python中偏函数用法示例
2018/06/07 Python
Django之使用celery和NGINX生成静态页面实现性能优化
2019/10/08 Python
python3 求约数的实例
2019/12/05 Python
Python time库基本使用方法分析
2019/12/13 Python
python sorted函数原理解析及练习
2020/02/10 Python
matlab中二维插值函数interp2的使用详解
2020/04/22 Python
Python中使用filter过滤列表的一个小技巧分享
2020/05/02 Python
Selenium alert 弹窗处理的示例代码
2020/08/06 Python
python实现快速文件格式批量转换的方法
2020/10/16 Python
如何用border-image实现文字气泡边框的示例代码
2020/01/21 HTML / CSS
HTML5 直播疯狂点赞动画实现代码 附源码
2020/04/14 HTML / CSS
英国Flybe航空官网:欧洲最大的独立支线廉价航空公司
2019/07/15 全球购物
美国最大的烧烤架和户外生活用品专业零售商:Barbeques Galore
2021/01/09 全球购物
城市轨道交通工程职业规划书范文
2014/01/18 职场文书
股权转让协议书范本
2014/04/12 职场文书
好的促销活动方案
2014/08/21 职场文书
护士工作失误检讨书
2014/09/14 职场文书
交通安全学习心得体会
2016/01/18 职场文书
在JavaScript中如何使用宏详解
2021/05/06 Javascript