热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php生成RSS订阅的方法

这篇文章主要介绍了php生成RSS订阅的方法,较为详细的分析了一个RSS订阅类及其具体使用技巧,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了php生成RSS订阅的方法。分享给大家供大家参考。具体分析如下:

RSS(简易信息聚合,也叫聚合内容)是一种描述和同步网站内容的格式。RSS可以是以下三个解释的其中一个: Really Simple Syndication;RDF (Resource Description Framework) Site Summary; Rich Site Summary。但其实这三个解释都是指同一种Syndication的技术。RSS目前广泛用于网上新闻频道,blog和wiki。使用RSS订阅能更快地获取信息,网站提供RSS输出,有利于让用户获取网站内容的最新更新。网络用户可以在客户端借助于支持RSS的聚合工具软件,在不打开网站内容页面的情况下阅读支持RSS输出的网站内容。
从技术上来说一个RSS文件就是一段规范的XML数据,该文件一般以rss,xml或者rdf作为后缀,下面是一段 rss 文件的内容示例:

代码如下:

<&#63;xml version="1.0" encoding="utf-8"&#63;>



http://www.jb51.net/
脚本之家


网站地址/rss
New RSS tutorial on W3School



网站地址/xml
New XML tutorial on W3School


下面分享一段使用 php 动态生成 RSS 的代码示例:

代码如下:

<&#63;php
/**
** php 动态生成 RSS 类
**/
define("TIME_ZONE","");
define("FEEDCREATOR_VERSION","www.jb51.net");//您的网址
class FeedItem extends HtmlDescribable{
var $title,$description,$link;
var $author,$authorEmail,$image,$category,$comments,$guid,$source,$creator;
var $date;
var $additiOnalElements=Array();
}

class FeedImage extends HtmlDescribable{
var $title,$url,$link;
var $width,$height,$description;
}

class HtmlDescribable{
var $descriptionHtmlSyndicated;
var $descriptionTruncSize;

function getDescription(){
$descriptiOnField=new FeedHtmlField($this->description);
$descriptionField->syndicateHtml=$this->descriptionHtmlSyndicated;
$descriptionField->truncSize=$this->descriptionTruncSize;
return $descriptionField->output();
}
}

class FeedHtmlField{
var $rawFieldContent;
var $truncSize,$syndicateHtml;
function FeedHtmlField($parFieldContent){
if($parFieldContent){
$this->rawFieldCOntent=$parFieldContent;
}
}
function output(){
if(!$this->rawFieldContent){
$result="";
} elseif($this->syndicateHtml){
$result="rawFieldContent."]]>";
}else{
if($this->truncSize and is_int($this->truncSize)){
$result=FeedCreator::iTrunc(htmlspecialchars($this->rawFieldContent),$this->truncSize);
}else{
$result=htmlspecialchars($this->rawFieldContent);
}
}
return $result;
}
}

class UniversalFeedCreator extends FeedCreator{
var $_feed;
function _setFormat($format){
switch (strtoupper($format)){
case "2.0":
// fall through
case "RSS2.0":
$this->_feed=new RSSCreator20();
break;
case "0.91":
// fall through
case "RSS0.91":
$this->_feed=new RSSCreator091();
break;
default:
$this->_feed=new RSSCreator091();
break;
}
$vars=get_object_vars($this);
foreach ($vars as $key => $value){
// prevent overwriting of properties "contentType","encoding"; do not copy "_feed" itself
if(!in_array($key, array("_feed","contentType","encoding"))){
$this->_feed->{$key}=$this->{$key};
}
}
}

function createFeed($format="RSS0.91"){
$this->_setFormat($format);
return $this->_feed->createFeed();
}

function saveFeed($format="RSS0.91",$filename="",$displayCOntents=true){
$this->_setFormat($format);
$this->_feed->saveFeed($filename,$displayContents);
}

function useCached($format="RSS0.91",$filename="",$timeout=3600){
$this->_setFormat($format);
$this->_feed->useCached($filename,$timeout);
}
}

