php实现的RSS生成类实例


Posted in PHP onApril 23, 2015

本文实例讲述了php实现的RSS生成类。分享给大家供大家参考。具体如下:

class RSS
{
 var $title;
 var $link;
 var $description;
 var $language = "en-us";
 var $pubDate;
 var $items;
 var $tags;
 function RSS()
 {
  $this->items = array();
  $this->tags = array();
 }
 function addItem($item)
 {
  $this->items[] = $item;
 }
 function setPubDate($when)
 {
  if(strtotime($when) == false)
   $this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
  else
   $this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
 }
 function getPubDate()
 {
  if(empty($this->pubDate))
   return date("D, d M Y H:i:s ") . "GMT";
  else
   return $this->pubDate;
 }
 function addTag($tag, $value)
 {
  $this->tags[$tag] = $value;
 }
 function out()
 {
  $out = $this->header();
  $out .= "<channel>\n";
  $out .= "<title>" . $this->title . "</title>\n";
  $out .= "<link>" . $this->link . "</link>\n";
  $out .= "<description>" . $this->description . "</description>\n";
  $out .= "<language>" . $this->language . "</language>\n";
  $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";
  foreach($this->tags as $key => $val) $out .= "<$key>$val</$key>\n";
  foreach($this->items as $item) $out .= $item->out();
  $out .= "</channel>\n";
  $out .= $this->footer();
  $out = str_replace("&", "&", $out);
  return $out;
 }
 function serve($contentType = "application/xml")
 {
  $xml = $this->out();
  header("Content-type: $contentType");
  echo $xml;
 }
 function header()
 {
  $out = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
  $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">' . "\n";
  return $out;
 }
 function footer()
 {
  return '</rss>';
 }
}
class RSSItem
{
 var $title;
 var $link;
 var $description;
 var $pubDate;
 var $guid;
 var $tags;
 var $attachment;
 var $length;
 var $mimetype;
 function RSSItem()
 { 
  $this->tags = array();
 }
 function setPubDate($when)
 {
  if(strtotime($when) == false)
   $this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
  else
   $this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
 }
 function getPubDate()
 {
  if(empty($this->pubDate))
   return date("D, d M Y H:i:s ") . "GMT";
  else
   return $this->pubDate;
 }
 function addTag($tag, $value)
 {
  $this->tags[$tag] = $value;
 }
 function out()
 {
  $out .= "<item>\n";
  $out .= "<title>" . $this->title . "</title>\n";
  $out .= "<link>" . $this->link . "</link>\n";
  $out .= "<description>" . $this->description . "</description>\n";
  $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";
  if($this->attachment != "")
   $out .= "<enclosure url='{$this->attachment}' length='{$this->length}' type='{$this->mimetype}' />";
  if(empty($this->guid)) $this->guid = $this->link;
  $out .= "<guid>" . $this->guid . "</guid>\n";

  foreach($this->tags as $key => $val) $out .= "<$key>$val</$key\n>";
  $out .= "</item>\n";
  return $out;
 }
 function enclosure($url, $mimetype, $length)
 {
  $this->attachment = $url;
  $this->mimetype  = $mimetype;
  $this->length   = $length;
 }
}

使用示例如下:

$feed = new RSS();
$feed->title    = "RSS Feed Title";
$feed->link    = "http://website.com";
$feed->description = "Recent articles on your website.";
$db->query($query);
$result = $db->result;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
  $item = new RSSItem();
  $item->title = $title;
  $item->link = $link;
  $item->setPubDate($create_date); 
  $item->description = "<![CDATA[ $html ]]>";
  $feed->addItem($item);
}
echo $feed->serve();

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

