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 相关文章推荐
火车头采集器3.0采集图文教程
Mar 17 PHP
PHP 定界符 使用技巧
Jun 14 PHP
PhpMyAdmin中无法导入sql文件的解决办法
Jan 08 PHP
PHP读取ACCESS数据到MYSQL的代码
May 11 PHP
PHP中::、-&amp;gt;、self、$this几种操作符的区别介绍
Apr 24 PHP
二进制交叉权限微型php类分享
Feb 07 PHP
php轻松实现中英文混排字符串截取
May 28 PHP
iis6手工创建网站后无法运行php脚本的解决方法
Jun 08 PHP
PHP实现的redis主从数据库状态检测功能示例
Jul 20 PHP
redis+php实现微博(一)注册与登录功能详解
Sep 23 PHP
TP5框架实现一次选择多张图片并预览的方法示例
Apr 04 PHP
thinkphp5.1框架模板赋值与变量输出示例
May 25 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
支持oicq头像的留言簿(二)
2006/10/09 PHP
PHP原生模板引擎 最简单的模板引擎
2012/04/25 PHP
SESSION存放在数据库用法实例
2015/08/08 PHP
标准版Eclipse搭建PHP环境的详细步骤
2015/11/18 PHP
PHP PDOStatement::debugDumpParams讲解
2019/01/30 PHP
可以把编码转换成 gb2312编码lib.UTF8toGB2312.js
2007/08/21 Javascript
js根据给定的日期计算当月有多少天实现思路及代码
2013/02/25 Javascript
JS实现多物体缓冲运动实例代码
2013/11/29 Javascript
js如何调用qq互联api实现第三方登录
2014/03/28 Javascript
js动态添加onclick事件可传参数与不传参数
2014/07/29 Javascript
jQuery的图片滑块焦点图插件整理推荐
2014/12/07 Javascript
优化RequireJS项目的相关技巧总结
2015/07/01 Javascript
老生常谈js动态添加事件--- 事件委托
2016/07/19 Javascript
JS 根据子网掩码,网关计算出所有IP地址范围示例
2020/04/23 Javascript
node的process以及child_process模块学习笔记
2018/03/06 Javascript
JavaScript原型式继承实现方法
2019/11/06 Javascript
用vue写一个日历
2020/11/02 Javascript
使用Python下载Bing图片(代码)
2013/11/07 Python
简单理解Python中的装饰器
2015/07/31 Python
Scrapy-redis爬虫分布式爬取的分析和实现
2017/02/07 Python
使用python进行广告点击率的预测的实现
2019/07/04 Python
Python values()与itervalues()的用法详解
2019/11/27 Python
关于python中plt.hist参数的使用详解
2019/11/28 Python
纯CSS3实现带动画效果导航菜单无需js
2013/09/27 HTML / CSS
英国护肤品购物网站:Beauty Expert
2016/08/19 全球购物
美国在线乐器和设备商店:Musician’s Friend
2018/07/06 全球购物
Clarks其乐鞋荷兰官网:Clarks荷兰
2019/07/05 全球购物
求职信写作要突出重点
2014/01/01 职场文书
市场部业务员岗位职责
2014/04/02 职场文书
还款承诺书范文
2014/05/20 职场文书
关于清明节的演讲稿2015
2015/03/18 职场文书
同事打架检讨书
2015/05/06 职场文书
婚礼双方父亲致辞
2015/07/27 职场文书
宾馆客房管理制度
2015/08/06 职场文书
某某店铺的开业庆典主持词范本
2019/11/25 职场文书
pycharm无法安装cv2模块问题
2022/05/20 Python