class FeedCreator extends HtmlDescribable{
var $title,$description,$link;
var $syndicationURL,$image,$language,$copyright,$pubDate,$lastBuildDate,$editor,$editorEmail,$webmaster,$category,$docs,$ttl,$rating,$skipHours,$skipDays;
var $xslStyleSheet="";
var $items=Array();
var $cOntentType="application/xml";
var $encoding="utf-8";
var $additiOnalElements=Array();

function addItem($item){
$this->items[]=$item;
}

function clearItem2Null(){
$this->items=array();
}

function iTrunc($string,$length){
if(strlen($string)<=$length){
return $string;
}

$pos=strrpos($string,".");
if($pos>=$length-4){
$string=substr($string,0,$length-4);
$pos=strrpos($string,".");
}
if($pos>=$length*0.4){
return substr($string,0,$pos+1)." ...";
}

$pos=strrpos($string," ");
if($pos>=$length-4){
$string=substr($string,0,$length-4);
$pos=strrpos($string," ");
}
if($pos>=$length*0.4){
return substr($string,0,$pos)." ...";
}

return substr($string,0,$length-4)." ...";
}

function _createGeneratorComment(){
return "\n";
}

function _createAdditionalElements($elements,$indentString=""){
$ae="";
if(is_array($elements)){
foreach($elements AS $key => $value){
$ae.= $indentString."<$key>$value\n";
}
}
return $ae;
}

function _createStylesheetReferences(){
$xml="";
if($this->cssStyleSheet) $xml .= "<&#63;xml-stylesheet href=\"".$this->cssStyleSheet."\" type=\"text/css\"&#63;>\n";
if($this->xslStyleSheet) $xml .= "<&#63;xml-stylesheet href=\"".$this->xslStyleSheet."\" type=\"text/xsl\"&#63;>\n";
return $xml;
}

function createFeed(){}

function _generateFilename(){
$fileInfo=pathinfo($_SERVER["PHP_SELF"]);
return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".xml";
}

function _redirect($filename){
Header("Content-Type: ".$this->contentType."; charset=".$this->encoding."; filename=".basename($filename));
Header("Content-Disposition: inline; filename=".basename($filename));
readfile($filename,"r");
die();
}

function useCached($filename="",$timeout=3600){
$this->_timeout=$timeout;
if($filename==""){
$filename=$this->_generateFilename();
}
if(file_exists($filename) && (time()-filemtime($filename) <$timeout)){
$this->_redirect($filename);
}
}

function saveFeed($filename="",$displayCOntents=true){
if($filename==""){
$filename=$this->_generateFilename();
}
$feedFile=fopen($filename,"w+");
if($feedFile){
fputs($feedFile,$this->createFeed());
fclose($feedFile);
if($displayContents){
$this->_redirect($filename);
}
}else{
echo "
Error creating feed file, please check write permissions.
";
}
}
}

class FeedDate{
var $unix;
function FeedDate($dateString=""){
if($dateString=="") $dateString=date("r");
if(is_integer($dateString)){
$this->unix=$dateString;
return;
}
if(preg_match("~(&#63;:(&#63;:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s+)&#63;(\\d{1,2})\\s+([a-zA-Z]{3})\\s+(\\d{4})\\s+(\\d{2}):(\\d{2}):(\\d{2})\\s+(.*)~",$dateString,$matches)){
$mOnths=Array("Jan"=>1,"Feb"=>2,"Mar"=>3,"Apr"=>4,"May"=>5,"Jun"=>6,"Jul"=>7,"Aug"=>8,"Sep"=>9,"Oct"=>10,"Nov"=>11,"Dec"=>12);
$this->unix=mktime($matches[4],$matches[5],$matches[6],$months[$matches[2]],$matches[1],$matches[3]);
if(substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-'){
$tzOffset=(substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
}else{
if(strlen($matches[7])==1){
$OneHour=3600;
$ord=ord($matches[7]);
if($ord $tzOffset=(ord("A") - $ord - 1) * $oneHour;
} elseif($ord >= ord("M") && $matches[7]!="Z"){
$tzOffset=($ord - ord("M")) * $oneHour;
} elseif($matches[7]=="Z"){
$tzOffset=0;
}
}
switch ($matches[7]){
case "UT":
case "GMT": $tzOffset=0;
}
}
$this->unix += $tzOffset;
return;
}
if(preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~",$dateString,$matches)){
$this->unix=mktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]);
if(substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-'){
$tzOffset=(substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
}else{
if($matches[7]=="Z"){
$tzOffset=0;
}
}
$this->unix += $tzOffset;
return;
}
$this->unix=0;
}

function rfc822(){
$date=gmdate("Y-m-d H:i:s",$this->unix);
if(TIME_ZONE!="") $date .= " ".str_replace(":","",TIME_ZONE);
return $date;
}

function iso8601(){
$date=gmdate("Y-m-d H:i:s",$this->unix);
$date=substr($date,0,22) . ':' . substr($date,-2);
if(TIME_ZONE!="") $date=str_replace("+00:00",TIME_ZONE,$date);
return $date;
}

function unix(){
return $this->unix;
}
}

