Archive for August, 2008

Export RSS 1.0 or 2.0 feeds to an array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
< ?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;
	}
}?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
&lt; ?php 
include("exportrss.php"); 
 
// Parse XML/RSS 2.0 feed 
$feed = new ExportRSS("test.xml", "2.0"); 
$channel = $feed-&gt;get_channel_data(); 
echo "
<h3>Channel</h3>
<strong>Title:</strong> {$channel['title']} 
<strong>Date:</strong> {$channel['date']} 
<strong>Description:</strong> {$channel['description']} 
<strong>Editor:</strong> {$channel['editor']} 
<strong>Webmaster:</strong> {$channel['webmaster']} 
<strong>Language:</strong> {$channel['language']} 
<strong>Generator:</strong> {$channel['generator']} 
<strong>Link:</strong> {$channel['link']} 
 
"; 
 
echo "
<h3>Feed Items</h3>
"; 
foreach ($feed-&gt;get_data() as $item) 
{ 
        echo " 
 
        <strong>Title</strong>: {$item['title']} 
        <strong>Date Published</strong>: {$item['date']} 
        <strong>Description</strong>: {$item['description']} 
        <strong>Link</strong>: {$item['link']} 
 
"; 
} 
?&gt;