- <?php
- ?>
- <?php
- class rssItem {
-
-
- private $title = null;
-
-
- private $link = null;
-
-
- private $description = null;
-
-
- private $pubDate = null;
-
-
- private $category = null;
-
-
- private $author = null;
-
-
- private $guid = null;
-
-
- public function __construct($title = '', $link = '', $description = '', $pubDate = '', $category = '', $author = '', $guid = '') {
- $this->rssItem ( $title, $link, $description, $pubDate, $category, $author, $guid );
- }
-
-
- 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;
- }
-
-
- public function __get($key) {
- return str_replace ( array ("<", ">", "&", "'", "\"" ), array ("<", ">", "&", "'", "*" ), $this->$key );
- }
- }
- class rss {
-
-
- private $encoding = null;
-
-
- private $version = null;
-
-
- private $title = null;
-
-
- private $link = null;
-
-
- private $description = null;
-
-
- private $language = null;
-
-
- private $pubDate = null;
-
-
- private $lastBuildDate = null;
-
-
- private $docs = null;
-
-
- private $generator = null;
-
-
- private $managingEditor = null;
-
-
- private $webMaster = null;
-
-
- private $ttl = null;
-
-
- private $items = null;
-
-
- public function __construct($encoding = 'gb2312') {
- $this->rss ( $encoding );
- }
-
-
- public function rss($encoding = 'utf-8') {
- $this->encoding = $encoding;
- $this->version = '2.0';
- $this->items = array ();
- }
-
-
- 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;
- }
-
-
- public function addItem($title = '', $link = '', $description = '', $pubDate = '', $category = '', $author = '', $guid = '') {
- $this->items [] = new rssItem ( $title, $link, $description, $pubDate, $category, $author, $guid );
- }
-
-
- 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" );
- }
-
-
- private function createFolders($path) {
- if (! file_exists ( $path )) {
- $this->createFolders ( dirname ( $path ) );
- mkdir ( $path, 0777 );
- }
- }
-
-
- public function RSSToFile($save_file) {
- $this->createFolders ( dirname ( $save_file ) );
- if (PHP_VERSION >= '5.0') {
- file_put_contents ( $save_file, $this->fetch () );
- } else {
- $fp = @fopen ( $save_file, 'ab+' );
- if ($fp) {
- fwrite ( $fp, $this->fetch () );
- fclose ( $fp );
- }
- }
- chmod ( $save_file, 0777 );
- }
- }
- ?>
- <?php
- $lang=array('title'=>'测试RSS网页','description'=>'RSS测试程序','itemtitle'=>'新闻项目标题','itmedescription'=>'新闻新闻项目的说明部分','author'=>'王康');
- ?>
- <?php
- define ( 'IN_APPLICATION', true );
- define ( 'encoding', 'gb2312' );
- 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 ();
- ?>
posted on
2008-11-07 09:25
wkjs
阅读(
178)
评论()
编辑
收藏
举报