设为首页 收藏本站
查看: 522|回复: 0

[经验分享] PHP RSS/Feed 生成类库(支持RSS 1.0/2.0和ATOM)

[复制链接]

尚未签到

发表于 2017-4-13 10:23:10 | 显示全部楼层 |阅读模式
  通用PHP RSS/Feed 生成类库(支持RSS 1.0/2.0和ATOM)

PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM)
  
下载 : FeedWriter

  
生成的RSS版本:





  • RSS 1.0
    (which officially obsoleted RSS 0.90)

  • RSS
    2.0

    (which officially obsoleted RSS 0.91, 0.92,
    0.93 and 0.94)

  • ATOM 1.0
  功能:



  • 可生成RSS 1.0, RSS 2.0 和ATOM 1.0 feeds
  • 所有生成的Feed可经过验证 feed validator
    .

  • 持所有Feed属性.
  • 容易的设置channel 和 feed 条目
  • 为不同的版本使用命名空间.

  • 动转换日期格式.
  • 为ATOM feeds生成 UUID (通用唯一标识符 Universally Unique
    Identifier).
  • 支持子标签和子标签属性. (如: image 和 encloser tags)
  • 完全的
    PHP5面向对像构造 class structure.
  • 为需要的标签CDATA 编码.
  • 使用差不多的代码生成所有
    版本的feed
  使用范例:


<?php
// This is a minimum example of using the class
include("FeedWriter.php");
//Creating an instance of FeedWriter class.
$TestFeed = new FeedWriter(RSS2);
//Setting the channel elements
//Use wrapper functions for common channel elements
$TestFeed->setTitle('Testing & Checking the RSS writer class');
$TestFeed->setLink('http://www.ajaxray.com/projects/rss');
$TestFeed->setDescription('This is test of creating a RSS 2.0 feed Universal Feed Writer');
//Image title and link must match with the 'title' and 'link' channel elements for valid RSS 2.0
$TestFeed->setImage('Testing the RSS writer class','http://www.ajaxray.com/projects/rss','http://www.rightbrainsolution.com/images/logo.gif');
//Detriving informations from database addin feeds
$db->query($query);
$result = $db->result;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
//Create an empty FeedItem
$newItem = $TestFeed->createNewItem();
//Add elements to the feed item   
$newItem->setTitle($row['title']);
$newItem->setLink($row['link']);
$newItem->setDate($row['create_date']);
$newItem->setDescription($row['description']);
//Now add the feed item
$TestFeed->addItem($newItem);
}
//OK. Everything is done. Now genarate the feed.
$TestFeed->genarateFeed();
?>
   补充:

另一个生成RSS 2.0的PHP Class   

下载: class_rss_writer.php


<?php
/**
* 使用范例:
* ==============================================================
$feed = new RSS();
$feed->title       = "RSS Feed Title";
$feed->link        = "http://www.newyork.com/";
$feed->description = "Recent articles on newyork.com.";
$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();
* ==============================================================
*/
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("&", "&amp;", $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;
}
}
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-364310-1-1.html 上篇帖子: (转)Push Notification的证书及服务端php的代码方面的资料 下篇帖子: php 实现进制转换(二进制、八进制、十六进制)互相转换_转
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表