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

PHP生成RSS订阅的程序代码-PHP源码

生成RSS非常的简单只需要使用rss格式然后生成输出到浏览器就可以了,重点就是要告诉浏览器你是rss格式的数据即可,具体来看个例子。
生成RSS非常的简单只需要使用rss格式然后生成输出到浏览器就可以了,重点就是要告诉浏览器你是rss格式的数据即可,具体来看个例子。


rss(简易信息聚合也叫聚合内容)是一种描述和同步网站内容的格式。下面的生成RSS订阅的代码:
rss XML结构





http://www.111cn.net/
网站描述!


网站地址/rss
New RSS tutorial on W3School



网站地址/xml
New XML tutorial on W3School


RSS实例





'.$webUrl.'
'.$webDesc.'
'.$this->createItem().'


';
echo $html;
}
private function createItem() {
//RSS item
//$data可替换为自己的数据
$html = '';
//文章数据
$data = array(
'id' => 1,
'date' => date('r', time()),
'title' => '文章标题',
'link' => 'http://www.111cn.net',    //文章地址
'description' => '网站描述'
);
for($i = 0; $i <6; $i++) {
$html .= &#39;


&#39;.$data[&#39;link&#39;].&#39;
&#39;.$data[&#39;date&#39;].&#39;


&#39;;
}
return $html;
}
}
header("Content-Type: text/xml; charset=utf-8");
$rss = new Rss();
$rss->createFeed();
exit;
?>

RSS Feed 生成后,如何设置才能给网站添加 RSS 呢?并且让 Firefox、IE7 或其它 Feed 机器人自动发现?很简单,在网页的 Head 节添加一个特定的 Link 标签即可,如下:

设置 title 为 Feed 标题,href 为 Feed 地址,一切就 OK 了!


例子2









http://www.111cn.net/

本站是一个php程序员的工作生活笔记!





网站地址/rss

New RSS tutorial on W3School







网站地址/xml

New XML tutorial on W3School





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

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 .= "cssStyleSheet."\" type=\"text/css\"?>\n";
  if($this->xslStyleSheet) $xml .= "xslStyleSheet."\" type=\"text/xsl\"?>\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("~(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s+)?(\\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)==&#39;+&#39; OR substr($matches[7],0,1)==&#39;-&#39;){ $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 = 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)==&#39;+&#39; OR substr($matches[7],0,1)==&#39;-&#39;){ $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) . &#39;:&#39; . 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="encoding."\"?>\n"; $feed.= $this->_createGeneratorComment(); if($this->cssStyleSheet==""){ $cssStyleSheet="http://www.w3.org/2000/08/w3c-synd/style.css"; } $feed.= $this->_createStylesheetReferences(); $feed.= "\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="encoding."\"?>\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"); } }

使用示例:

title="页面标题";
$rss->link="网址http://";
$rss->description="rss标题";
while($rowbrs=mysql_fetch_array($brs)){
 $item=new FeedItem();
 $item->title =$rowbrs[&#39;subject&#39;];
 $item->link=&#39;http://www.111cn.net/&#39;;
 $item->description =$rowbrs[&#39;description&#39;];
 $rss->addItem($item);
}
mysql_close($db);
$rss->saveFeed("RSS2.0","rss.xml");

推荐阅读
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 分享css中提升优先级属性!important的用法总结
    web前端|css教程css!importantweb前端-css教程本文分享css中提升优先级属性!important的用法总结微信门店展示源码,vscode如何管理站点,ubu ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 本文介绍了使用cacti监控mssql 2005运行资源情况的操作步骤,包括安装必要的工具和驱动,测试mssql的连接,配置监控脚本等。通过php连接mssql来获取SQL 2005性能计算器的值,实现对mssql的监控。详细的操作步骤和代码请参考附件。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
author-avatar
谢海武181_160
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有