RSS订阅制作
今天给网站搞了个RSS订阅,希望可以有更多的人接受到杂志的最新信息.
效果地址:http://zazhi.goufang.com/feed.rss
现在把RSS类粘一下,是从网上搜罗到的,绝对好用.
RSS.PHP代码:
<?php
/**
* Class name: RSS
* Author : LeadStar Team
* website : http://www.leadstar.com.cn/
* CopyRight : LeadStar Team
*/
if (defined(‘_CLASS_RSS_PHP’)) return;
define(‘_CLASS_RSS_PHP’,1);
class RSS {
//public
var $rss_ver = “2.0”;
var $channel_title = ”;
var $channel_link = ”;
var $channel_description = ”;
var $language = ‘zh_CN’;
var $copyright = ”;
var $webMaster = ”;
var $pubDate = ”;
var $lastBuildDate = ”;
var $generator = ‘RedFox RSS Generator’;
var $content = ”;
var $items = array();
/**************************************************************************/
// 函数名: RSS
// 功能: 构造函数
// 参数: $title
// $link
// $description
/**************************************************************************/
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());
}
/**************************************************************************/
// 函数名: AddItem
// 功能: 添加一个节点
// 参数: $title
// $link
// $description $pubDate
/**************************************************************************/
function AddItem($title, $link, $description ,$pubDate) {
$this->items[] = array(‘title’ => $title ,
‘link’ => $link,
‘description’ => $description,
‘pubDate’ => $pubDate);
}
/**************************************************************************/
// 函数名: BuildRSS
// 功能: 生成rss xml文件内容
/**************************************************************************/
function BuildRSS() {
$s = “<?xml version=’1.0′ encoding=’utf-8′?>
n<rss version=”2.0″>n”;
// start channel
$s .= “<channel>n”;
$s .= “<title><![CDATA[{$this->channel_title}]]></title>n”;
$s .= “<link><![CDATA[{$this->channel_link}]]></link>n”;
$s .= “<description><![CDATA[{$this->channel_description}]]></description>n”;
$s .= “<language>{$this->language}</language>n”;
if (!empty($this->copyright)) {
$s .= “<copyright><![CDATA[{$this->copyright}]]></copyright>n”;
}
if (!empty($this->webMaster)) {
$s .= “<webMaster><![CDATA[{$this->webMaster}]]></webMaster>n”;
}
if (!empty($this->pubDate)) {
$s .= “<pubDate>{$this->pubDate}</pubDate>n”;
}
if (!empty($this->lastBuildDate)) {
$s .= “<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>n”;
}
if (!empty($this->generator)) {
$s .= “<generator>{$this->generator}</generator>n”;
}
// start items
for ($i=0;$i<count($this->items);$i++) {
$s .= “<item>n”;
$s .= “<title><![CDATA[{$this->items[$i][‘title’]}]]></title>n”;
$s .= “<link><![CDATA[{$this->items[$i][‘link’]}]]></link>n”;
$s .= “<description><![CDATA[{$this->items[$i][‘description’]}]]></description>n”;
$s .= “<pubDate>{$this->items[$i][‘pubDate’]}</pubDate>n”;
$s .= “</item>n”;
}
// close channel
$s .= “</channel>n</rss>”;
$this->content = $s;
}
/**************************************************************************/
// 函数名: Show
// 功能: 将产生的rss内容直接打印输出
/**************************************************************************/
function Show() {
if (empty($this->content)) $this->BuildRSS();
echo($this->content);
}
/**************************************************************************/
// 函数名: SaveToFile
// 功能: 将产生的rss内容保存到文件
// 参数: $fname 要保存的文件名
/**************************************************************************/
function SaveToFile($fname) {
$handle = fopen($fname, ‘wb’);
if ($handle === false) return false;
fwrite($handle, $this->content);
fclose($handle);
}
}
?>
feed.php代码:
<?php
require_once(‘RSS.php’);
require_once(‘conn/connect.inc’);
function changUTF($con){
$con = iconv(“gb2312″,”utf-8”,$con);
return $con;
}
$myRss = new RSS(“购房者杂志网”,”http://zazhi.goufang.com”,”购房者杂志网”);
//$myRss->RSS();
$sql = “select ID,m_name,m_num,m_class,m_intro,m_time from m_property”;
$result = mysql_query($sql, $connection);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$sql1 = “select tag_con from m_tag where m_id=”.$row[‘ID’];
$result1 = mysql_query($sql1,$connection);
while ($row1 = mysql_fetch_array($result1, MYSQL_ASSOC)) {
$tag = $tag.” “.$row[‘tag_con’];
}
$myRss->AddItem(changUTF($row[‘m_name’].$row[‘m_num’]),’http://zazhi.goufang.com/html/’.$row[‘ID’].’.html’,changUTF($row1[‘$tag’].$row[‘m_intro’]),$row[‘m_time’]);
}
$myRss->BuildRSS();
$myRss->SaveToFile(‘feed.rss’);
?>