rss
<?php
/*****************************************
* RSS2.0 Generator
* Create by MagicBoy(darasion)
* Copyright ? 神奇男孩
******************************************/
/*————————————————
使用方法:
將下面的數組,帶入相應的函數。
1、頻道主體
$channel = array(
title => '頻道名稱(必備)',
link => '頻道的URL(必備)',
description => '頻道的描述(必備)',
language => '頻道文章所用語言(可選)',
webmaster => '負責頻道技術事務的網站管理員email(可選)',
managingEditor => '責任編輯的email(可選)',
pubDate => '頻道內容發布日期,格式遵循RFC822格式(年份可為2位或4位)(可選)',
lastBuildDate => '頻道內容最後的修改日期(Sat, 07 Sep 2002 09:42:31 GMT)(可選)',
skipDays => '提示新聞彙總器,那些天它可以跳過。(可選)',
copyright => '頻道內容的著作權說明(可選)',
ttl => '有效期間,用以指明該頻道可被緩衝的最長時間(可選)',
);
2、頻道圖片
$image = array(
url => '圖片的連結(必備)',
title => '圖片的標題,用於http的alt屬性(必備)',
link => '網站的url(實際中常以頻道的url代替)(必備)',
width => '圖片的寬度(象素為單位)最大144,預設88(可選)',
height => '圖片的高度(象素為單位)最大400,預設31(可選)',
description => '用於link的title屬性(可選)'
);
3、頻道項
$item = array(
title => '項(item)的標題(必備)',
description => '項的大綱(必備)',
link => '項的URL(必備)',
comments => '該項的評論(comments)頁的URL(可選)',
guid => '1項的唯一標誌符串(可選)',
author => '該項作者的email(可選)',
enclosure => '描述該附帶的媒體對象(可選)',
category => '包含該項的一個或幾個分類(catogory)(可選)',
pubdate => '項的發布時間(可選)',
source_url => '該項來自的RSS道(可選)',
source_name => '該項來自的RSS道(可選)'
);
————————————————*/
class rss2
{
var $channel_pre="";
var $str_image="";
var $str_item="";
var $channel_end="";
/*建構函式*/
function rss2($channel,$encoding="GB2312")
{
$this->channel($channel,$encoding);
}
/*產生頻道主體*/
function channel($channel,$encoding="GB2312")
{
$this->channel_pre.="<?xml version=\"1.0\" encoding=\"$encoding\"?>\n";
$this->channel_pre.= "<rss version=\"2.0\">\n";
$this->channel_pre.= " <channel>\n";
$this->channel_pre.= " <title>".$channel['title']."</title>\n";//頻道名稱(必備)
$this->channel_pre.= " <link>".$channel['link']."</link>\n";//頻道的URL(必備)
$this->channel_pre.= " <description>".$channel['description']."</description>\n";//頻道的描述(必備)
$this->channel_pre.= " <generator>MagicBoy RSS Generator v1.0</generator>\n";//建立此文檔的程式(可選)
if(isset($channel['language']))$this->channel_pre.= " <language>".$channel['language']."</language>\n";//頻道文章所用語言(可選)
if(isset($channel['webmaster']))$this->channel_pre.= " <webMaster>".$channel['webmaster']."</webMaster>\n";//負責頻道技術事務的網站管理員email(可選)
if(isset($channel['managingeditor']))$this->channel_pre.= " <managingEditor>".$channel['managingeditor']."</managingEditor>\n";//責任編輯的email(可選)
if(isset($channel['pubdate']))$this->channel_pre.= " <pubDate>".$channel['pubdate']."</pubDate>\n";//頻道內容發布日期,格式遵循RFC822格式(年份可為2位或4位)(可選)
if(isset($channel['lastbuilddate']))$this->channel_pre.= " <lastBuildDate>".$channel['lastbuilddate']."</lastBuildDate>\n";//頻道內容最後的修改日期(Sat, 07 Sep 2002 09:42:31 GMT)(可選)
if(isset($channel['skipdays']))$this->channel_pre.= " <skipDays>".$channel['skipdays']."</skipDays>\n";//提示新聞彙總器,那些天它可以跳過。(可選)
if(isset($channel['copyright']))$this->channel_pre.= " <copyright>".$channel['copyright']."</copyright>\n";//頻道內容的著作權說明(可選)
if(isset($channel['ttl']))$this->channel_pre.= " <ttl>".$channel['ttl']."</ttl>\n";//有效期間,用以指明該頻道可被緩衝的最長時間(可選)
$this->channel_end.= " </channel>\n";
$this->channel_end.= "</rss>\n";
}
/*產生頻道圖片*/
function image($image)
{
if(isset($this->str_image))unset($this->str_image);
$this->str_image.= " <image>\n";
if(isset($image['url']))$this->str_image.= " <url>".$image['url']."</url>\n";//圖片的url(必備)
if(isset($image['title']))$this->str_image.= " <title>".$image['title']."</title>\n";//圖片的標題,用於http的alt屬性(必備)
if(isset($image['link']))$this->str_image.= " <link>".$image['link']."</link>\n";//網站的url(實際中常以頻道的url代替)(必備)
if(isset($image['width']))$this->str_image.= " <width>".$image['width']."</width>\n";//圖片的寬度(象素為單位)最大144,預設88(可選)
if(isset($image['height']))$this->str_image.= " <height>".$image['height']."</height>\n";//圖片的高度(象素為單位)最大400,預設31(可選)
if(isset($image['description']))$this->str_image.= " <description>".$image['description']."</description>\n";//用於link的title屬性(可選)
$this->str_image.= " </image>\n";
}
/*頻道項*/
function item($item)
{
$this->str_item.=" <item>\n";
$this->str_item.=" <title>".$item['title']."</title>\n";//項(item)的標題(必備)
$this->str_item.=" <description>".$item['description']."</description>\n";//項的大綱(必備)
$this->str_item.=" <link>".$item['link']."</link>\n";//項的URL(必備)
if(isset($item['comments']))$this->str_item.=" <comments>".$item['comments']."</comments>\n";//該項的評論(comments)頁的URL(可選)
if(isset($item['guid']))$this->str_item.=" <guid>".$item['guid']."</guid>\n";//1項的唯一標誌符串(可選)
if(isset($item['author']))$this->str_item.=" <author>".$item['author']."</author>\n";//該項作者的email(可選)
if(isset($item['enclosure']))$this->str_item.=" <enclosure>".$item['enclosure']."</enclosure>\n";//描述該附帶的媒體對象(可選)
if(isset($item['category']))$this->str_item.=" <category>".$item['category']."</category>\n";//包含該項的一個或幾個分類(catogory)(可選)
if(isset($item['pubdate']))$this->str_item.=" <pubDate>".$item['pubdate']."</pubDate>\n";//項的發布時間(可選)
if(isset($item['source_url']))$this->str_item.=" <source url=\"".$item['source_url']."\">".$item['source_name']."</source>\n";//該項來自的RSS道(可選)
$this->str_item.=" </item>\n";
}
/*輸出xml*/
function generate()
{
if(isset($this->channel_pre)&&isset($this->channel_end)&&isset($this->str_item))
{
Header("Content-type:text/xml");
echo $this->channel_pre;
echo $this->str_image;
echo $this->str_item;
echo $this->channel_end;
}
}
/*擦除頻道*/
function erase_channel()
{
unset($this->channel_pre);
unset($this->channel_end);
}
/*擦除頻道圖片*/
function erase_image()
{
unset($this->str_image);
}
/*擦除項目*/
function erase_item()
{
unset($this->str_item);
}
/*擦除所有*/
function erase()
{
$this->erase_channel();
$this->erase_image();
$this->erase_item();
}
}
?>