(总结)三种PHP生成rss聚合器的方法

RSS(简易信息聚合,也叫聚合内容)是一种描述和同步网站内容的格式。RSS可以是以下三个解释的其中一个: Really Simple Syndication;RDF (Resource Description Framework) Site Summary; Rich Site Summary。但其实这三个解释都是指同一种Syndication的技术。RSS目前广泛用于网上新闻频道,blog和wiki,主要的版本有0.91, 1.0, 2.0。使用RSS订阅能更快地获取信息,网站提供RSS输出,有利于让用户获取网站内容的最新更新。网络用户可以在客户端借助于支持RSS的聚合工具软件,在不打开网站内容页面的情况下阅读支持RSS输出的网站内容。

下面介绍PHP生成rss的三种方法:

1.PHP生成rss简单直接的办法

一个RSS文件就是一段规范的XML数据。

代码如下:

<?php
$dbconnect =mysql_connect(“localhost”,”root”,”123456″);
$database =  “test”;
mysql_select_db($database, $dbconnect);

$query = “select * fromrsslimit 15″;
$result = mysql_query($query, $dbconnect);
while ($line = mysql_fetch_assoc($result))
{
    $return[] = $line;
}

$now = date(“Y-m-d”);

$output = “<?xml version=\”1.0\”?>
            <rss version=\”2.0\”>
                <channel>
                    <title>Demo RSS</title>
                    <link>http://localhost/test/rss/rss.xml</link>
                    <description>Test RSS</description>
                    <language>en-us</language>
                    <pubDate>$now</pubDate>
                    <lastBuildDate>$now</lastBuildDate>
                    <docs>http://localhost</docs>
                    <managingEditor>you@youremail.com</managingEditor>
                    <webMaster>you@youremail.com</webMaster>
            “;
           
foreach ($return as $line)
{
    $output .= “<item><title>”.htmlentities($line['title']).”</title>
                    <link>”.htmlentities($line['url']).”</link>
                    <description>”.htmlentities(strip_tags($line['description'])).”</description>
        <pubDate>”.$line['date'].”</pubDate>
                </item>”;
}
$output .= “</channel></rss>”;

file_put_contents(‘rss.xml’,$output);
echo $output;
?>

2.php动态生成rss  

第一个文件:

<?php
include_once(“inc/conn.php”);
include_once(“inc/function.php”);
$fp = fopen (“temp1.xml”,”r”);
$content = fread ($fp,filesize (“temp1.xml”)); #读入打开文件的内容;
$filename = “rss.xml”; #拟将rss.xml文件最后生成在test目录下,test目录要具有写权限;
$handle = fopen ($filename,”w”); #fopen,即打开文件,若文件不存在,则自动创建;
if (!is_writable ($filename)){die (“文件:”.$filename.”不可写,请检查其属性后重试!”);}
if (!fwrite ($handle,$content)){die (“生成文件”.$filename.”失败!”);}

$i=0;

$result= $db->query(“select * from  order by id desc”);
$k = mysql_num_rows($result);

while( $i<$k )
{
$result= $db->query(“select * from order by id desc limit $i,1″);
$a = mysql_fetch_array($result);
$b=$a['id'];
$i++;
$title = $a['title'];
$link = “http://www.jz580.net/php?id=’$b’“;
$time=$a['time'];
$maintext=$a['content'];
$str=trim(strip_tags(substr($maintext,0,30)),”&nbsp;”);
$fp = fopen (“temp2.xml”,”r”);
$content = fread ($fp,filesize (“temp2.xml”));
$content = str_replace (“{title}”,$title,$content); #替换模板变量中的数据;
$content = str_replace (“{link}”,$link,$content);
$content = str_replace (“{maintext}”,$str,$content);
$content = str_replace (“{time}”,$time,$content);
$filename = “rss.xml”;
$handle = fopen ($filename,”a”); #注意这里是a,不是w,因为要追加不是覆盖数据。
if (!is_writable ($filename)){die (“文件:”.$filename.”不可写,请检查其属性后重试!”);}
if (!fwrite ($handle,$content)){die (“生成文件”.$filename.”失败!”);}
}
$fp = fopen (“temp3.xml”,”r”);
$content = fread ($fp,filesize (“temp3.xml”));
$filename = “rss.xml”;
$handle = fopen ($filename,”a”);
if (!is_writable ($filename)){die (“文件:”.$filename.”不可写,请检查其属性后重试!”);}
if (!fwrite($handle,$content)){die (“生成文件”.$filename.”失败!”);}
fclose ($handle);
die (“成功!”);
?>

