多编码RSS生成类

  1. //RSS类
  2. <?php
  3. /**  
  4.  * @name:cls.rss.php
  5.  * @uses:RSS处理函数(只针对2.0版本)
  6.  * @version:1.0.0
  7.  * @author:王康
  8.  * @copyright:http://blog.csdn.net/wkjs
  9.  */
  10. ?>
  11. <?php
  12. class rssItem {
  13.     
  14.     /**
  15.      * 项标题
  16.      *
  17.      * @var string 
  18.      *
  19.      */
  20.     private $title = null;
  21.     
  22.     /**
  23.      * 项链接地址
  24.      *
  25.      * @var string 
  26.      *
  27.      */
  28.     private $link = null;
  29.     
  30.     /**
  31.      * 项内容简要描述
  32.      *
  33.      * @var string 
  34.      *
  35.      */
  36.     private $description = null;
  37.     
  38.     /**
  39.      * 项发布时间
  40.      *
  41.      * @var string 
  42.      *
  43.      */
  44.     private $pubDate = null;
  45.     
  46.     /**
  47.      * 项目录
  48.      *
  49.      * @var string 
  50.      *
  51.      */
  52.     private $category = null;
  53.     
  54.     /**
  55.      * 项作者
  56.      *
  57.      * @var string 
  58.      *
  59.      */
  60.     private $author = null;
  61.     
  62.     /**
  63.      * 项的唯一标识符
  64.      *
  65.      * @var string 
  66.      *
  67.      */
  68.     private $guid = null;
  69.     
  70.     /**
  71.      * 构造函数
  72.      *
  73.      * @param string $title 项标题
  74.      * @param string $link 项链接地址
  75.      * @param string $description 项说明
  76.      * @param string $pubDate 项时间
  77.      * @param string $category 项目录
  78.      * @param string $author 项作者
  79.      * @param string $guid 项唯一标识符
  80.      * @return void 
  81.      *
  82.      */
  83.     public function __construct($title = ''$link = ''$description = ''$pubDate = ''$category = ''$author = ''$guid = '') {
  84.         $this->rssItem ( $title$link$description$pubDate$category$author$guid );
  85.     }
  86.     
  87.     /**
  88.      * 构造函数
  89.      *
  90.      * @param string $title 项标题
  91.      * @param string $link 项链接地址
  92.      * @param string $description 项说明
  93.      * @param string $pubDate 项时间
  94.      * @param string $category 项目录
  95.      * @param string $author 项作者
  96.      * @param string $guid 项唯一标识符
  97.      * @return void 
  98.      *
  99.      */
  100.     public function rssItem($title = ''$link = ''$description = ''$pubDate = ''$category = ''$author = ''$guid = '') {
  101.         $this->title = $title;
  102.         $this->link = $link;
  103.         $this->description = $description;
  104.         $this->pubDate = $pubDate;
  105.         $this->category = $category;
  106.         $this->author = $author;
  107.         $this->guid = $guid;
  108.     }
  109.     
  110.     /**
  111.      * 获取项的值(模范函数)
  112.      *
  113.      * @param string $key key的名称
  114.      * @return mixed key的值
  115.      *
  116.      */
  117.     public function __get($key) {
  118.         return str_replace ( array ("<"">""&""'""/"" ), array ("<"">""&""'""*" ), $this->$key );    //替换XML不允许出现的字符
  119.     }
  120. }
  121. class rss {
  122.     
  123.     /**
  124.      * 程序编码
  125.      *
  126.      * @var string 
  127.      *
  128.      */
  129.     private $encoding = null;
  130.     
  131.     /**
  132.      * RSS版本
  133.      *
  134.      * @var string 
  135.      *
  136.      */
  137.     private $version = null;
  138.     
  139.     /**
  140.      * 频道标题
  141.      *
  142.      * @var string 
  143.      *
  144.      */
  145.     private $title = null;
  146.     
  147.     /**
  148.      * 频道链接的总地址
  149.      *
  150.      * @var string 
  151.      *
  152.      */
  153.     private $link = null;
  154.     
  155.     /**
  156.      * 频道描述文字
  157.      *
  158.      * @var string 
  159.      *
  160.      */
  161.     private $description = null;
  162.     
  163.     /**
  164.      * 频道使用的语言
  165.      *
  166.      * @var string 
  167.      *
  168.      */
  169.     private $language = null;
  170.     
  171.     /**
  172.      * 频道发布时间
  173.      *
  174.      * @var string 
  175.      *
  176.      */
  177.     private $pubDate = null;
  178.     
  179.     /**
  180.      * 频道最后更新时间
  181.      *
  182.      * @var string 
  183.      *
  184.      */
  185.     private $lastBuildDate = null;
  186.     
  187.     /**
  188.      * 指向该RSS文件所有格式说明的URL
  189.      *
  190.      * @var string 
  191.      *
  192.      */
  193.     private $docs = null;
  194.     
  195.     /**
  196.      * 频道生成程序名称
  197.      *
  198.      * @var string 
  199.      *
  200.      */
  201.     private $generator = null;
  202.     
  203.     /**
  204.      * This is variable managingEditor description
  205.      *
  206.      * @var mixed 
  207.      *
  208.      */
  209.     private $managingEditor = null;
  210.     
  211.     /**
  212.      * 负责频道技术事物的管理员Email
  213.      *
  214.      * @var string 
  215.      *
  216.      */
  217.     private $webMaster = null;
  218.     
  219.     /**
  220.      * 频道过期时间
  221.      *
  222.      * @var string 
  223.      *
  224.      */
  225.     private $ttl = null;
  226.     
  227.     /**
  228.      * 频道信息表
  229.      *
  230.      * @var array 
  231.      *
  232.      */
  233.     private $items = null;
  234.     
  235.     /**
  236.      * 构造函数
  237.      *
  238.      * @param string $encoding 编码类型
  239.      * @return void 
  240.      *
  241.      */
  242.     public function __construct($encoding = 'gb2312') {
  243.         $this->rss ( $encoding );
  244.     }
  245.     
  246.     /**
  247.      * 构造函数
  248.      *
  249.      * @param string $encoding 编码类型
  250.      * @return void 
  251.      *
  252.      */
  253.     public function rss($encoding = 'utf-8') {
  254.         $this->encoding = $encoding;
  255.         $this->version = '2.0';
  256.         $this->items = array ();
  257.     }
  258.     
  259.     /**
  260.      * 初始化频道总体信息
  261.      *
  262.      * @param string $title 频道标题
  263.      * @param string $link 频道链接的总地址
  264.      * @param string $description 频道描述文字
  265.      * @param string $language 频道使用的语言
  266.      * @param string $pubDate 频道发布时间
  267.      * @param string $lastBuildDate 频道最后更新时间
  268.      * @param string $docs 指向该RSS文件所有格式说明的URL
  269.      * @param string $webMaster 负责频道技术事物的管理员Email
  270.      * @param string $ttl 频道过期时间
  271.      * @return void 
  272.      *
  273.      */
  274.     public function initBaseChannel($title = ''$link = ''$description = ''$language = ''$pubDate = ''$lastBuildDate = ''$docs = ''$webMaster = ''$ttl = '') {
  275.         $this->title = $title;
  276.         $this->link = $link;
  277.         $this->description = $description;
  278.         $this->language = $language;
  279.         $this->pubDate = $pubDate;
  280.         $this->lastBuildDate = $lastBuildDate;
  281.         $this->docs = $docs;
  282.         $this->generator = 'RSS2.0 Maker 1.0.0';
  283.         $this->managingEditor = 'walter.k.wang@gmail.com';
  284.         $this->webMaster = $webMaster;
  285.         $this->ttl = $ttl;
  286.     }
  287.     
  288.     /**
  289.      * 添加项
  290.      *
  291.      * @param string $title 项标题
  292.      * @param string $link 项链接地址
  293.      * @param string $description 项说明
  294.      * @param string $pubDate 项时间
  295.      * @param string $category 项目录
  296.      * @param string $author 项作者
  297.      * @param string $guid 项唯一标识符
  298.      * @return void 
  299.      *
  300.      */
  301.     public function addItem($title = ''$link = ''$description = ''$pubDate = ''$category = ''$author = ''$guid = '') {
  302.         $this->items [] = new rssItem ( $title$link$description$pubDate$category$author$guid ); 
  303.     }
  304.     
  305.     /**
  306.      * 生成RSS数据
  307.      *
  308.      * @return string RSS数据文件
  309.      *
  310.      */
  311.     public function fetch() {
  312.         $output = '';
  313.         $output .= ( string ) '<?xml version="1.0" encoding="' . $this->encoding . '"?>' . "/n";
  314.         $output .= ( string ) '<?xml-stylesheet type="text/css" href="rss.css"?>'"/n";
  315.         $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";
  316.         $output .= ( string ) '<channel>' . "/n/t/t";
  317.         $output .= ( string ) '<title>' . $this->title . '</title>' . "/n/t/t";
  318.         $output .= ( string ) '<link>' . $this->link . '</link>' . "/n/t/t";
  319.         $output .= ( string ) '<description>' . $this->description . '</description>' . "/n/t/t";
  320.         $output .= ( string ) '<language>' . $this->language . '</language>' . "/n/t/t";
  321.         $output .= ( string ) '<pubDate>' . $this->pubDate . '</pubDate>' . "/n/t/t";
  322.         $output .= ( string ) '<lastBuildDate>' . $this->lastBuildDate . '</lastBuildDate>' . "/n/t/t";
  323.         $output .= ( string ) '<docs>' . $this->docs . '</docs>' . "/n/t/t";
  324.         $output .= ( string ) '<generator>' . $this->generator . '</generator>' . "/n/t/t";
  325.         $output .= ( string ) '<managingEditor>' . $this->managingEditor . '</managingEditor>' . "/n/t/t";
  326.         $output .= ( string ) '<webMaster>' . $this->webMaster . '</webMaster>' . "/n/t/t";
  327.         $output .= ( string ) '<ttl>' . $this->ttl . '</ttl>' . "/n/t";
  328.         foreach ( $this->items as $item ) {
  329.             $output .= "/t" . ( string ) '<item>' . "/n/t/t/t";
  330.             $output .= ( string ) '<title>' . $item->title . '</title>' . "/n/t/t/t";
  331.             $output .= ( string ) '<link>' . $item->link . '</link>' . "/n/t/t/t";
  332.             $output .= ( string ) '<description>' . $item->description . '</description>' . "/n/t/t/t";
  333.             $output .= ( string ) '<pubDate>' . $item->pubDate . '</pubDate>' . "/n/t/t/t";
  334.             $output .= ( string ) '<category>' . $item->category . '</category>' . "/n/t/t/t";
  335.             $output .= ( string ) '<author>' . $item->author . '</author>' . "/n/t/t/t";
  336.             $output .= ( string ) '<guid>' . $item->guid . '</guid>' . "/n/t/t";
  337.             $output .= ( string ) '</item>' . "/n/t";
  338.         }
  339.         $output .= ( string ) '</channel>' . "/n";
  340.         $output .= ( string ) '</rss>' . "/n";
  341.         return mb_convert_encoding ( $output$this->encoding, "auto" );    //格式化文件编码
  342.     }
  343.     
  344.     /**
  345.      * 创建文件夹
  346.      *
  347.      * @param string $path 文件夹名称
  348.      * @return void 
  349.      *
  350.      */
  351.     private function createFolders($path) {
  352.         if (! file_exists ( $path )) {
  353.             $this->createFolders ( dirname ( $path ) );
  354.             mkdir ( $path, 0777 );
  355.         }
  356.     }
  357.     
  358.     /**
  359.      * 创建文件
  360.      *
  361.      * @param string $filename 文件名称
  362.      * @param string $contents 文件
  363.      * @return void 
  364.      *
  365.      */
  366.     public function RSSToFile($save_file) {
  367.         $this->createFolders ( dirname ( $save_file ) );
  368.         if (PHP_VERSION >= '5.0') {
  369.             file_put_contents ( $save_file$this->fetch () ); //, FILE_APPEND, LOCK_EX );
  370.         } else {
  371.             $fp = @fopen ( $save_file'ab+' );
  372.             if ($fp) {
  373.                 fwrite ( $fp$this->fetch () );
  374.                 fclose ( $fp );
  375.             }
  376.         }
  377.         chmod ( $save_file, 0777 );
  378.     }
  379. }
  380. ?>
  381. //gb2312编码语言文件,utf-8编码文件内容一样保存文件编码不同
  382. <?php
  383. $lang=array('title'=>'测试RSS网页','description'=>'RSS测试程序','itemtitle'=>'新闻项目标题','itmedescription'=>'新闻新闻项目的说明部分','author'=>'王康');
  384. ?>
  385. //页面调用代码
  386. <?php
  387. define ( 'IN_APPLICATION', true );
  388. define ( 'encoding''gb2312' );
  389. //define ( 'encoding', 'utf-8' );
  390. define ( 'ROOT_PATH', dirname ( __FILE__ ) );
  391. error_reporting ( 0 );
  392. require_once ('cls.rss.php');
  393. require_once ('lang.' . encoding . '.php');
  394. date_default_timezone_set ( 'Asia/Shanghai' );
  395. $rs = new rss ( encoding );
  396. $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 );
  397. for($i = 0; $i < 10; $i ++) {
  398.     $rs->addItem ( $lang ['itemtitle'] . $i'http://www.demo.com/view.php?type=meiti&id=' . $i$lang ['itmedescription'] . $idate ( "Y-m-d h:i:s", time () ), 'MT'$lang ['author'], $i );
  399. }
  400. $rs->RSSToFile ( 'xml/index.xml' );
  401. echo $rs->fetch ();
  402. ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值