Chinaunix

标题: rss生成类 [打印本页]

作者: wleige    时间: 2006-01-23 17:37
标题: rss生成类

if (defined('_CLASS_RSS_PHP')) return;
define('_CLASS_RSS_PHP',1);
/**
*  Class name: RSS
*  Author    : RedFox
*  website   : http://www.foxbat.cn/
*  blog      : http://redsoft.yculblog.com/
*  CopyRight : RedFox (
singlecat@163.com
)
*  说明 : 你可使用它到任意地方,但请保留此信息
*  使用说明:
*  $rss = new RSS('RedFox','http://redsoft.yculblog.com/',"RedFox's Blog");
*  $rss->AddItem('RSS Class',"
http://www.xxx.com","xxx",date
());
*  $rss->AddItem(...);
*  $rss->SaveToFile(...);
*/
class RSS {
   //public
   $rss_ver = "2.0";
   $channel_title = '';
   $channel_link = '';
   $channel_description = '';
   $language = 'zh_CN';
   $copyright = '';
   $webMaster = '';
   $pubDate = '';
   $lastBuildDate = '';
   $generator = 'RedFox RSS Generator';
   $content = '';
   $items = array();
   function RSS($title, $link, $description) {
       $this->channel_title = $title;
       $this->channel_link = $link;
       $this->channel_description = $description;
       $this->pubDate = Date('Y-m-d H:i:s',time());
       $this->lastBuildDate = Date('Y-m-d H:i:s',time());
   }
   function AddItem($title, $link, $description ,$pubDate) {
       $this->items[] = array('titile' => $title ,
                        'link' => $link,
                        'description' => $description,
                        'pubDate' => $pubDate);
   }
   function BuildRSS() {
       $s = "\n \n";
       // start channel
       $s .= "\n";
       $s .= "{$this->channel_title}\n"
       $s .= "{$this->channel_link}\n";
       $s .= "{$this->channel_description}\n";
       $s .= "{$this->language}\n";
       if (!empty($this->copyright)) {
          $s .= "{$this->copyright}\n";
       }
       if (!empty($this->webMaster)) {
          $s .= "{$this->webMaster}\n";
       }
       if (!empty($this->pubDate)) {
          $s .= "{$this->pubDate}\n";
       }
       if (!empty($this->lastBuildDate)) {
          $s .= "{$this->lastBuildDate}\n";
       }
       if (!empty($this->generator)) {
          $s .= "{$this->generator}\n";
       }
      
       // start items
       for ($i=0;$iitems),$i++) {
           $s .= "\n";
           $s .= "{$this->items[$i]['title']}\n";
           $s .= "{$this->items[$i]['link']}\n";
           $s .= "items[$i]['description']}]]>\n";
           $s .= "{$this->items[$i]['pubDate']}\n";           
           $s .= "\n";
       }
      
      // close channel
      $s .= "\n";
      $this->content = $s;
   }
   function Show() {
       if (empty($this->content)) $this->BuildRSS();
       header('Content-type:text/xml');
       echo($this->content);
   }
   function SaveToFile($fname) {
       if (empty($this->content)) $this->BuildRSS();
       $handle = fopen($fname, 'wb');
       if ($handle === false)  return false;
       fwrite($handle, $this->content);
       fclose($handle);
   }
}
?>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/13004/showart_70102.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2