第二个文件:

<?xml version=”1.0″ encoding=”gb2312″ ?>
<rss version=”2.0″>
<channel>
<title>我是程序员 | 旨在为了给大家一个学习PHP技术以及PHP学习交流的平台</title>
<link>http://www.tianjianlin.cn/</link>
<description>我是程序员,旨在为了和也大家提供一个学习PHP和学习PHP技术交流的平台。另本人也想学学SEO,  所以希望学SEO搜索引擎优化的人员来多多赐教。方便大家学习PHP,也方便大家学习SEO。</description>
<language>zh-cn</language>
<generator>我是程序员 版权所有</generator>
<webmaster>1394939582@qq.com</webmaster>

 <item>
    <title> {title} </title>
    <link> {link} </link>
    <description> {maintext} </description>
    <pubDate> {time} </pubDate>
</item>

 </channel>
</rss>

3.PHP生成rss聚合器类

<?php
class RSS
{
    /**
     +----------------------------------------------------------
     * RSS频道名
     +----------------------------------------------------------
     */
    protected $channel_title = '';
    /**
     +----------------------------------------------------------
     * RSS频道链接
     +----------------------------------------------------------
     */
    protected $channel_link = '';
    /**
     +----------------------------------------------------------
     * RSS频道描述
     +----------------------------------------------------------
     */
    protected $channel_description = '';
    /**
     +----------------------------------------------------------
     * RSS频道使用的小图标的URL
     +----------------------------------------------------------
     */
    protected $channel_imgurl = '';
    /**
     +----------------------------------------------------------
     * RSS频道所使用的语言
     +----------------------------------------------------------
     */
    protected $language = 'zh_CN';
    /**
     +----------------------------------------------------------
     * RSS文档创建日期,默认为今天
     +----------------------------------------------------------
     */
    protected $pubDate = '';
    protected $lastBuildDate = '';

    protected $generator = 'YBlog RSS Generator';

    /**
     +----------------------------------------------------------
     * RSS单条信息的数组
     +----------------------------------------------------------
     */
    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 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n";
        $rss = "<rss version=\"2.0\">\r\n";
        $rss .= "<channel>\r\n";
        $rss .= "<title><![CDATA[{$this->channel_title}]]></title>\r\n";
        $rss .= "<description><![CDATA[{$this->channel_description}]]></description>\r\n";
        $rss .= "<link>{$this->channel_link}</link>\r\n";
        $rss .= "<language>{$this->language}</language>\r\n";

        if (!empty($this->pubDate))
            $rss .= "<pubDate>{$this->pubDate}</pubDate>\r\n";
        if (!empty($this->lastBuildDate))
            $rss .= "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>\r\n";
        if (!empty($this->generator))
            $rss .= "<generator>{$this->generator}</generator>\r\n";

        $rss .= "<ttl>5</ttl>\r\n";

        if (!empty($this->channel_imgurl)) {
            $rss .= "<image>\r\n";
            $rss .= "<title><![CDATA[{$this->channel_title}]]></title>\r\n";
            $rss .= "<link>{$this->channel_link}</link>\r\n";
            $rss .= "<url>{$this->channel_imgurl}</url>\r\n";
            $rss .= "</image>\r\n";
        }

        for ($i = 0; $i < count($this->items); $i++) {
            $rss .= "<item>\r\n";
            $rss .= "<title><![CDATA[{$this->items[$i]['title']}]]></title>\r\n";
            $rss .= "<link>{$this->items[$i]['link']}</link>\r\n";
            $rss .= "<description><![CDATA[{$this->items[$i]['description']}]]></description>\r\n";
            $rss .= "<pubDate>{$this->items[$i]['pubDate']}</pubDate>\r\n";
            $rss .= "</item>\r\n";
        }

        $rss .= "</channel>\r\n</rss>";
        return $rss;
    }

    /**
     +----------------------------------------------------------
     * 输出RSS的XML到浏览器
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
    public function Display()
    {
        header("Content-Type: text/xml; charset=utf-8");
        echo $this->Fetch();
        exit;
    }
}
?>
以上就是总结的三种PHP生成rss聚合器的方法。

Copyright © All Rights Reserved · Proudly powered by WordPress

css.php