PHP生成RSS文件类实例

前端之家收集整理的这篇文章主要介绍了PHP生成RSS文件类实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

代码如下:
  • 使用说明:
  • $rss = new rss('redfox','http://jb51.cc/',"redfox's blog");
  • $rss->additem('RSS class',"//www.jb51.cc","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,$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程序设计有所帮助。

    原文链接:https://www.f2er.com/php/23155.html

    猜你在找的PHP相关文章