【php开源代码栏目提醒】:网学会员鉴于大家对php开源代码十分关注,论文会员在此为大家搜集整理了“class.RSS.php”一文,供大家参考学习
<?php
/*
* Project: MagpieRSS: a simple RSS integration tool
* File: rss_parse.inc includes code for parsing
* RSS, and returning an RSS object
* Author: Kellan Elliott-McCrea <kellan@protest.net>
* Version: 0.51
* License: GPL
*
* The lastest version of MagpieRSS can be obtained from:
* http://magpierss.sourceforge.net
*
* For questions, help, comments, discussion, etc., please join the
* Magpie mailing list:
* magpierss-general@lists.sourceforge.net
*
*/
/*
* NOTES ON RSS PARSING PHILOSOPHY (moderately important):
* MagpieRSS parse all versions of RSS with a few limitation (mod_content, and
* mod_taxonomy support is shaky) into a simple object, with 2 fields,
* the hash 'channel', and the array 'items'.
*
* MagpieRSS is a forgiving and inclusive parser. It currently makes no
* attempt to enforce the validity on an RSS feed. It will include any
* properly formatted tags it finds, allowing to you to mix RSS 0.93, with RSS
* 1.0, with tags or your own imagining. This sort of witches brew is a bad
* bad idea! But Magpie is less pendantic then I am.
*
* RSS validators are readily available on the
web at:
* http://feeds.archive.org/validator/
* http://www.ldodds.com/rss_validator/1.0/validator.html
*
*/
/*
* EXAMPLE PARSE RESULTS:
*
* Magpie tries to parse RSS into ease to use PHP datastructures.
*
* For example, Magpie on encountering RSS 1.0 item entry:
*
* <item rdf:about="http://protest.net/NorthEast/calendrome.cgi?span=event&ID=210257">
* <title>Weekly Peace Vigil</title>
* <link>http://protest.net/NorthEast/calendrome.cgi?span=event&ID=210257</link>
* <description>Wear a white ribbon</description>
* <dc:subject>Peace</dc:subject>
* <ev:startdate>2002-06-01T11:00:00</ev:startdate>
* <ev:location>Northampton, MA</ev:location>
* <ev:enddate>2002-06-01T12:00:00</ev:enddate>
* <ev:type>Protest</ev:type>
* </item>
*
* Would transform it into the following associative array, and push it
* onto the array $rss-items
*
* array(
* title => 'Weekly Peace Vigil',
* link =>
* 'http://protest.net/NorthEast/calendrome.cgi?span=event&ID=210257',
* description => 'Wear a white ribbon',
* dc => array (
* subject => 'Peace'
* ),
* ev => array (
* startdate => '2002-06-01T11:00:00',
* enddate => '2002-06-01T12:00:00',
* type => 'Protest',
* location => 'Northampton, MA'
* )
* )
*
*/
define('RSS', 'RSS');
define('ATOM', 'Atom');
class MagpieRSS {
/*
* Hybrid parser, and object. (probably a bad idea! :)
*
* Useage Example:
*
* $some_rss = "<?xml version="1.0"......
*
* $rss = new MagpieRSS( $some_rss );
*
* // print rss chanel title
* echo $rss->channel['title'];
*
* // print the title of each item
* foreach ($rss->items as $item ) {
* echo $item[title];
* }
*
* see rss_fetch.inc for a simpler interface
*/
var $parser;
var $current_item = array(); // item currently being parsed
var $items = array(); // collection of parsed items
var $channel = array(); // hash of channel fields
var $textinput = array();
var $image = array();
var $feed_type;
var $feed_version;
// parser variables
var $stack = array(); // parser stack
var $inchannel = false;
var $initem = false;
var $incontent = false; // if in Atom <content mode="xml"> field
var $intextinput = false;
var $inimage = false;
var $current_field = '';
var $current_namespace = false;
var $ERROR = "";
/*======================================================================*\
Function: MagpieRSS
Purpose: Constructor, sets up XML parser,parses source,
and populates object..
Input: String containing the RSS to be parsed
\*======================================================================*/
function MagpieRSS ($source) {
# if PHP xml isn't compiled in, die
#
if (!function_exists('xml_parser_create')) {
$this->error( "Failed to load
PHP's XML Extension. " .
"http://www.php.net/manual/en/ref.xml.php",
E_USER_ERROR );
}
$parser = @xml_parser_create();
if (!is_resource($parser))
{
$this->error( "Failed to create an instance of PHP's XML parser. " .
"http://www.php.net/manual/en/ref.xml.php",
E_USER_ERROR );
}
$this->parser = $parser;
# pass in parser, and a reference to this object
# setup handlers
#
xml_set_object( $this->parser, $this );
xml_set_element_handler($this->parser, 'feed_start_element', 'feed_end_element');
xml_set_character_data_handler( $this->parser, 'feed_cdata' );
$status