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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
| <?php
class rss_parser {
function rss_parser()
{
$this->error = '';
$this->file = '';
$this->channel = array();
$this->data = '';
$this->stack = array();
$this->num_items = 0;
$this->xml_parser = xml_parser_create();
xml_set_element_handler($this->xml_parser, "rss_start_element", "rss_end_element");
xml_set_character_data_handler($this->xml_parser, "rss_character_data");
}
function character_data($parser, $data)
{
if (empty($this->data)) $this->data = trim($data);
else $this->data .= ' '.trim($data);
}
function start_element($parser, $name, $attrs)
{
switch($name)
{
case 'RSS':
break;
case 'CHANNEL':
break;
case 'IMAGE':
array_push($this->stack, $name);
break;
case 'ITEM':
array_push($this->stack, $name);
array_push($this->stack, $this->num_items); $this->item[$this->num_items] = array();
$this->num_items++;
break;
case 'TEXTINPUT':
array_push($this->stack, $name);
break;
default:
array_push($this->stack, $name);
break;
}
}
function end_element($parser, $name)
{
switch ($name)
{
case 'RSS':
break;
case 'CHANNEL':
break;
case 'IMAGE':
array_pop($this->stack);
break;
case 'ITEM':
array_pop($this->stack);
array_pop($this->stack);
break;
case 'TEXTINPUT':
array_pop($this->stack);
break;
default:
$element = (implode("']['",$this->stack));
eval("$this->channel['$element']=$this->data;");
array_pop($this->stack);
$this->data = '';
break;
}
}
function parse()
{
if (!($fp = @fopen($this->file, "r")))
{
$this->error = "Could not open RSS source "$this->file".";
return false;
}
while ($data = fread($fp, 4096))
{
if (!xml_parse($this->xml_parser, $data, feof($fp)))
{
$this->error = sprintf("XML error: %s at line %d.",
xml_error_string(xml_get_error_code($this->xml_parser)),
xml_get_current_line_number($this->xml_parser));
return false;
}
}
xml_parser_free($this->xml_parser);
return true;
}
}
function rss_start_element($parser, $name, $attributes)
{
global $rss;
$rss->start_element($parser, $name, $attributes);
if( $description )
{
print "<h2>$description</h2>";
}
}
function rss_end_element($parser, $name)
{
global $rss;
$rss->end_element($parser, $name);
}
function rss_character_data($parser, $data)
{
global $rss;
$rss->character_data($parser, $data);
}
?> |