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 相关文章推荐
基于mysql的论坛(2)
Oct 09 PHP
php图片验证码代码
Mar 27 PHP
php下连接mssql2005的代码
Jan 17 PHP
php生成不重复随机数、数组的4种方法分享
Mar 30 PHP
PHP代码实现表单数据验证类
Jul 28 PHP
PHP 与 UTF-8 的最佳实践详细介绍
Jan 04 PHP
php 如何设置一个严格控制过期时间的session
May 05 PHP
PHP 中使用explode()函数切割字符串为数组的示例
May 06 PHP
tp5(thinkPHP5)框架数据库Db增删改查常见操作总结
Jan 10 PHP
Laravel如何创建服务器提供者实例代码
Apr 15 PHP
PHP使用反向Ajax技术实现在线客服系统详解
Jul 01 PHP
PHP检查文件是否存在,不存在自动创建及读取文件内容操作示例
Jan 23 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
2006/11/25 PHP
php zlib压缩和解压缩swf文件的代码
2008/12/30 PHP
使用PHP备份MySQL和网站发送到邮箱实例代码
2013/11/28 PHP
利用PHP命令行模式采集股票趋势信息
2016/08/09 PHP
PHP+mysql+ajax轻量级聊天室实现方法详解
2016/10/17 PHP
PHP中的use关键字及文件的加载详解
2016/11/28 PHP
PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】
2019/12/20 PHP
JQuery中的$.getJSON 使用说明
2011/03/10 Javascript
javascript内置对象arguments详解
2014/03/16 Javascript
JS实现超过长度限制后自动跳转下一款文本框的方法
2015/02/23 Javascript
JavaScript 正则表达式中global模式的特性
2016/02/25 Javascript
jQuery实时显示鼠标指针位置和键盘ASCII码
2016/03/28 Javascript
AngularJS应用开发思维之依赖注入3
2016/08/19 Javascript
详解AngularJS1.x学习directive 中‘&amp; ’‘=’ ‘@’符号的区别使用
2017/08/23 Javascript
JavaScript中递归实现的方法及其区别
2017/09/12 Javascript
vue addRoutes实现动态权限路由菜单的示例
2018/05/15 Javascript
解决vue.js this.$router.push无效的问题
2018/09/03 Javascript
浅谈Vue CLI 3结合Lerna进行UI框架设计
2019/04/14 Javascript
vue-socket.io接收不到数据问题的解决方法
2020/05/13 Javascript
微信小程序实现列表的横向滑动方式
2020/07/15 Javascript
使用python开发vim插件及心得分享
2014/11/04 Python
django自带的server 让外网主机访问方法
2018/05/14 Python
python 遍历目录(包括子目录)下所有文件的实例
2018/07/11 Python
Python 识别12306图片验证码物品的实现示例
2020/01/20 Python
Python通过正则库爬取淘宝商品信息代码实例
2020/03/02 Python
parser.add_argument中的action使用
2020/04/20 Python
python3.7 openpyxl 在excel单元格中写入数据实例
2020/09/01 Python
Python 解析xml文件的示例
2020/09/29 Python
HTML5拖拉上传文件的简单实例
2017/01/11 HTML / CSS
详解HTML5 Canvas标签及基本使用
2020/01/10 HTML / CSS
情侣吵架检讨书
2014/02/05 职场文书
根叔历年演讲稿
2014/05/20 职场文书
运动会加油口号
2014/06/07 职场文书
趣味运动会通讯稿
2015/07/18 职场文书
运动会致辞稿
2015/07/29 职场文书
详解python的内存分配机制
2021/05/10 Python