当前位置:首页 > PHP教程 > php应用 > 列表

php生成RSS订阅的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-14 10:30:23 浏览: 评论:0 

这篇文章主要介绍了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 文件的内容示例:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <rss version="2.0"> 
  3. <channel> 
  4. <title>php粉丝网</title> 
  5. <link>https://www.phpfensi.com/</link> 
  6. <description>php粉丝网</description> 
  7. <item> 
  8. <title>RSS Tutorial</title> 
  9. <link>网站地址/rss</link> 
  10. <description>New RSS tutorial on W3School</description> 
  11. </item> 
  12. <item> 
  13. <title>XML Tutorial</title> 
  14. <link>网站地址/xml</link> 
  15. <description>New XML tutorial on W3School</description> 
  16. </item> 
  17. </channel> 
  18. </rss> 

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

  1. <?php 
  2. /** 
  3. ** php 动态生成 RSS 类 
  4. **/ 
  5. define("TIME_ZONE",""); 
  6. define("FEEDCREATOR_VERSION","www.phpfensi.com");//您的网址 
  7. class FeedItem extends HtmlDescribable{ 
  8.     var $title,$description,$link; 
  9.     var $author,$authorEmail,$image,$category,$comments,$guid,$source,$creator; 
  10.     var $date; 
  11.     var $additionalElements=Array(); 
  12. } 
  13.  
  14. class FeedImage extends HtmlDescribable{ 
  15.     var $title,$url,$link; 
  16.     var $width,$height,$description; 
  17. } 
  18.  
  19. class HtmlDescribable{ 
  20.     var $descriptionHtmlSyndicated; 
  21.     var $descriptionTruncSize; 
  22.  
  23.     function getDescription(){ 
  24.         $descriptionField=new FeedHtmlField($this->description); 
  25.         $descriptionField->syndicateHtml=$this->descriptionHtmlSyndicated; 
  26.         $descriptionField->truncSize=$this->descriptionTruncSize; 
  27.         return $descriptionField->output(); 
  28.     } 
  29. } 
  30.  
  31. class FeedHtmlField{ 
  32.     var $rawFieldContent; 
  33.     var $truncSize,$syndicateHtml; 
  34.     function FeedHtmlField($parFieldContent){ 
  35.         if($parFieldContent){ 
  36.             $this->rawFieldContent=$parFieldContent; 
  37.         } 
  38.     } 
  39.     function output(){ 
  40.         if(!$this->rawFieldContent){ 
  41.             $result=""; 
  42.         }    elseif($this->syndicateHtml){ 
  43.             $result="<![CDATA[".$this->rawFieldContent."]]>"; 
  44.         }else{ 
  45.             if($this->truncSize and is_int($this->truncSize)){ 
  46.                 $result=FeedCreator::iTrunc(htmlspecialchars($this->rawFieldContent),$this->truncSize); 
  47.             }else{ 
  48.                 $result=htmlspecialchars($this->rawFieldContent); 
  49.             } 
  50.         } 
  51.         return $result; 
  52.     } 
  53. } 
  54.  
  55. class UniversalFeedCreator extends FeedCreator{ 
  56.     var $_feed; 
  57.     function _setFormat($format){ 
  58.         switch (strtoupper($format)){ 
  59.             case "2.0": 
  60.                 // fall through 
  61.             case "RSS2.0": 
  62.                 $this->_feed=new RSSCreator20(); 
  63.                 break; 
  64.             case "0.91": 
  65.                 // fall through 
  66.             case "RSS0.91": 
  67.                 $this->_feed=new RSSCreator091(); 
  68.                 break; 
  69.             default: 
  70.                 $this->_feed=new RSSCreator091(); 
  71.                 break; 
  72.         } 
  73.         $vars=get_object_vars($this); 
  74.         foreach ($vars as $key => $value){ 
  75.             // prevent overwriting of properties "contentType","encoding"; do not copy "_feed" itself 
  76.             if(!in_array($key, array("_feed","contentType","encoding"))){ 
  77.                 $this->_feed->{$key}=$this->{$key}; 
  78.             } 
  79.         } 
  80.     } 
  81.  
  82.     function createFeed($format="RSS0.91"){ 
  83.         $this->_setFormat($format); 
  84.         return $this->_feed->createFeed(); 
  85.     } 
  86.  
  87.     function saveFeed($format="RSS0.91",$filename="",$displayContents=true){ 
  88.         $this->_setFormat($format); 
  89.         $this->_feed->saveFeed($filename,$displayContents); 
  90.     } 
  91.  
  92.     function useCached($format="RSS0.91",$filename="",$timeout=3600){ 
  93.         $this->_setFormat($format); 
  94.         $this->_feed->useCached($filename,$timeout); 
  95.     } 
  96. } 
  97.  
  98. class FeedCreator extends HtmlDescribable{ 
  99.     var $title,$description,$link; 
  100.     var $syndicationURL,$image,$language,$copyright,$pubDate,$lastBuildDate,$editor,$editorEmail,$webmaster,$category,$docs,$ttl,$rating,$skipHours,$skipDays; 
  101.     var $xslStyleSheet=""; 
  102.     var $items=Array(); 
  103.     var $contentType="application/xml"; 
  104.     var $encoding="utf-8"; 
  105.     var $additionalElements=Array(); 
  106.  
  107.     function addItem($item){ 
  108.         $this->items[]=$item; 
  109.     } 
  110.  
  111.     function clearItem2Null(){ 
  112.         $this->items=array(); 
  113.     } 
  114.  
  115.     function iTrunc($string,$length){ 
  116.         if(strlen($string)<=$length){ 
  117.             return $string; 
  118.         } 
  119.  
  120.         $pos=strrpos($string,"."); 
  121.         if($pos>=$length-4){ 
  122.             $string=substr($string,0,$length-4); 
  123.             $pos=strrpos($string,"."); 
  124.         } 
  125.         if($pos>=$length*0.4){ 
  126.             return substr($string,0,$pos+1)." ..."; 
  127.         } 
  128.  
  129.         $pos=strrpos($string," "); 
  130.         if($pos>=$length-4){ 
  131.             $string=substr($string,0,$length-4); 
  132.             $pos=strrpos($string," "); 
  133.         } 
  134.         if($pos>=$length*0.4){ 
  135.             return substr($string,0,$pos)." ..."; 
  136.         } 
  137.  
  138.         return substr($string,0,$length-4)." ..."; 
  139.     } 
  140.  
  141.     function _createGeneratorComment(){ 
  142.         return "<!-- generator=\"".FEEDCREATOR_VERSION."\" -->\n"; 
  143.     } 
  144.  
  145.     function _createAdditionalElements($elements,$indentString=""){ 
  146.         $ae=""; 
  147.         if(is_array($elements)){ 
  148.             foreach($elements AS $key => $value){ 
  149.                 $ae.= $indentString."<$key>$value</$key>\n"; 
  150.             } 
  151.         } 
  152.         return $ae; 
  153.     } 
  154.  
  155.     function _createStylesheetReferences(){ 
  156.         $xml=""; 
  157.         if($this->cssStyleSheet) $xml .= "<?xml-stylesheet href=\"".$this->cssStyleSheet."\" type=\"text/css\"?>\n"; 
  158.         if($this->xslStyleSheet) $xml .= "<?xml-stylesheet href=\"".$this->xslStyleSheet."\" type=\"text/xsl\"?>\n"; 
  159.         return $xml; 
  160.     } 
  161.  
  162.     function createFeed(){} 
  163.  
  164.     function _generateFilename(){ 
  165.         $fileInfo=pathinfo($_SERVER["PHP_SELF"]); 
  166.         return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".xml"; 
  167.     } 
  168.  
  169.     function _redirect($filename){ 
  170.         Header("Content-Type: ".$this->contentType."; charset=".$this->encoding."; filename=".basename($filename)); 
  171.         Header("Content-Disposition: inline; filename=".basename($filename)); 
  172.         readfile($filename,"r"); 
  173.         die(); 
  174.     } 
  175.  
  176.     function useCached($filename="",$timeout=3600){ 
  177.         $this->_timeout=$timeout; 
  178.         if($filename==""){ 
  179.             $filename=$this->_generateFilename(); 
  180.         } 
  181.         if(file_exists($filename) && (time()-filemtime($filename) < $timeout)){ 
  182.             $this->_redirect($filename); 
  183.         } 
  184.     } 
  185.  
  186.     function saveFeed($filename="",$displayContents=true){ 
  187.         if($filename==""){ 
  188.             $filename=$this->_generateFilename(); 
  189.         } 
  190.         $feedFile=fopen($filename,"w+"); 
  191.         if($feedFile){ 
  192.             fputs($feedFile,$this->createFeed()); 
  193.             fclose($feedFile); 
  194.             if($displayContents){ 
  195.                 $this->_redirect($filename); 
  196.             } 
  197.         }else{ 
  198.             echo "<br /><b>Error creating feed file, please check write permissions.</b><br />"; 
  199.         } 
  200.     } 
  201. } 
  202.  
  203. class FeedDate{ 
  204.     var $unix; 
  205.     function FeedDate($dateString=""){ 
  206.         if($dateString=="") $dateString=date("r"); 
  207.         if(is_integer($dateString)){ 
  208.             $this->unix=$dateString; 
  209.             return; 
  210.         } 
  211.         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)){ 
  212.             $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); 
  213.             $this->unix=mktime($matches[4],$matches[5],$matches[6],$months[$matches[2]],$matches[1],$matches[3]); 
  214.             if(substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-'){ 
  215.                 $tzOffset=(substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60; 
  216.             }else{ 
  217.                 if(strlen($matches[7])==1){ 
  218.                     $oneHour=3600; 
  219.                     $ord=ord($matches[7]); 
  220.                     if($ord < ord("M")){ 
  221.                         $tzOffset=(ord("A") - $ord - 1) * $oneHour; 
  222.                     } elseif($ord >= ord("M") && $matches[7]!="Z"){ 
  223.                         $tzOffset=($ord - ord("M")) * $oneHour; 
  224.                     } elseif($matches[7]=="Z"){ 
  225.                         $tzOffset=0; 
  226.                     } 
  227.                 } 
  228.                 switch ($matches[7]){ 
  229.                     case "UT": 
  230.                     case "GMT":    $tzOffset=0; 
  231.                 } 
  232.             } 
  233.             $this->unix += $tzOffset; 
  234.             return; 
  235.         } 
  236.         if(preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~",$dateString,$matches)){ 
  237.             $this->unix=mktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]); 
  238.             if(substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-'){ 
  239.                 $tzOffset=(substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60; 
  240.             }else{ 
  241.                 if($matches[7]=="Z"){ 
  242.                     $tzOffset=0; 
  243.                 } 
  244.             } 
  245.             $this->unix += $tzOffset; 
  246.             return; 
  247.         } 
  248.         $this->unix=0; 
  249.     } 
  250.  
  251.     function rfc822(){ 
  252.         $date=gmdate("Y-m-d H:i:s",$this->unix); 
  253.         if(TIME_ZONE!="") $date .= " ".str_replace(":","",TIME_ZONE); 
  254.         return $date; 
  255.     } 
  256.  
  257.     function iso8601(){ 
  258.         $date=gmdate("Y-m-d H:i:s",$this->unix); 
  259.         $date=substr($date,0,22) . ':' . substr($date,-2); 
  260.         if(TIME_ZONE!="") $date=str_replace("+00:00",TIME_ZONE,$date); 
  261.         return $date; 
  262.     } 
  263.  
  264.     function unix(){ 
  265.         return $this->unix; 
  266.     } 
  267. } 
  268.  
  269. class RSSCreator10 extends FeedCreator{ 
  270.     function createFeed(){ 
  271.         $feed="<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n"; 
  272.         $feed.= $this->_createGeneratorComment(); 
  273.         if($this->cssStyleSheet==""){ 
  274.             $cssStyleSheet="http://www.w3.org/2000/08/w3c-synd/style.css"; 
  275.         } 
  276.         $feed.= $this->_createStylesheetReferences(); 
  277.         $feed.= "<rdf:RDF\n"; 
  278.         $feed.= "    xmlns=\"http://purl.org/rss/1.0/\"\n"; 
  279.         $feed.= "    xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"; 
  280.         $feed.= "    xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"\n"; 
  281.         $feed.= "    xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"; 
  282.         $feed.= "    <channel rdf:about=\"".$this->syndicationURL."\">\n"; 
  283.         $feed.= "        <title>".htmlspecialchars($this->title)."</title>\n"; 
  284.         $feed.= "        <description>".htmlspecialchars($this->description)."</description>\n"; 
  285.         $feed.= "        <link>".$this->link."</link>\n"; 
  286.         if($this->image!=null){ 
  287.             $feed.= "        <image rdf:resource=\"".$this->image->url."\" />\n"; 
  288.         } 
  289.         $now=new FeedDate(); 
  290.         $feed.= "       <dc:date>".htmlspecialchars($now->iso8601())."</dc:date>\n"; 
  291.         $feed.= "        <items>\n"; 
  292.         $feed.= "            <rdf:Seq>\n"; 
  293.         for ($i=0;$i<count($this->items);$i++){ 
  294.             $feed.= "                <rdf:li rdf:resource=\"".htmlspecialchars($this->items[$i]->link)."\"/>\n"; 
  295.         } 
  296.         $feed.= "            </rdf:Seq>\n"; 
  297.         $feed.= "        </items>\n"; 
  298.         $feed.= "    </channel>\n"; 
  299.         if($this->image!=null){ 
  300.             $feed.= "    <image rdf:about=\"".$this->image->url."\">\n"; 
  301.             $feed.= "        <title>".$this->image->title."</title>\n"; 
  302.             $feed.= "        <link>".$this->image->link."</link>\n"; 
  303.             $feed.= "        <url>".$this->image->url."</url>\n"; 
  304.             $feed.= "    </image>\n"; 
  305.         } 
  306.         $feed.= $this->_createAdditionalElements($this->additionalElements,"    "); 
  307.  
  308.         for ($i=0;$i<count($this->items);$i++){ 
  309.             $feed.= "    <item rdf:about=\"".htmlspecialchars($this->items[$i]->link)."\">\n"; 
  310.             //$feed.= "        <dc:type>Posting</dc:type>\n"; 
  311.             $feed.= "        <dc:format>text/html</dc:format>\n"; 
  312.             if($this->items[$i]->date!=null){ 
  313.                 $itemDate=new FeedDate($this->items[$i]->date); 
  314.                 $feed.= "        <dc:date>".htmlspecialchars($itemDate->iso8601())."</dc:date>\n"; 
  315.             } 
  316.             if($this->items[$i]->source!=""){ 
  317.                 $feed.= "        <dc:source>".htmlspecialchars($this->items[$i]->source)."</dc:source>\n"; 
  318.             } 
  319.             if($this->items[$i]->author!=""){ 
  320.                 $feed.= "        <dc:creator>".htmlspecialchars($this->items[$i]->author)."</dc:creator>\n"; 
  321.             } 
  322.             $feed.= "        <title>".htmlspecialchars(strip_tags(strtr($this->items[$i]->title,"\n\r","  ")))."</title>\n"; 
  323.             $feed.= "        <link>".htmlspecialchars($this->items[$i]->link)."</link>\n"; 
  324.             $feed.= "        <description>".htmlspecialchars($this->items[$i]->description)."</description>\n"; 
  325.             $feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements,"        "); 
  326.             $feed.= "    </item>\n"; 
  327.         } 
  328.         $feed.= "</rdf:RDF>\n"; 
  329.         return $feed; 
  330.     } 
  331. } 
  332.  
  333. class RSSCreator091 extends FeedCreator{ 
  334.     var $RSSVersion; 
  335.  
  336.     function RSSCreator091(){ 
  337.         $this->_setRSSVersion("0.91"); 
  338.         $this->contentType="application/rss+xml"; 
  339.     } 
  340.  
  341.     function _setRSSVersion($version){ 
  342.         $this->RSSVersion=$version; 
  343.     } 
  344.  
  345.     function createFeed(){ 
  346.         $feed="<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n"; 
  347.         $feed.= $this->_createGeneratorComment(); 
  348.         $feed.= $this->_createStylesheetReferences(); 
  349.         $feed.= "<rss version=\"".$this->RSSVersion."\">\n"; 
  350.         $feed.= "    <channel>\n"; 
  351.         $feed.= "        <title>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</title>\n"; 
  352.         $this->descriptionTruncSize=500; 
  353.         $feed.= "        <description>".$this->getDescription()."</description>\n"; 
  354.         $feed.= "        <link>".$this->link."</link>\n"; 
  355.         $now=new FeedDate(); 
  356.         $feed.= "        <lastBuildDate>".htmlspecialchars($now->rfc822())."</lastBuildDate>\n"; 
  357.         $feed.= "        <generator>".FEEDCREATOR_VERSION."</generator>\n"; 
  358.  
  359.         if($this->image!=null){ 
  360.             $feed.= "        <image>\n"; 
  361.             $feed.= "            <url>".$this->image->url."</url>\n"; 
  362.             $feed.= "            <title>".FeedCreator::iTrunc(htmlspecialchars($this->image->title),100)."</title>\n"; 
  363.             $feed.= "            <link>".$this->image->link."</link>\n"; 
  364.             if($this->image->width!=""){ 
  365.                 $feed.= "            <width>".$this->image->width."</width>\n"; 
  366.             } 
  367.             if($this->image->height!=""){ 
  368.                 $feed.= "            <height>".$this->image->height."</height>\n"; 
  369.             } 
  370.             if($this->image->description!=""){ 
  371.                 $feed.= "            <description>".$this->image->getDescription()."</description>\n"; 
  372.             } 
  373.             $feed.= "        </image>\n"; 
  374.         } 
  375.         if($this->language!=""){ 
  376.             $feed.= "        <language>".$this->language."</language>\n"; 
  377.         } 
  378.         if($this->copyright!=""){ 
  379.             $feed.= "        <copyright>".FeedCreator::iTrunc(htmlspecialchars($this->copyright),100)."</copyright>\n"; 
  380.         } 
  381.         if($this->editor!=""){ 
  382.             $feed.= "        <managingEditor>".FeedCreator::iTrunc(htmlspecialchars($this->editor),100)."</managingEditor>\n"; 
  383.         } 
  384.         if($this->webmaster!=""){ 
  385.             $feed.= "        <webMaster>".FeedCreator::iTrunc(htmlspecialchars($this->webmaster),100)."</webMaster>\n"; 
  386.         } 
  387.         if($this->pubDate!=""){ 
  388.             $pubDate=new FeedDate($this->pubDate); 
  389.             $feed.= "        <pubDate>".htmlspecialchars($pubDate->rfc822())."</pubDate>\n"; 
  390.         } 
  391.         if($this->category!=""){ 
  392.             $feed.= "        <category>".htmlspecialchars($this->category)."</category>\n"; 
  393.         } 
  394.         if($this->docs!=""){ 
  395.             $feed.= "        <docs>".FeedCreator::iTrunc(htmlspecialchars($this->docs),500)."</docs>\n"; 
  396.         } 
  397.         if($this->ttl!=""){ 
  398.             $feed.= "        <ttl>".htmlspecialchars($this->ttl)."</ttl>\n"; 
  399.         } 
  400.         if($this->rating!=""){ 
  401.             $feed.= "        <rating>".FeedCreator::iTrunc(htmlspecialchars($this->rating),500)."</rating>\n"; 
  402.         } 
  403.         if($this->skipHours!=""){ 
  404.             $feed.= "        <skipHours>".htmlspecialchars($this->skipHours)."</skipHours>\n"; 
  405.         } 
  406.         if($this->skipDays!=""){ 
  407.             $feed.= "        <skipDays>".htmlspecialchars($this->skipDays)."</skipDays>\n"; 
  408.         } 
  409.         $feed.= $this->_createAdditionalElements($this->additionalElements,"    "); 
  410.  
  411.         for ($i=0;$i<count($this->items);$i++){ 
  412.             $feed.= "        <item>\n"; 
  413.             $feed.= "            <title>".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)."</title>\n"; 
  414.             $feed.= "            <link>".htmlspecialchars($this->items[$i]->link)."</link>\n"; 
  415.             $feed.= "            <description>".$this->items[$i]->getDescription()."</description>\n"; 
  416.  
  417.             if($this->items[$i]->author!=""){ 
  418.                 $feed.= "            <author>".htmlspecialchars($this->items[$i]->author)."</author>\n"; 
  419.             } 
  420.             /* 
  421.              // on hold 
  422.              if($this->items[$i]->source!=""){ 
  423.              $feed.= "            <source>".htmlspecialchars($this->items[$i]->source)."</source>\n"; 
  424.              } 
  425.              */ 
  426.             if($this->items[$i]->category!=""){ 
  427.                 $feed.= "            <category>".htmlspecialchars($this->items[$i]->category)."</category>\n"; 
  428.             } 
  429.             if($this->items[$i]->comments!=""){ 
  430.                 $feed.= "            <comments>".htmlspecialchars($this->items[$i]->comments)."</comments>\n"; 
  431.             } 
  432.             if($this->items[$i]->date!=""){ 
  433.                 $itemDate=new FeedDate($this->items[$i]->date); 
  434.                 $feed.= "            <pubDate>".htmlspecialchars($itemDate->rfc822())."</pubDate>\n"; 
  435.             } 
  436.             if($this->items[$i]->guid!=""){ 
  437.                 $feed.= "            <guid>".htmlspecialchars($this->items[$i]->guid)."</guid>\n"; 
  438.             } 
  439.             $feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements,"        "); 
  440.             $feed.= "        </item>\n"; 
  441.         } 
  442.         $feed.= "    </channel>\n"; 
  443.         $feed.= "</rss>\n"; 
  444.         return $feed; 
  445.     } 
  446. } 
  447.  
  448. class RSSCreator20 extends RSSCreator091{ 
  449.  
  450.     function RSSCreator20(){ 
  451.         parent::_setRSSVersion("2.0"); 
  452.     } 
  453. } 

使用示例:

  1. <?php 
  2. header('Content-Type:text/html; charset=utf-8'); 
  3. $db=mysql_connect('127.0.0.1','root','123456'); 
  4. mysql_query("set names utf8"); 
  5. mysql_select_db('dbname',$db); 
  6. $brs=mysql_query('select * from article order by add_time desc limit 0,10',$db); 
  7. $rss=new UniversalFeedCreator(); 
  8. $rss->title="页面标题"; 
  9. $rss->link="网址http://"; 
  10. $rss->description="rss标题"; 
  11. while($rowbrs=mysql_fetch_array($brs)){ 
  12.     $item=new FeedItem(); 
  13.     $item->title =$rowbrs['subject']; 
  14.     $item->link='https://www.phpfensi.com/'; 
  15.     $item->description =$rowbrs['description']; 
  16.     $rss->addItem($item); 
  17. } 
  18. mysql_close($db); 
  19. $rss->saveFeed("RSS2.0","rss.xml");

Tags: php生成RSS订阅

分享到: