找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1033|回复: 0
打印 上一主题 下一主题

php实现的RSS生成类实例

[复制链接]

2500

主题

2513

帖子

7520

积分

论坛元老

Rank: 8Rank: 8

积分
7520
跳转到指定楼层
楼主
发表于 2018-2-14 05:46:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了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 .= "\n";
  $out .= "" . $this->title . "\n";
  $out .= "" . $this->link . "\n";
  $out .= "" . $this->description . "\n";
  $out .= "" . $this->language . "\n";
  $out .= "" . $this->getPubDate() . "\n";
  foreach($this->tags as $key => $val) $out .= "$val\n";
  foreach($this->items as $item) $out .= $item->out();
  $out .= "\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 = '' . "\n";
  $out .= '' . "\n";
  return $out;
}
function footer()
{
  return '';
}
}
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 .= "\n";
  $out .= "" . $this->title . "\n";
  $out .= "" . $this->link . "\n";
  $out .= "" . $this->description . "\n";
  $out .= "" . $this->getPubDate() . "\n";
  if($this->attachment != "")
   $out .= "attachment}' length='{$this->length}' type='{$this->mimetype}' />";
  if(empty($this->guid)) $this->guid = $this->link;
  $out .= "" . $this->guid . "\n";
  foreach($this->tags as $key => $val) $out .= "$val";
  $out .= "
\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 = "";
  $feed->addItem($item);
}
echo $feed->serve();
希望本文所述对大家的php程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • 利用PHP和AJAX创建RSS聚合器的代码
  • 用PHP读取RSS feed的代码
  • PHP 正则表达式分析RSS
  • PHP调用Twitter的RSS的实现代码
  • php XMLWriter类的简单示例代码(RSS输出)
  • php的SimpleXML方法读写XML接口文件实例解析
  • PHP生成RSS文件类实例
  • php生成RSS订阅的方法
  • php实现将数组转换为XML的方法
  • php生成rss类用法实例
  • PHP处理数组和XML之间的互相转换
  • php制作基于xml的RSS订阅源功能示例
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端