-
php实现的RSS生成类实例
这篇文章主要介绍了php实现的RSS生成类,实例分析了RSS生成类的原理、定义与使用技巧,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了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(emptyempty($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(emptyempty($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(emptyempty($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();
出处:http://www.phpfensi.com/php/20210522/15772.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式