class RSSCreator10 extends FeedCreator{
function createFeed(){
$feed="<&#63;xml version=\"1.0\" encoding=\"".$this->encoding."\"&#63;>\n";
$feed.= $this->_createGeneratorComment();
if($this->cssStyleSheet==""){
$cssStyleSheet="http://www.w3.org/2000/08/w3c-synd/style.css?7.1.2";
}
$feed.= $this->_createStylesheetReferences();
$feed.= " $feed.= " xmlns=\"http://purl.org/rss/1.0/\"\n";
$feed.= " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n";
$feed.= " xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"\n";
$feed.= " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n";
$feed.= " syndicationURL."\">\n";
$feed.= " \n";
$feed.= " ".htmlspecialchars($this->description)."\n";
$feed.= " ".$this->link."\n";
if($this->image!=null){
$feed.= " image->url."\" />\n";
}
$now=new FeedDate();
$feed.= " ".htmlspecialchars($now->iso8601())."\n";
$feed.= " \n";
$feed.= " \n";
for ($i=0;$iitems);$i++){
$feed.= " items[$i]->link)."\"/>\n";
}
$feed.= "
\n";
$feed.= "
\n";
$feed.= "
\n";
if($this->image!=null){
$feed.= " image->url."\">\n";
$feed.= " \n";
$feed.= " ".$this->image->link."\n";
$feed.= " ".$this->image->url."\n";
$feed.= " \n";
}
$feed.= $this->_createAdditionalElements($this->additionalElements," ");

for ($i=0;$iitems);$i++){
$feed.= " items[$i]->link)."\">\n";
//$feed.= " Posting\n";
$feed.= " text/html\n";
if($this->items[$i]->date!=null){
$itemDate=new FeedDate($this->items[$i]->date);
$feed.= " ".htmlspecialchars($itemDate->iso8601())."\n";
}
if($this->items[$i]->source!=""){
$feed.= " ".htmlspecialchars($this->items[$i]->source)."\n";
}
if($this->items[$i]->author!=""){
$feed.= " ".htmlspecialchars($this->items[$i]->author)."\n";
}
$feed.= " \n";
$feed.= " ".htmlspecialchars($this->items[$i]->link)."\n";
$feed.= " ".htmlspecialchars($this->items[$i]->description)."\n";
$feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements," ");
$feed.= "
\n";
}
$feed.= "\n";
return $feed;
}
}

