PHP生成RSS文件类实例

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP生成RSS文件类文件。分享给大家供大家参考。具体如下:

PHP RSS 生成类实例代码如下:

复制代码 代码如下:
<?php
if (defined('_class_rss_php')) return;
define('_class_rss_php教程',1);
/**
 
 *  使用说明:
 *  $rss = new rss('redfox','http://jb51.net/',"redfox's blog");
 *  $rss->additem('rss class',"//www.jb51.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 = "<!--l version="1.0" encoding="gb2312"--> ";
       // start channel
       $s .= " ";
       $s .= " "
       $s .= "<link />{$this->channel_link} ";
       $s .= "{$this->channel_description} ";
       $s .= "{$this->language} ";
       if (!emptyempty($this->copyright)) {
          $s .= "{$this->copyright} ";
       }
       if (!emptyempty($this->webmaster)) {
          $s .= "{$this->webmaster} ";
       }
       if (!emptyempty($this->pubdate)) {
          $s .= "{$this->pubdate} ";
       }
 
       if (!emptyempty($this->lastbuilddate)) {
          $s .= "{$this->lastbuilddate} ";
       }
 
       if (!emptyempty($this->generator)) {
          $s .= "{$this->generator} ";
       }
      
       // start items
       for ($i=0;$iitems),$i++) {
           $s .= " ";
           $s .= " ";
           $s .= "<link />{$this->items[$i]['link']} ";
           $s .= "<!--data[{$thi-->items[$i]['description']}]]> ";
           $s .= "{$this->items[$i]['pubdate']} ";          
           $s .= " ";
       }
     
      // close channel
      $s .= " ";
      $this->content = $s;
   }
 
   function show() {
       if (emptyempty($this->content)) $this->buildrss();
       header('content-type:text/xml');
       echo($this->content);
   }
 
   function savetofile($fname) {
       if (emptyempty($this->content)) $this->buildrss();
       $handle = fopen($fname, 'wb');
       if ($handle === false)  return false;
       fwrite($handle, $this->content);
       fclose($handle);
   }
}
?>

希望本文所述对大家的PHP程序设计有所帮助。

相关文章

php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)

微信入口绑定,微信事件处理,微信API全部操作包含在这些文件中。 内容有:微信摇一摇接口/微信多客服接口/微信支付接口/微信红包接口/微信卡券接口/微信小店接口/JSAPI <...

php集成开发环境详解

HP开发使用的集成环境,可用PHPStorm, 或者用免费版的 IDEA 加 PHP 插件,两者功能基本相同,只是后者安装起来略折腾。 PHPStorm的特点:跨平台,我在 Window...

解析二进制流接口应用实例 pack、unpack、ord 函数使用方法

在工作中,我也逐渐了解到pack,unpack,ord对于二进制字节处理的强大。 下面我逐一介绍它们。在我们工作中,用到它们的估计不多。 我在最近一个工作中,因为通讯需要用到二进制流,然...

Yii2框架整合Xunsearch搜索引擎的方法

本文实例讲述了Yii2框架整合Xunsearch搜索引擎的方法。分享给大家供大家参考,具体如下: 公司一直用的YII2框架,然后要做一个中文搜索引擎,所有想的Xunsearch这个项目,...

php面向对象程序设计入门教程

本文实例讲述了php面向对象程序设计。分享给大家供大家参考,具体如下: 1.面向对象与面向过程的对比 面向过程:以事件为中心,分几个步骤去完成。不可扩展,html与php不分离 面向对象...