首页 > 开发 > PHP > 正文

PHP生成RSS订阅的程序代码

2024-05-04 22:00:31
字体:大 中 小
来源:转载
供稿:网友

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

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

rss XML结构.

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <rss version="2.0"> 
  3. <channel> 
  4. <title>网站名称</title> 
  5. <link>http://www.Vevb.com/</link> 
  6. <description>网站描述!</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> 

RSS实例

  1. <?php 
  2. class Rss { 
  3. public function createFeed() { 
  4. //RSS头部 
  5. $webUrl = 'http://'.$_SERVER['HTTP_HOST'];//网站地址 
  6. $webName = '网站名称';    //网站名称 
  7. $webDesc = '网站描述';    //网站描述 
  8. $html = '<?xml version="1.0" encoding="utf-8"?> 
  9. <rss version="2.0"> 
  10. <channel> 
  11. <title>'.$webName.'</title> 
  12. <link>'.$webUrl.'</link> 
  13. <description>'.$webDesc.'</description> 
  14. '.$this->createItem().' 
  15. </channel> 
  16. </rss> 
  17. '; 
  18. echo $html; 
  19. } 
  20. private function createItem() { 
  21. //RSS item 
  22. //$data可替换为自己的数据 
  23. $html = ''; 
  24. //文章数据 
  25. $data = array( 
  26. 'id' => 1, 
  27. 'date' => date('r', time()), 
  28. 'title' => '文章标题', 
  29. 'link' => 'http://www.Vevb.com',    //文章地址 
  30. 'description' => '网站描述' 
  31. ); 
  32. for($i = 0; $i < 6; $i++) { 
  33. $html .= ' 
  34. <item> 
  35. <title>'.$data['title'].'</title> 
  36. <link>'.$data['link'].'</link> 
  37. <pubDate>'.$data['date'].'</pubDate> 
  38. <description><![CDATA['.$data['description'].']]></description> 
  39. </item> 
  40. '; 
  41. } 
  42. return $html; 
  43. } 
  44. } 
  45. header("Content-Type: text/xml; charset=utf-8"); 
  46. $rss = new Rss(); 
  47. $rss->createFeed(); 
  48. exit; 
  49. ?> 

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

  1. <link rel=”alternate” type=”application/rss+xml” title=”网站名称 RSS Feed” href=”http://www.Vevb.com” /> 

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

例子2:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <rss version="2.0"> 
  4.  
  5. <channel> 
  6.  
  7. <title>php程序员教程网</title> 
  8.  
  9. <link>http://www.Vevb.com/</link> 
  10.  
  11. <description>本站是一个php程序员的工作生活笔记!</description> 
  12.  
  13. <item> 
  14.  
  15. <title>RSS Tutorial</title> 
  16.  
  17. <link>网站地址/rss</link> 
  18.  
  19. <description>New RSS tutorial on W3School</description> 
  20.  
  21. </item> 
  22.  
  23. <item> 
  24.  
  25. <title>XML Tutorial</title> 
  26.  
  27. <link>网站地址/xml</link> 
  28.  
  29. <description>New XML tutorial on W3School</description> 
  30.  
  31. </item> 
  32.  
  33. </channel> 
  34.  
  35. </rss> 

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

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

使用示例:

  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_<a href="/tags.php/select/" target="_blank">select</a>_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='http://www.Vevb.com/'; 
  15.  $item->description =$rowbrs['description']; 
  16.  $rss->addItem($item); 
  17. } 
  18. mysql_close($db); 
  19. $rss->saveFeed("RSS2.0","rss.xml"); 
  20. ?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表