登录  /  注册

php完美的rss 生成类_PHP教程

php中文网
发布: 2016-07-20 11:10:51
原创
688人浏览过

RSS订阅功能,在很多网站都可以有但也有很多,下面代码是自己写的,其中使用到了一个PHP类:RSS.class.php,感觉非常方便,不敢独享,特拿出来跟大家分享。

 代码如下 复制代码

include_once("class/rss.class.php");//引入rss php类
$rss= new rss("名称","地址","描述","rss频道图标");
$rss->additem("日志的标题","日志的地址","日志的摘要","日志的发布日期");
$rss->display();//输出rss内容


全部代码如下:

 代码如下 复制代码
// +----------------------------------------------------------------------
// | YBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.hzhuti.com/nokia/n97/ All rights reserved.
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | Author: yhustc
// +----------------------------------------------------------------------
// $Id$
 
/**
 +------------------------------------------------------------------------------
 * RSS生成类
 +------------------------------------------------------------------------------
 * @author    yhustc
 * @version   $Id$
 +------------------------------------------------------------------------------
 */
class RSS
{
    /**
     +----------------------------------------------------------
     * RSS频道名
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $channel_title = "';
    /**
     +----------------------------------------------------------
     * RSS频道链接
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $channel_link = "';
    /**
     +----------------------------------------------------------
     * RSS频道描述
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $channel_description = '';
    /**
     +----------------------------------------------------------
     * RSS频道使用的小图标的URL
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $channel_imgurl = '';
    /**
     +----------------------------------------------------------
     * RSS频道所使用的语言
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $language = 'zh_CN';
    /**
     +----------------------------------------------------------
     * RSS文档创建日期,默认为今天
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $pubDate = '';
    protected $lastBuildDate = '';
 
    protected $generator = 'YBlog RSS Generator';
 
    /**
     +----------------------------------------------------------
     * RSS单条信息的数组
     +----------------------------------------------------------
     * @var string
     * @access protected
     +----------------------------------------------------------
     */
    protected $items = array();
 
    /**
     +----------------------------------------------------------
     * 构造函数
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $title  RSS频道名
     * @param string $link  RSS频道链接
     * @param string $description  RSS频道描述
     * @param string $imgurl  RSS频道图标
     +----------------------------------------------------------
     */
    public function __construct($title, $link, $description, $imgurl = '')
    {
        $this->channel_title = $title;
        $this->channel_link = $link;
        $this->channel_description = $description;
        $this->channel_imgurl = $imgurl;
        $this->pubDate = Date('Y-m-d H:i:s', time());
        $this->lastBuildDate = Date('Y-m-d H:i:s', time());
    }
 
    /**
     +----------------------------------------------------------
     * 设置私有变量
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $key  变量名
     * @param string $value  变量的值
     +----------------------------------------------------------
     */
     public function Config($key,$value)
     {
        $this->{$key} = $value;
     }
 
    /**
     +----------------------------------------------------------
     * 添加RSS项
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $title  日志的标题
     * @param string $link  日志的链接
     * @param string $description  日志的摘要
     * @param string $pubDate  日志的发布日期
     +----------------------------------------------------------
     */
     function AddItem($title, $link, $description, $pubDate)
     {
        $this->items[] = array('title' => $title, 'link' => $link, 'description' => $description, 'pubDate' => $pubDate);
     }
 
     /**
     +----------------------------------------------------------
     * 输出RSS的XML为字符串
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
    public function Fetch()
    {
        $rss = "rn";
        $rss = "rn";
        $rss .= "rn";
        $rss .= "channel_title}]]>rn";
        $rss .= "channel_description}]]>rn";
        $rss .= "{$this->channel_link}rn";
        $rss .= "{$this->language}rn";
 
        if (!empty($this->pubDate))
            $rss .= "{$this->pubDate}rn";
        if (!empty($this->lastBuildDate))
            $rss .= "{$this->lastBuildDate}rn";
        if (!empty($this->generator))
            $rss .= "{$this->generator}rn";
 
        $rss .= "5rn";
 
        if (!empty($this->channel_imgurl)) {
            $rss .= "rn";
            $rss .= "channel_title}]]>rn";
            $rss .= "{$this->channel_link}rn";
            $rss .= "{$this->channel_imgurl}rn";
            $rss .= "rn";
        }
 
        for ($i = 0; $i items); $i++) {
            $rss .= "rn";
            $rss .= "items[$i]['title']}]]>rn";
            $rss .= "{$this->items[$i]['link']}rn";
            $rss .= "items[$i]['description']}]]>rn";
            $rss .= "{$this->items[$i]['pubDate']}rn";
            $rss .= "
rn";
        }
 
        $rss .= "
rn
";
        return $rss;
    }
 
    /**
     +----------------------------------------------------------
     * 输出RSS的XML到浏览器
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
    public function Display()
    {
        header("Content-Type: text/xml; charset=utf-8");
        echo $this->Fetch();
        exit;
    }
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444698.htmlTechArticleRSS订阅功能,在很多网站都可以有但也有很多,下面代码是自己写的,其中使用到了一个PHP类:RSS.class.php,感觉非常方便,不敢独享,特拿...
智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号