- //RSS类
- <?php
- /**
- * @name:cls.rss.php
- * @uses:RSS处理函数(只针对2.0版本)
- * @version:1.0.0
- * @author:王康
- * @copyright:http://blog.csdn.net/wkjs
- */
- ?>
- <?php
- class rssItem {
- /**
- * 项标题
- *
- * @var string
- *
- */
- private $title = null;
- /**
- * 项链接地址
- *
- * @var string
- *
- */
- private $link = null;
- /**
- * 项内容简要描述
- *
- * @var string
- *
- */
- private $description = null;
- /**
- * 项发布时间
- *
- * @var string
- *
- */
- private $pubDate = null;
- /**
- * 项目录
- *
- * @var string
- *
- */
- private $category = null;
- /**
- * 项作者
- *
- * @var string
- *
- */
- private $author = null;
- /**
- * 项的唯一标识符
- *
- * @var string
- *
- */
- private $guid = null;
- /**
- * 构造函数
- *
- * @param string $title 项标题
- * @param string $link 项链接地址
- * @param string $description 项说明
- * @param string $pubDate 项时间
- * @param string $category 项目录
- * @param string $author 项作者
- * @param string $guid 项唯一标识符
- * @return void
- *
- */
- public function __construct($title = '', $link = '', $description = '', $pubDate = '', $category = '', $author = '', $guid = '') {
- $this->rssItem ( $title, $link, $description, $pubDate, $category, $author, $guid );
- }
- /**
- * 构造函数
- *
- * @param string $title 项标题
- * @param string $link 项链接地址
- * @param string $description 项说明
- * @param string $pubDate 项时间
- * @param string $category 项目录
- * @param string $author 项作者
- * @param string $guid 项唯一标识符
- * @return void
- *
- */
- public function rssItem($title = '', $link = '', $description = '', $pubDate = '', $category = '', $author = '', $guid = '') {
- $this->title = $title;
- $this->link = $link;
- $this->description = $description;
- $this->pubDate = $pubDate;
- $this->category = $category;
- $this->author = $author;
- $this->guid = $guid;
- }
- /**
- * 获取项的值(模范函数)
- *
- * @param string $key key的名称
- * @return mixed key的值
- *
- */
- public function __get($key) {
- return str_replace ( array ("<", ">", "&", "'", "/"" ), array ("<", ">", "&", "'", "*" ), $this->$key ); //替换XML不允许出现的字符
- }
- }
- class rss {
- /**
- * 程序编码
- *
- * @var string
- *
- */
- private $encoding = null;
- /**
- * RSS版本
- *
- * @var string
- *
- */
- private $version = null;
- /**
- * 频道标题
- *
- * @var string
- *
- */
- private $title = null;
- /**
- * 频道链接的总地址
- *
- * @var string
- *
- */
- private $link = null;
- /**
- * 频道描述文字
- *
- * @var string
- *
- */
- private $description = null;
- /**
- * 频道使用的语言
- *
- * @var string
- *
- */
- private $language = null;
- /**
- * 频道发布时间
- *
- * @var string
- *
- */
- private $pubDate = null;
- /**
- * 频道最后更新时间
- *
- * @var string
- *
- */
- private $lastBuildDate = null;
- /**
- * 指向该RSS文件所有格式说明的URL
- *
- * @var string
- *
- */
- private $docs = null;
- /**
- * 频道生成程序名称
- *
- * @var string
- *
- */
- private $generator = null;
- /**
- * This is variable managingEditor description
- *
- * @var mixed
- *
- */
- private $managingEditor = null;
- /**
- * 负责频道技术事物的管理员Email
- *
- * @var string
- *
- */
- private $webMaster = null;
- /**
- * 频道过期时间
- *
- * @var string
- *
- */
- private $ttl = null;
- /**
- * 频道信息表
- *
- * @var array
- *
- */
- private $items = null;
- /**
- * 构造函数
- *
- * @param string $encoding 编码类型
- * @return void
- *
- */
- public function __construct($encoding = 'gb2312') {
- $this->rss ( $encoding );
- }
- /**
- * 构造函数
- *
- * @param string $encoding 编码类型
- * @return void
- *
- */
- public function rss($encoding = 'utf-8') {
- $this->encoding = $encoding;
- $this->version = '2.0';
- $this->items = array ();
- }
- /**
- * 初始化频道总体信息
- *
- * @param string $title 频道标题
- * @param string $link 频道链接的总地址
- * @param string $description 频道描述文字
- * @param string $language 频道使用的语言
- * @param string $pubDate 频道发布时间
- * @param string $lastBuildDate 频道最后更新时间
- * @param string $docs 指向该RSS文件所有格式说明的URL
- * @param string $webMaster 负责频道技术事物的管理员Email
- * @param string $ttl 频道过期时间
- * @return void
- *
- */
- public function initBaseChannel($title = '', $link = '', $description = '', $language = '', $pubDate = '', $lastBuildDate = '', $docs = '', $webMaster = '', $ttl = '') {
- $this->title = $title;
- $this->link = $link;
- $this->description = $description;
- $this->language = $language;
- $this->pubDate = $pubDate;
- $this->lastBuildDate = $lastBuildDate;
- $this->docs = $docs;
- $this->generator = 'RSS2.0 Maker 1.0.0';
- $this->managingEditor = 'walter.k.wang@gmail.com';
- $this->webMaster = $webMaster;
- $this->ttl = $ttl;
- }
- /**
- * 添加项
- *
- * @param string $title 项标题
- * @param string $link 项链接地址
- * @param string $description 项说明
- * @param string $pubDate 项时间
- * @param string $category 项目录
- * @param string $author 项作者
- * @param string $guid 项唯一标识符
- * @return void
- *
- */
- public function addItem($title = '', $link = '', $description = '', $pubDate = '', $category = '', $author = '', $guid = '') {
- $this->items [] = new rssItem ( $title, $link, $description, $pubDate, $category, $author, $guid );
- }
- /**
- * 生成RSS数据
- *
- * @return string RSS数据文件
- *
- */
- public function fetch() {
- $output = '';
- $output .= ( string ) '<?xml version="1.0" encoding="' . $this->encoding . '"?>' . "/n";
- $output .= ( string ) '<?xml-stylesheet type="text/css" href="rss.css"?>'. "/n";
- $output .= ( string ) '<rss version="' . $this->version . '" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">' . "/n/t";
- $output .= ( string ) '<channel>' . "/n/t/t";
- $output .= ( string ) '<title>' . $this->title . '</title>' . "/n/t/t";
- $output .= ( string ) '<link>' . $this->link . '</link>' . "/n/t/t";
- $output .= ( string ) '<description>' . $this->description . '</description>' . "/n/t/t";
- $output .= ( string ) '<language>' . $this->language . '</language>' . "/n/t/t";
- $output .= ( string ) '<pubDate>' . $this->pubDate . '</pubDate>' . "/n/t/t";
- $output .= ( string ) '<lastBuildDate>' . $this->lastBuildDate . '</lastBuildDate>' . "/n/t/t";
- $output .= ( string ) '<docs>' . $this->docs . '</docs>' . "/n/t/t";
- $output .= ( string ) '<generator>' . $this->generator . '</generator>' . "/n/t/t";
- $output .= ( string ) '<managingEditor>' . $this->managingEditor . '</managingEditor>' . "/n/t/t";
- $output .= ( string ) '<webMaster>' . $this->webMaster . '</webMaster>' . "/n/t/t";
- $output .= ( string ) '<ttl>' . $this->ttl . '</ttl>' . "/n/t";
- foreach ( $this->items as $item ) {
- $output .= "/t" . ( string ) '<item>' . "/n/t/t/t";
- $output .= ( string ) '<title>' . $item->title . '</title>' . "/n/t/t/t";
- $output .= ( string ) '<link>' . $item->link . '</link>' . "/n/t/t/t";
- $output .= ( string ) '<description>' . $item->description . '</description>' . "/n/t/t/t";
- $output .= ( string ) '<pubDate>' . $item->pubDate . '</pubDate>' . "/n/t/t/t";
- $output .= ( string ) '<category>' . $item->category . '</category>' . "/n/t/t/t";
- $output .= ( string ) '<author>' . $item->author . '</author>' . "/n/t/t/t";
- $output .= ( string ) '<guid>' . $item->guid . '</guid>' . "/n/t/t";
- $output .= ( string ) '</item>' . "/n/t";
- }
- $output .= ( string ) '</channel>' . "/n";
- $output .= ( string ) '</rss>' . "/n";
- return mb_convert_encoding ( $output, $this->encoding, "auto" ); //格式化文件编码
- }
- /**
- * 创建文件夹
- *
- * @param string $path 文件夹名称
- * @return void
- *
- */
- private function createFolders($path) {
- if (! file_exists ( $path )) {
- $this->createFolders ( dirname ( $path ) );
- mkdir ( $path, 0777 );
- }
- }
- /**
- * 创建文件
- *
- * @param string $filename 文件名称
- * @param string $contents 文件
- * @return void
- *
- */
- public function RSSToFile($save_file) {
- $this->createFolders ( dirname ( $save_file ) );
- if (PHP_VERSION >= '5.0') {
- file_put_contents ( $save_file, $this->fetch () ); //, FILE_APPEND, LOCK_EX );
- } else {
- $fp = @fopen ( $save_file, 'ab+' );
- if ($fp) {
- fwrite ( $fp, $this->fetch () );
- fclose ( $fp );
- }
- }
- chmod ( $save_file, 0777 );
- }
- }
- ?>
- //gb2312编码语言文件,utf-8编码文件内容一样保存文件编码不同
- <?php
- $lang=array('title'=>'测试RSS网页','description'=>'RSS测试程序','itemtitle'=>'新闻项目标题','itmedescription'=>'新闻新闻项目的说明部分','author'=>'王康');
- ?>
- //页面调用代码
- <?php
- define ( 'IN_APPLICATION', true );
- define ( 'encoding', 'gb2312' );
- //define ( 'encoding', 'utf-8' );
- define ( 'ROOT_PATH', dirname ( __FILE__ ) );
- error_reporting ( 0 );
- require_once ('cls.rss.php');
- require_once ('lang.' . encoding . '.php');
- date_default_timezone_set ( 'Asia/Shanghai' );
- $rs = new rss ( encoding );
- $rs->initBaseChannel ( $lang ['title'], 'http://www.demo.com', $lang ['description'], 'zh-cn', date ( "Y-m-d h:i:s", time () ), date ( "Y-m-d h:i:s", time () ), 'http://blog.csdn.net/wkjs', 'walter.k.wang@gmail.com', 5 );
- for($i = 0; $i < 10; $i ++) {
- $rs->addItem ( $lang ['itemtitle'] . $i, 'http://www.demo.com/view.php?type=meiti&id=' . $i, $lang ['itmedescription'] . $i, date ( "Y-m-d h:i:s", time () ), 'MT', $lang ['author'], $i );
- }
- $rs->RSSToFile ( 'xml/index.xml' );
- echo $rs->fetch ();
- ?>
多编码RSS生成类
最新推荐文章于 2022-12-19 20:45:47 发布