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 相关文章推荐
Smarty结合Ajax实现无刷新留言本实例
Jan 02 PHP
phpinfo 系统查看参数函数代码
Jun 05 PHP
php版本的cron定时任务执行器使用实例
Aug 19 PHP
php导入模块文件分享
Mar 17 PHP
jQuery+php简单实现全选删除的方法
Nov 28 PHP
php 调用ffmpeg获取视频信息的简单实现
Apr 03 PHP
php静态成员方法和静态的成员属性的使用方法
Oct 26 PHP
在php的yii2框架中整合hbase库的方法
Sep 20 PHP
PHP创建XML接口示例
Jul 04 PHP
Laravel框架实现的上传图片到七牛功能详解
Sep 06 PHP
详解Laravel服务容器的绑定与解析
Nov 05 PHP
php去除数组中为0的元素的实例分析
Nov 17 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
第八节 访问方式 [8]
2006/10/09 PHP
php操作redis中的hash和zset类型数据的方法和代码例子
2014/07/05 PHP
php实现网站顶踩功能的完整前端代码
2015/07/19 PHP
Jquery 基础学习笔记
2009/05/29 Javascript
详解JavaScript中undefined与null的区别
2014/03/29 Javascript
jQuery实现的倒计时效果实例小结
2016/04/16 Javascript
jQuery鼠标事件总结
2016/10/13 Javascript
怎样判断jQuery当前元素是隐藏还是显示
2016/11/23 Javascript
原生JS实现层叠轮播图
2017/05/17 Javascript
详解Vue 中 extend 、component 、mixins 、extends 的区别
2017/12/20 Javascript
在vue中使用Autoprefixed的方法
2018/07/27 Javascript
node.js遍历目录的方法示例
2018/08/01 Javascript
js纯前端实现腾讯cos文件上传功能的示例代码
2019/05/14 Javascript
layui问题之模拟table表格中的选中按钮选中事件的方法
2019/09/20 Javascript
详解JavaScript 作用域
2020/07/14 Javascript
js实现滑动滑块验证登录
2020/07/24 Javascript
js实现前端界面导航栏下拉列表
2020/08/27 Javascript
Django Highcharts制作图表
2016/08/27 Python
浅谈python可视化包Bokeh
2018/02/07 Python
Python提取支付宝和微信支付二维码的示例代码
2019/02/15 Python
Python绘制股票移动均线的实例
2019/08/24 Python
python3.8与pyinstaller冲突问题的快速解决方法
2020/01/16 Python
Python 解决相对路径问题:&quot;No such file or directory&quot;
2020/06/05 Python
pytorch快速搭建神经网络_Sequential操作
2020/06/17 Python
Python配置pip国内镜像源的实现
2020/08/20 Python
python能做哪些生活有趣的事情
2020/09/09 Python
html5指南-5.使用web storage存储键值对的数据
2013/01/07 HTML / CSS
加拿大时尚潮流大码女装购物网站:Addition Elle
2018/04/02 全球购物
学习十八大精神心得体会
2013/12/31 职场文书
电子专业毕业生自我鉴定
2014/01/22 职场文书
模具毕业生推荐信
2014/02/15 职场文书
学生社团文化节开幕式主持词
2014/03/28 职场文书
酒店七夕情人节活动策划方案
2014/08/24 职场文书
2015学生会文艺部工作总结
2015/04/03 职场文书
2015年文秘个人工作总结
2015/10/14 职场文书
Nginx服务器添加Systemd自定义服务过程解析
2021/03/31 Servers