Tuesday, September 18, 2007

php Script to fetch RSS info

/**
* Class name: RSS
* Author : LeadStar Team
* website : http://www.leadstar.com.cn/
* CopyRight : LeadStar Team
*/
if (defined('_CLASS_RSS_PHP')) return;
define('_CLASS_RSS_PHP',1);

class RSS {
//public
var $rss_ver = "2.0";
var $channel_title = '';
var $channel_link = '';
var $channel_description = '';
var $language = 'zh_CN';
var $copyright = '';
var $webMaster = '';
var $pubDate = '';
var $lastBuildDate = '';
var $generator = 'RedFox RSS Generator';

var $content = '';
var $items = array();

/**************************************************************************/
// function name: RSS
// function description: construct
// parameter: $title
// $link
// $description
/**************************************************************************/
function RSS($title, $link, $description) {
$this->channel_title = $title;
$this->channel_link = $link;
$this->channel_description = $description;
$this->pubDate = Date('Y-m-d H:i:s',time());
$this->lastBuildDate = Date('Y-m-d H:i:s',time());
}
/**************************************************************************/
// function name: AddItem
// function description: add a new node
// parameter: $title
// $link
// $description $pubDate
/**************************************************************************/
function AddItem($title, $link, $description ,$pubDate) {
$this->items[] = array('title' => $title ,
'link' => $link,
'description' => $description,
'pubDate' => $pubDate);
}
/**************************************************************************/
// function name: BuildRSS
// function description: generate rss xml file content
/**************************************************************************/
function BuildRSS() {
$s = "
\n\n";
// start channel
$s .= "\n";
$s .= "channel_title}]]>\n";
$s .= "<1ink>channel_link}]]>\n";
$s .= "channel_description}]]>\n";
$s .= "{$this->language}\n";
if (!empty($this->copyright)) {
$s .= "copyright}]]>\n";
}
if (!empty($this->webMaster)) {
$s .= "webMaster}]]>\n";
}
if (!empty($this->pubDate)) {
$s .= "{$this->pubDate}\n";
}

if (!empty($this->lastBuildDate)) {
$s .= "{$this->lastBuildDate}\n";
}

if (!empty($this->generator)) {
$s .= "{$this->generator}\n";
}

// start items
for ($i=0;$iitems);$i++) {
$s .= "\n";
$s .= "items[$i]['title']}]]>\n";
$s .= "<1ink>items[$i]['link']}]]>\n";
$s .= "items[$i]['description']}]]>\n";
$s .= "{$this->items[$i]['pubDate']}\n";
$s .= "
\n";
}

// close channel
$s .= "
\n
";
$this->content = $s;
}
/**************************************************************************/
// function name: Show
// function description: print out the RSS xml file content
/**************************************************************************/
function Show() {
if (empty($this->content)) $this->BuildRSS();
echo($this->content);
}
/**************************************************************************/
// function name: SaveToFile
// function description: save the RSS content to a file
// parameter: $fname the file name to be saved
/**************************************************************************/
function SaveToFile($fname) {
$handle = fopen($fname, 'wb');
if ($handle === false) return false;
fwrite($handle, $this->content);
fclose($handle);
}
}

No comments: