<?php
<?php
/**
* Class name: RSS
*/
if (defined('_CLASS_RSS_PHP')) return;
define('_CLASS_RSS_PHP',1);
class rss74 {
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';
// RSS feed title:
var $title = "Untitled";
// RSS description:
var $desc = "";
// RSS base url
// -> Example: http://www.jonasjohn.de/
var $base_url = "";
// XSL file for the resulting RSS feed:
var $xsl_file = 'rss.xsl';
// RSS 2.0 Specification (URL):
var $doc_url = 'http://blogs.law.harvard.edu/tech/rss';
// Copyright text:
// -> Example: Copyright 2006, Jonas John
var $copyright = '';
// RSS language setting:
// Example: en-us, de-de, fr-fr
var $language = 'en-us';
// Managing editor and webmaster:
// (should contain a E-Mail adress)
var $managing_editor = '';
var $webmaster = '';
// Feedburner URL
// Example: http://feeds.feedburner.com/codedump-rss
// (If a FB URL is set, all requests except the Feedburner and
Google
// requests will be redricted to the feedburner URL)
var $feedburner_url = '';
// RSS generator:
var $generator = 'rss74/v0.3';
// Limit RSS entries to:
// (Example: 20, 30, 40, 50, etc.)
var $limit_entries = 20;
var $content = '';
var $items = array();
// RSS items:
var $items = array();
/**************************************************************************/
// 函数名: RSS
// 功能: 构造函数
// 参数: $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());
}
/**************************************************************************/
// 函数名: AddItem
// 功能: 添加一个节点
// 参数: $title
// $link
// $description $pubDate
/**************************************************************************/
function AddItem($title, $link, $description ,$pubDate) {
$this->items[] = array('title' => $title ,
'link' => $link,
'description' => $description,
'pubDate' => $pubDate);
}
/**************************************************************************/
// 函数名: BuildRSS
// 功能: 生成rss xml文件内容
/**************************************************************************/
function BuildRSS() {
$s = " <?xml version="1.0" encoding="gb2312" ?>
n<rss version="2.0">n";
// start channel
$s .= "<channel>n";
$s .=
"<title><![CDATA[{$this->channel_title}]]></title>n";
$s .=
"<link><![CDATA[{$this->channel_link}]]></link>n";
$s .=
"<description><![CDATA[{$this->channel_description}]]></description>n";
$s .= "<language>{$this->language}</language>n";
if (!empty($this->copyright)) {
$s .=
"<copyright><![CDATA[{$this->copyright}]]></copyright>n";
}
if (!empty($this->webMaster)) {
$s .=
"<webMaster><![CDATA[{$this->webMaster}]]></webMaster>n";
}
if (!empty($this->pubDate)) {
$s .= "<pubDate>{$this->pubDate}</pubDate>n";
}
// constructor:
function rss74(){
}
function add_entry($entry){
// create date key:
$date = isset($entry['date']) ? $entry['date'] : time();
if (!empty($this->lastBuildDate)) {
$s .=
"<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>n";
}
// add unique string:
$date .= '_' . md5($entry['title']);
$this->items[$date] = $entry;
}
function _get_val(&$array, $key){
return isset($array[$key]) ? $array[$key] : '';
}
function _exists_val(&$array, $key){
return isset($array[$key]);
}
function get_rss_headers($rss_webpath){
return '<link rel="alternate" type="application/rss+xml"
title="RSS" href="'.$rss_webpath.'" />';
}
function print_rss(){
global $_SERVER;
krsort($this->items);
$this->items = array_slice($this->items, 0,
$this->limit_entries);
$first_item = array_keys($this->items);
$first_item = $first_item[0];
$last_change = $this->items[$first_item]['date'];
header('Last-Modified: '.gmdate('D, d M Y H:i:s',
$last_change).' GMT');
header('Content-Type: text/xml; charset=utf-8');
if (!empty($this->generator)) {
$s .= "<generator>{$this->generator}</generator>n";
}
if (!empty($this->feedburner_url)){
if (!preg_match("/feedburner/i",
$_宝马娱乐在线,SERVER['HTTP_USER_AGENT']) and !preg_match("/google/i",
$_SERVER['HTTP_USER_AGENT'])) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $this->feedburner_url);
print '<a
href="'.$this->feedburner_url.'">Redirecting...</a>';
return true;
}
}
// start items
for ($i=0;$i<count($this->items);$i++) {
$s .= "<item>n";
$s .=
"<title><![CDATA[{$this->items[$i]['title']}]]></title>n";
$s .=
"<link><![CDATA[{$this->items[$i]['link']}]]></link>n";
$s .=
"<description><![CDATA[{$this->items[$i]['description']}]]></description>n";
$s .=
"<pubDate>{$this->items[$i]['pubDate']}</pubDate>n";
$s .= "</item>n";
}
print '<?xml version="1.0" encoding="utf-8"?>' . " ";
if ($this->xsl_file != '')
print '<?xml-stylesheet href="rss.xsl" type="text/xsl"
media="screen"?>' . " ";
print '<!DOCTYPE rss [<!ENTITY % HTMLlat1 PUBLIC
"-//W3C//ENTITIES Latin 1 for XHTML//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent">]>' . " ";
print "<!-- Hello! This web page is a RSS file that is meant
to be read by a RSS reader application. Look at
http://en.wikipedia.org/wiki/RSS to learn more about RSS. -->";
print '<rss version="2.0"
xmlns:xhtml="http://www.w3.org/1999/xhtml">' . " ";
print '<channel>' . " ";
print "
".'<title>'.htmlentities($this->title).'</title>' . "
";
if (!empty($this->desc))
print "
".'<description>'.htmlentities($this->desc).'</description>'
. " ";
if (!empty($this->base_url))
print "
".'<link>'.htmlentities($this->base_url).'</link>' . "
";
print " ".'<lastBuildDate>'.date('r',
time()).'</lastBuildDate>' . " ";
print " ".'<pubDate>'.date('r', time()).'</pubDate>'
. " ";
if (!empty($this->generator))
print "
".'<generator>'.$this->generator.'</generator>' . " ";
if (!empty($this->copyright))
print "
".'<copyright>'.$this->copyright.'</copyright>' . " ";
if (!empty($this->doc_url))
print " ".'<docs>'.$this->doc_url.'</docs>'
. " ";
if (!empty($this->language))
print "
<language>".$this->language."</language> ";
if (!empty($this->managing_editor))
print "
<managingEditor>".$this->managing_editor."</managingEditor>
";
// close channel
$s .= "</channel>n</rss>";
$this->content = $s;
}
/**************************************************************************/
// 函数名: Show
// 功能: 将产生的rss内容直接打印输出
/**************************************************************************/
function Show() {
if (empty($this->content)) $this->BuildRSS();
echo($this->content);
}
/**************************************************************************/
// 函数名: SaveToFile
// 功能: 将产生的rss内容保存到文件
// 参数: $fname 要保存的文件名
/**************************************************************************/
function SaveToFile($fname) {
$handle = fopen($fname, 'wb');
if ($handle === false) return false;
fwrite($handle, $this->content);
fclose($handle);
}
}
?>
if (!empty($this->webmaster))
print "
<webMaster>".$this->webmaster."</webMaster> ";
while(list($num, $item) = each($this->items)){
print " <item> ";
print "
<title>".htmlentities($this->_get_val($item,
'title'))."</title> ";
if ($this->_exists_val($item, 'url')){
print " <link>".$this->_get_val($item,
'url')."</link> ";
print " <guid
isPermaLink="true">".$this->_get_val($item,
'url')."</guid> ";
}
if ($this->_exists_val($item, 'desc'))
print "
<description><![CDATA[".$this->_get_val($item,
'desc')."]]></description> ";
if ($this->_exists_val($item, 'date')){
print " <pubDate>".date('r',
intval($this->_get_val($item, 'date')))."</pubDate> ";
}
$cats = array();
while(list($cn, $citem) = each($cats)){
print " <category>$citem</category> ";
}
print " </item> ";
}
本文由宝马娱乐在线发布于网络频道,转载请注明出处:php rss分析类程序