一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

PHP RSS 生成类

时间:2010-06-23 编辑:简简单单 来源:一聚教程网

if (defined('_class_rss_php教程')) return;
define('_class_rss_php教程',1);
/**
 
 *  使用说明:
 *  $rss = new rss('redfox','http://111com.net/',"redfox's blog");
 *  $rss->additem('rss class',"http://www.111com.net","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 = " ";
       // start channel
       $s .= " ";
       $s .= " "
       $s .= "{$this->channel_link} ";
       $s .= "{$this->channel_description} ";
       $s .= "{$this->language} ";
       if (!empty($this->copyright)) {
          $s .= "{$this->copyright} ";
       }
       if (!empty($this->webmaster)) {
          $s .= "{$this->webmaster} ";
       }
       if (!empty($this->pubdate)) {
          $s .= "{$this->pubdate} ";
       }


       if (!empty($this->lastbuilddate)) {
          $s .= "{$this->lastbuilddate} ";
       }


       if (!empty($this->generator)) {
          $s .= "{$this->generator} ";
       }
     
       // start items
       for ($i=0;$iitems),$i++) {
           $s .= " ";
           $s .= " ";
           $s .= "{$this->items[$i]['link']} ";
           $s .= "items[$i]['description']}]]> ";
           $s .= "{$this->items[$i]['pubdate']} ";         
           $s .= " ";
       }
    
      // close channel
      $s .= " ";
      $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);
   }
}


?>
同时也可以用网页特效来解析xml文档

热门栏目