class RSSCreator091 extends FeedCreator{
var $RSSVersion;

function RSSCreator091(){
$this->_setRSSVersion("0.91");
$this->cOntentType="application/rss+xml";
}

function _setRSSVersion($version){
$this->RSSVersion=$version;
}

function createFeed(){
$feed="<&#63;xml version=\"1.0\" encoding=\"".$this->encoding."\"&#63;>\n";
$feed.= $this->_createGeneratorComment();
$feed.= $this->_createStylesheetReferences();
$feed.= "RSSVersion."\">\n";
$feed.= " \n";
$feed.= " \n";
$this->descriptiOnTruncSize=500;
$feed.= " ".$this->getDescription()."\n";
$feed.= " ".$this->link."\n";
$now=new FeedDate();
$feed.= " ".htmlspecialchars($now->rfc822())."\n";
$feed.= " ".FEEDCREATOR_VERSION."\n";

if($this->image!=null){
$feed.= " \n";
$feed.= " ".$this->image->url."\n";
$feed.= " \n";
$feed.= " ".$this->image->link."\n";
if($this->image->width!=""){
$feed.= " ".$this->image->width."\n";
}
if($this->image->height!=""){
$feed.= " ".$this->image->height."\n";
}
if($this->image->description!=""){
$feed.= " ".$this->image->getDescription()."\n";
}
$feed.= " \n";
}
if($this->language!=""){
$feed.= " ".$this->language."\n";
}
if($this->copyright!=""){
$feed.= " ".FeedCreator::iTrunc(htmlspecialchars($this->copyright),100)."\n";
}
if($this->editor!=""){
$feed.= " ".FeedCreator::iTrunc(htmlspecialchars($this->editor),100)."\n";
}
if($this->webmaster!=""){
$feed.= " ".FeedCreator::iTrunc(htmlspecialchars($this->webmaster),100)."\n";
}
if($this->pubDate!=""){
$pubDate=new FeedDate($this->pubDate);
$feed.= " ".htmlspecialchars($pubDate->rfc822())."\n";
}
if($this->category!=""){
$feed.= " ".htmlspecialchars($this->category)."\n";
}
if($this->docs!=""){
$feed.= " ".FeedCreator::iTrunc(htmlspecialchars($this->docs),500)."\n";
}
if($this->ttl!=""){
$feed.= " ".htmlspecialchars($this->ttl)."\n";
}
if($this->rating!=""){
$feed.= " ".FeedCreator::iTrunc(htmlspecialchars($this->rating),500)."\n";
}
if($this->skipHours!=""){
$feed.= " ".htmlspecialchars($this->skipHours)."\n";
}
if($this->skipDays!=""){
$feed.= " ".htmlspecialchars($this->skipDays)."\n";
}
$feed.= $this->_createAdditionalElements($this->additionalElements," ");

for ($i=0;$iitems);$i++){
$feed.= " \n";
$feed.= " \n";
$feed.= " ".htmlspecialchars($this->items[$i]->link)."\n";
$feed.= " ".$this->items[$i]->getDescription()."\n";

if($this->items[$i]->author!=""){
$feed.= " ".htmlspecialchars($this->items[$i]->author)."\n";
}
/*
// on hold
if($this->items[$i]->source!=""){
$feed.= " ".htmlspecialchars($this->items[$i]->source)."\n";
}
*/
if($this->items[$i]->category!=""){
$feed.= " ".htmlspecialchars($this->items[$i]->category)."\n";
}
if($this->items[$i]->comments!=""){
$feed.= " ".htmlspecialchars($this->items[$i]->comments)."\n";
}
if($this->items[$i]->date!=""){
$itemDate=new FeedDate($this->items[$i]->date);
$feed.= " ".htmlspecialchars($itemDate->rfc822())."\n";
}
if($this->items[$i]->guid!=""){
$feed.= " ".htmlspecialchars($this->items[$i]->guid)."\n";
}
$feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements," ");
$feed.= "
\n";
}
$feed.= "
\n";
$feed.= "
\n";
return $feed;
}
}

class RSSCreator20 extends RSSCreator091{

function RSSCreator20(){
parent::_setRSSVersion("2.0");
}
}


使用示例:

代码如下:

<&#63;php
header('Content-Type:text/html; charset=utf-8');
$db=mysql_connect('127.0.0.1','root','123456');
mysql_query("set names utf8");
mysql_select_db('dbname',$db);
$brs=mysql_query('select * from article order by add_time desc limit 0,10',$db);
$rss=new UniversalFeedCreator();
$rss->title="页面标题";
$rss->link="网址http://";
$rss->description="rss标题";
while($rowbrs=mysql_fetch_array($brs)){
$item=new FeedItem();
$item->title =$rowbrs['subject'];
$item->link='http://www.jb51.net/';
$item->description =$rowbrs['description'];
$rss->addItem($item);
}
mysql_close($db);
$rss->saveFeed("RSS2.0","rss.xml");

希望本文所述对大家的php程序设计有所帮助。

推荐阅读
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
author-avatar
手浪用户2602916903
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有