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 相关文章推荐
zend api扩展的php对象的autoload工具
Apr 18 PHP
PHP的explode和implode的使用说明
Jul 17 PHP
基于PHP字符串的比较函数strcmp()与strcasecmp()的使用详解
May 15 PHP
深入PHP empty(),isset(),is_null()的实例测试详解
Jun 06 PHP
PHP二维数组排序的3种方法和自定义函数分享
Apr 09 PHP
PHP如何将log信息写入服务器中的log文件
Jul 29 PHP
如何解决PHP无法实现多线程的问题
Sep 25 PHP
PHP对象链式操作实现原理分析
Oct 09 PHP
php rmdir使用递归函数删除非空目录实例详解
Oct 20 PHP
PHP实现的曲线统计图表示例
Nov 10 PHP
零基础php编程好学吗
Oct 11 PHP
PHP常用函数之格式化时间操作示例
Oct 21 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用户指南-cookies部分
2006/10/09 PHP
IP攻击升级,程序改进以对付新的攻击
2010/11/23 PHP
PHP中通过语义URL防止网站被攻击的方法分享
2011/09/08 PHP
php使用explode()函数将字符串拆分成数组的方法
2015/02/17 PHP
PHP用户管理中常用接口调用实例及解析(含源码)
2017/03/09 PHP
详解PHP使用Redis存储session时的一个Warning定位
2017/07/05 PHP
javascript Base类 包含基本的方法
2009/07/22 Javascript
一个js控制的导航菜单实例代码
2013/12/03 Javascript
JavaScript字符串对象substr方法入门实例(用于截取字符串)
2014/10/16 Javascript
在JavaScript中call()与apply()区别
2016/01/22 Javascript
js实现tab选项卡切换功能
2017/01/13 Javascript
浅谈js-FCC算法Friendly Date Ranges(详解)
2017/04/10 Javascript
vue+iview 实现可编辑表格的示例代码
2018/10/31 Javascript
Easyui 关闭jquery-easui tab标签页前触发事件的解决方法
2019/04/28 jQuery
简述vue-cli中chainWebpack的使用方法
2019/07/30 Javascript
es6中new.target的作用和使用场景简单示例分析
2020/03/14 Javascript
原生JS实现天气预报
2020/06/16 Javascript
JavaScript函数柯里化实现原理及过程
2020/12/02 Javascript
[02:43]2014DOTA2国际邀请赛 官方Alliance战队纪录片
2014/07/14 DOTA
Python实现随机选择元素功能
2017/09/14 Python
python如何重载模块实例解析
2018/01/25 Python
python将一个英文语句以单词为单位逆序排放的方法
2018/12/20 Python
Python使用folium excel绘制point
2019/01/03 Python
基于Python实现下载网易音乐代码实例
2020/08/10 Python
利用CSS3实现单选框动画特效示例代码
2016/09/26 HTML / CSS
罗马尼亚在线杂货店:Pilulka.ro
2019/09/28 全球购物
党员思想汇报范文
2013/12/30 职场文书
爱情保证书范文
2014/02/01 职场文书
自强之星事迹材料
2014/05/12 职场文书
大学专科求职信
2014/07/02 职场文书
典型事迹材料范文
2014/12/29 职场文书
数学教师求职信范文
2015/03/20 职场文书
背起爸爸上学观后感
2015/06/08 职场文书
Go各时间字符串使用解析
2021/04/02 Golang
HTML+CSS制作心跳特效的实现
2021/05/26 HTML / CSS
详解Python函数print用法
2021/06/18 Python