Export RSS 1.0 or 2.0 feeds to an array

maddog39 0 Tallied Votes 287 Views Share

This class will allow you to take an RSS feed (local or remote) and "export" it to an easily managed/viewed array. Below is an example of it usage:

<?php
include("exportrss.php");

// Parse XML/RSS 2.0 feed
$feed = new ExportRSS("test.xml", "2.0");
$channel = $feed->get_channel_data();
echo "<h3>Channel</h3>
<p>
<b>Title:</b> {$channel['title']}<br/>
<b>Date:</b> {$channel['date']}<br/>
<b>Description:</b> {$channel['description']}<br/>
<b>Editor:</b> {$channel['editor']}<br/>
<b>Webmaster:</b> {$channel['webmaster']}<br/>
<b>Language:</b> {$channel['language']}<br/>
<b>Generator:</b> {$channel['generator']}<br/>
<b>Link:</b> {$channel['link']}
</p>";

echo "<h3>Feed Items</h3>";
foreach ($feed->get_data() as $item)
{
	echo "<p>
	<b>Title</b>: {$item['title']}<br/>
	<b>Date Published</b>: {$item['date']}<br/>
	<b>Description</b>: {$item['description']}<br/>
	<b>Link</b>: {$item['link']}</p>";
}
?>
<?php
/*
 *      exportrss.php
 *
 *      Copyright 2007 Alec Hussey <alec.hussey@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

class ExportRSS
{
	protected $data = array();
	protected $channel = array();
	protected $rawdata = array();

	public function __construct($data, $version)
	{
		$feed = @file_get_contents($data);
		$this->rawdata = simplexml_load_string($feed);
		
		// Parse the XML/RSS file
		switch ($version)
		{
			case "1.0":
			{
				$this->channel = array(
					"title"       => $this->rawdata->channel->title,
					"link"        => $this->rawdata->channel->link,
					"description" => $this->rawdata->channel->description,
					"image"       => $this->rawdata->channel->image,
					"items"       => $this->rawdata->channel->items,
					"textinput"   => $this->rowdata->channel->textinput
				);
				
				foreach ($this->rawdata->item as $item)
				{
					$row = array(
						"title"       => $item->title,
						"link"        => $item->link,
						"description" => $item->description
					);
					array_push($this->data, $row);
				}
				break;
			}
			case "2.0":
			{
				$this->channel = array(
					"title"       => $this->rawdata->channel->title,
					"link"        => $this->rawdata->channel->link,
					"description" => $this->rawdata->channel->description,
					"language"    => $this->rawdata->channel->language,
					"date"        => $this->rawdata->channel->pubDate,
					"builddate"   => $this->rawdata->channel->lastBuildDate,
					"docs"        => $this->rawdata->channel->docs,
					"generator"   => $this->rawdata->channel->generator,
					"editor"      => $this->rawdata->channel->managingEditor,
					"webmaster"   => $this->rawdata->channel->webMaster,
				);
				
				foreach ($this->rawdata->channel->item as $item)
				{
					$row = array(
						"title"       => $item->title,
						"link"        => $item->link,
						"description" => $item->description,
						"enclosure"   => $this->enclosure
						"date"        => $item->pubDate,
						"guid"        => $item->guid
					);
					array_push($this->data, $row);
				}
				break;
			}
			default:
			{
				echo "ExportRSS::WARNING: invalid version was specified";
				break;
			}
		}
	}
	
	public function get_raw_data()
	{
		return $this->rawdata;
	}
	
	public function get_channel_data()
	{
		return $this->channel;
	}
	
	public function get_data()
	{
		return $this->data;
	}
}
?>