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

PHP生成RSS订阅的程序代码

发布:smiling 来源: PHP粉丝网  添加日期:2016-02-16 16:24:46 浏览: 评论:0 

生成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.phpfensi.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. private function createItem() { 
  20. //RSS item 
  21. //$data可替换为自己的数据 
  22. $html = ''
  23. //文章数据 
  24. $data = array
  25. 'id' => 1, 
  26. 'date' => date('r', time()), 
  27. 'title' => '文章标题'
  28. 'link' => 'http://www.phpfensi.com',    //文章地址 
  29. 'description' => '网站描述' 
  30. ); 
  31. for($i = 0; $i < 6; $i++) { 
  32. $html .= ' 
  33. <item> 
  34. <title>'.$data['title'].'</title> 
  35. <link>'.$data['link'].'</link> 
  36. <pubDate>'.$data['date'].'</pubDate> 
  37. <description><![CDATA['.$data['description'].']]></description> 
  38. </item> 
  39. '; 
  40. return $html
  41. header("Content-Type: text/xml; charset=utf-8"); 
  42. $rss = new Rss(); 
  43. $rss->createFeed(); 
  44. exit
  45. ?> 

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

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

使用示例:

  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.phpfensi.com/'
  15.  $item->description =$rowbrs['description']; 
  16.  $rss->addItem($item); 
  17. mysql_close($db); 
  18. $rss->saveFeed("RSS2.0","rss.xml"); 
  19. ?> 

Tags: PHP生成RSS PHP订阅程序

分享到: