RSS PHP类

简单的php生成rss的 类

 
  
1 <? php 2 if ( defined ( ' _CLASS_RSS_PHP ' )) return ; 3 define ( ' _CLASS_RSS_PHP ' , 1 ); 4 /* * 5 * RSS 6 * @author sunny5156 7 * 8 */ 9 class RSS { 10 // public 11 var $rss_ver = " 2.0 " ; 12 var $channel_title = '' ; 13 var $channel_link = '' ; 14 var $channel_description = '' ; 15 var $language = ' zh_CN ' ; 16 var $copyright = '' ; 17 var $webMaster = '' ; 18 var $pubDate = '' ; 19 var $lastBuildDate = '' ; 20 var $generator = ' RedFox RSS Generator ' ; 21 var $content = '' ; 22 var $items = array (); 23 24 /* * 25 * 构造RSS 26 * 27 * @param string $title 28 * @param string $link 29 * @param string $description 30 */ 31 function RSS( $title , $link , $description ) { 32 $this -> channel_title = $title ; 33 $this -> channel_link = $link ; 34 $this -> channel_description = $description ; 35 $this -> pubDate = Date ( ' Y-m-d H:i:s ' , time () ); 36 $this -> lastBuildDate = Date ( ' Y-m-d H:i:s ' , time () ); 37 } 38 39 /* * 40 * 添加节点 41 * 42 * @param string $title 43 * @param string $link 44 * @param string $description 45 * @param string $pubDate 46 */ 47 function AddItem( $title , $link , $description , $pubDate ) { 48 $this -> items [] = array ( 49 ' title ' => $title , 50 ' link ' => $link , 51 ' description ' => $description , 52 ' pubDate ' => $pubDate 53 ); 54 } 55 56 /* * 57 * 生成RSS 58 */ 59 function BuildRSS() { 60 $s = " <?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<rss version=\"2.0\"> " ; 61 // start channel 62 $s .= " <channel>\n " ; 63 $s .= " <title><![CDATA[{ $this ->channel_title}]]></title>\n " ; 64 $s .= " <link><![CDATA[{ $this ->channel_link}]]></link>\n " ; 65 $s .= " <description><![CDATA[{ $this ->channel_description}]]></description>\n " ; 66 $s .= " <language>{ $this ->language}</language>\n " ; 67 if ( ! empty ( $this -> copyright )) { 68 $s .= " <copyright><![CDATA[{ $this ->copyright}]]></copyright>\n " ; 69 } 70 if ( ! empty ( $this -> webMaster )) { 71 $s .= " <webMaster><![CDATA[{ $this ->webMaster}]]></webMaster>\n " ; 72 } 73 if ( ! empty ( $this -> pubDate )) { 74 $s .= " <pubDate>{ $this ->pubDate}</pubDate>\n " ; 75 } 76 77 if ( ! empty ( $this -> lastBuildDate )) { 78 $s .= " <lastBuildDate>{ $this ->lastBuildDate}</lastBuildDate>\n " ; 79 } 80 81 if ( ! empty ( $this -> generator )) { 82 $s .= " <generator>{ $this ->generator}</generator>\n " ; 83 } 84 85 // start items 86 for ( $i = 0 ; $i < count ( $this -> items ); $i ++ ) { 87 $s .= " <item>\n " ; 88 $s .= " <title><![CDATA[{ $this ->items[ $i ]['title']}]]></title>\n " ; 89 $s .= " <link><![CDATA[{ $this ->items[ $i ]['link']}]]></link>\n " ; 90 $s .= " <description><![CDATA[{ $this ->items[ $i ]['description']}]]></description>\n " ; 91 $s .= " <pubDate>{ $this ->items[ $i ]['pubDate']}</pubDate>\n " ; 92 $s .= " </item>\n " ; 93 } 94 95 // close channel 96 $s .= " </channel>\n</rss> " ; 97 $this -> content = $s ; 98 } 99 100 /* * 101 * 显示RSS 102 */ 103 function Show() { 104 if ( empty ( $this -> content )) 105 $this -> BuildRSS (); 106 print_r ( $this -> content); 107 } 108 109 /* * 110 * 生成文件 111 * @param string $fname 112 * @return boolean 113 */ 114 function SaveToFile( $fname ) { 115 $handle = fopen ( $fname , ' wb ' ); 116 if ( $handle === false ) 117 return false ; 118 fwrite ( $handle , $this -> content ); 119 fclose ( $handle ); 120 } 121 } 122 ?>

转载于:https://www.cnblogs.com/sunny5156/archive/2013/01/18/2866424.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值