找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1812|回复: 0
打印 上一主题 下一主题

PHP生成RSS文件类实例

[复制链接]

2617

主题

2617

帖子

7789

积分

论坛元老

Rank: 8Rank: 8

积分
7789
跳转到指定楼层
楼主
发表于 2018-2-14 05:51:40 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

            本文实例讲述了PHP生成RSS文件类文件。分享给大家供大家参考。具体如下:
PHP RSS 生成类实例代码如下:
[U]复制代码[/U] 代码如下:additem('rss class',"http://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 = " ";
       // start channel
       $s .= " ";
       $s .= " "
       $s .= "{$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 .= "{$this->items[$i]['link']} ";
           $s .= "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在线生成ico文件的代码
  • php可生成缩略图的文件上传类实例
  • php静态文件生成类实例分析
  • php生成zip文件类实例
  • php生成PDF格式文件并且加密
  • php提交过来的数据生成为txt文件
  • PHP生成及获取JSON文件的方法
  • php生成与读取excel文件
  • 利用PHP生成CSV文件简单示例
  • php简单读取.vcf格式文件的方法示例
  • PHP实现生成vcf vcard文件功能类定义与使用方法详解【附demo源码下载】
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端