PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » PHP中高级 » rss生成类
本页主题: rss生成类 打印 | 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题

自由的龙

该用户目前不在线
级别: 中级程序员
精华: 1
发帖: 1264
威望: 1267 点
金钱: 12660 PYMB
贡献值: 0 点
在线时间:0(小时)
注册时间:2006-04-16
最后登录:2006-06-27

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"?>
";
$this->channel_pre.= "<rss version="2.0">
";

$this->channel_pre.= " <channel>
";

$this->channel_pre.= " <title>".$channel['title']."</title>
";//频道名称(必备)
$this->channel_pre.= " <link>".$channel['link']."</link>
";//频道的URL(必备)
$this->channel_pre.= " <description>".$channel['description']."</description>
";//频道的描述(必备)
$this->channel_pre.= " <generator>MagicBoy RSS Generator v1.0</generator>
";//创建此文档的程序(可选)

if(isset($channel['language']))$this->channel_pre.= " <language>".$channel['language']."</language>
";//频道文章所用语言(可选)
if(isset($channel['webmaster']))$this->channel_pre.= " <webMaster>".$channel['webmaster']."</webMaster>
";//负责频道技术事务的网站管理员email(可选)
if(isset($channel['managingeditor']))$this->channel_pre.= " <managingEditor>".$channel['managingeditor']."</managingEditor>
";//责任编辑的email(可选)
if(isset($channel['pubdate']))$this->channel_pre.= " <pubDate>".$channel['pubdate']."</pubDate>
";//频道内容发布日期,格式遵循RFC822格式(年份可为2位或4位)(可选)
if(isset($channel['lastbuilddate']))$this->channel_pre.= " <lastBuildDate>".$channel['lastbuilddate']."</lastBuildDate>
";//频道内容最后的修改日期(Sat, 07 Sep 2002 09:42:31 GMT)(可选)
if(isset($channel['skipdays']))$this->channel_pre.= " <skipDays>".$channel['skipdays']."</skipDays>
";//提示新闻聚合器,那些天它可以跳过。(可选)
if(isset($channel['copyright']))$this->channel_pre.= " <copyright>".$channel['copyright']."</copyright>
";//频道内容的版权说明(可选)
if(isset($channel['ttl']))$this->channel_pre.= " <ttl>".$channel['ttl']."</ttl>
";//有效期,用以指明该频道可被缓存的最长时间(可选)

$this->channel_end.= " </channel>
";
$this->channel_end.= "</rss>
";
}
/*生成频道图片*/
function image($image)
{
if(isset($this->str_image))unset($this->str_image);
$this->str_image.= " <image>
";
if(isset($image['url']))$this->str_image.= "   <url>".$image['url']."</url>
";//图片的url(必备)
if(isset($image['title']))$this->str_image.= "   <title>".$image['title']."</title>
";//图片的标题,用于http的alt属性(必备)
if(isset($image['link']))$this->str_image.= "   <link>".$image['link']."</link>
";//网站的url(实际中常以频道的url代替)(必备)
if(isset($image['width']))$this->str_image.= "   <width>".$image['width']."</width>
";//图片的宽度(象素为单位)最大144,默认88(可选)
if(isset($image['height']))$this->str_image.= "   <height>".$image['height']."</height>
";//图片的高度(象素为单位)最大400,默认31(可选)
if(isset($image['description']))$this->str_image.= "   <description>".$image['description']."</description>
";//用于link的title属性(可选)
$this->str_image.= " </image>
";
}
/*频道项*/
function item($item)
{
$this->str_item.=" <item>
";
$this->str_item.="   <title>".$item['title']."</title>
";//项(item)的标题(必备)
$this->str_item.="   <description>".$item['description']."</description>
";//项的大纲(必备)
$this->str_item.="   <link>".$item['link']."</link>
";//项的URL(必备)

if(isset($item['comments']))$this->str_item.="   <comments>".$item['comments']."</comments>
";//该项的评论(comments)页的URL(可选)
if(isset($item['guid']))$this->str_item.="   <guid>".$item['guid']."</guid>
";//1项的唯一标志符串(可选)
if(isset($item['author']))$this->str_item.="   <author>".$item['author']."</author>
";//该项作者的email(可选)
if(isset($item['enclosure']))$this->str_item.="   <enclosure>".$item['enclosure']."</enclosure>
";//描述该附带的媒体对象(可选)
if(isset($item['category']))$this->str_item.="   <category>".$item['category']."</category>
";//包含该项的一个或几个分类(catogory)(可选)
if(isset($item['pubdate']))$this->str_item.="   <pubDate>".$item['pubdate']."</pubDate>
";//项的发布时间(可选)
if(isset($item['source_url']))$this->str_item.="   <source url="".$item['source_url']."">".$item['source_name']."</source>
";//该项来自的RSS道(可选)
$this->str_item.=" </item>
";
}
/*输出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();
}
}
?>
顶端 Posted: 2006-04-25 16:11 | [楼 主]
PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » PHP中高级

时:11-23 22:29 Copyright © 2006 phpwhy.com 权
ICP05060669

曳息 -