PHP 相关文章推荐
php中将网址转换为超链接的函数
Sep 02 PHP
用C/C++扩展你的PHP 为你的php增加功能
Sep 06 PHP
做了CDN获取用户真实IP的函数代码(PHP与Asp设置方式)
Apr 13 PHP
PHP下打开phpMyAdmin出现403错误的问题解决方法
May 23 PHP
PHP中多维数组的foreach遍历示例
Jun 13 PHP
php中time()与$_SERVER[REQUEST_TIME]用法区别
Nov 19 PHP
PHP网络操作函数汇总
May 18 PHP
PHP使用stream_context_create()模拟POST/GET请求的方法
Apr 02 PHP
php获取字符串前几位的实例(substr返回字符串的子串用法)
Mar 08 PHP
基于ThinkPHP实现的日历功能实例详解
Apr 15 PHP
PHP基于cookie实现统计在线人数功能示例
Jan 16 PHP
php源码的安装方法和实例
Sep 26 PHP
php利用事务处理转账问题
Apr 22 #PHP
ThinkPHP文件缓存类代码分享
Apr 22 #PHP
php文件下载处理方法分析
Apr 22 #PHP
php实现用手机关闭计算机(电脑)的方法
Apr 22 #PHP
解决ThinkPHP关闭调试模式时报错的问题汇总
Apr 22 #PHP
php文件缓存类用法实例分析
Apr 22 #PHP
php实现将wav文件转换成图像文件并在页面中显示的方法
Apr 21 #PHP
You might like
php文件操作实例代码
2012/05/10 PHP
mysql 查询指定日期时间内sql语句实现原理与代码
2012/12/16 PHP
php去除字符串中空字符的常用方法小结
2015/03/17 PHP
PHP字符串逆序排列实现方法小结【strrev函数,二分法,循环法,递归法】
2017/01/13 PHP
静态的动态续篇之来点XML
2006/12/23 Javascript
js树形控件脚本代码
2008/07/24 Javascript
jQuery 动画弹出窗体支持多种展现方式
2010/04/29 Javascript
23个Javascript弹出窗口特效整理
2011/02/25 Javascript
JQuery入门——事件切换之toggle()方法应用介绍
2013/02/05 Javascript
调试代码导致IE出错的避免方法
2014/04/04 Javascript
更快的异步执行(setTimeout多浏览器)
2014/08/12 Javascript
JavaScript实现鼠标滑过处生成气泡的方法
2015/05/16 Javascript
jQuery Mobile和HTML5开发App推广注册页
2016/11/07 Javascript
Bootstrap基本模板的使用和理解1
2016/12/14 Javascript
js实现不提示直接关闭网页窗口
2017/03/30 Javascript
基于javascript的异步编程实例详解
2017/04/10 Javascript
基于$.ajax()方法从服务器获取json数据的几种方式总结
2018/01/31 Javascript
vue页面跳转后返回原页面初始位置方法
2018/02/11 Javascript
使用Angular CLI从蓝本生成代码详解
2018/03/24 Javascript
jquery检测上传文件大小示例
2020/04/26 jQuery
JavaScript 禁止用户保存图片的实现代码
2020/04/28 Javascript
JS常见内存泄漏及解决方案解析
2020/05/30 Javascript
[40:03]DOTA2上海特级锦标赛主赛事日 - 1 败者组第一轮#1EHOME VS Archon
2016/03/02 DOTA
Python实现根据指定端口探测服务器/模块部署的方法
2014/08/25 Python
Python中内建函数的简单用法说明
2016/05/05 Python
从局部变量和全局变量开始全面解析Python中变量的作用域
2016/06/16 Python
Python的面向对象编程方式学习笔记
2016/07/12 Python
python实现各进制转换的总结大全
2017/06/18 Python
python实现集中式的病毒扫描功能详解
2019/07/09 Python
对pytorch中的梯度更新方法详解
2019/08/20 Python
python实现图片二值化及灰度处理方式
2019/12/07 Python
Python 读取 YUV(NV12) 视频文件实例
2019/12/09 Python
Ellos瑞典官网:北欧地区时尚、美容和住宅领域领先的电子商务网站
2019/11/21 全球购物
商务日语专业毕业生求职信
2013/10/26 职场文书
公司聘任书模板
2014/03/29 职场文书
2014年党员自我评议对照检查材料
2014/09/20 职场文书