-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFeed.php
207 lines (187 loc) · 6.47 KB
/
Feed.php
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
@file
@brief A Tool for Consuming Atom/RSS Feeds
@see http://blog.sherifmansour.com/?p=302
@see http://stackoverflow.com/questions/595616/what-is-the-correct-mime-type-to-use-for-an-rss-feed
@see http://www.ibm.com/developerworks/library/x-phpatomfeed/
*/
namespace Edoceo\Radix;
class Feed implements \ArrayAccess, \Iterator
{
const MIME_ATOM = 'application/atom+xml';
const MIME_RSS = 'application/rss+xml';
// private $_feed;
// private $_info;
public $_list;
public $_meta;
private $_idx;
private $_max;
private $_uri;
public $xml;
/**
Construct based on URI
*/
function __construct($uri)
{
$this->_uri = $uri;
}
/**
Load the Feed
@return true|false
*/
function load()
{
$res = $this->_curl();
$type = $this->_type();
switch ($type) {
case self::MIME_ATOM:
$this->xml = simplexml_load_string($this->_body);
$this->_meta['name'] = strval($this->xml->title);
$this->_meta['note'] = strval($this->xml->subtitle);
$this->_meta['time_updated'] = strtotime($this->xml->updated);
$this->_meta['author'] = strval($this->xml->author->name);
// radix::dump($this->xml);
// exit;
// foreach ($this->xml->category as $x) {
// // Category[] = strval($x['term']);
// }
foreach ($this->xml->entry as $x) {
$this->_list[] = array(
'name' => strval($x->title),
'note' => null,
'body' => strval($x->content),
);
}
break;
case self::MIME_RSS:
$this->xml = simplexml_load_string($this->_body);
$this->_meta['name'] = strval($this->xml->channel->title);
$this->_meta['note'] = strval($this->xml->channel->description);
$this->_meta['link'] = strval($this->xml->channel->link);
$this->_meta['time_updated'] = strtotime($this->xml->channel->pubDate);
foreach ($this->xml->channel->item as $item) {
// radix::dump($item);
$this->_list[] = array(
'name' => strval($item->title),
'note' => strval($item->description),
'link' => strval($item->link),
'time_updated' => strval($item->pubDate),
);
}
break;
default:
throw new Exception(__CLASS__ . " Cannot Handle: $type");
}
// @todo should parse to common thing here?
$this->_idx = 0;
// $this->_max = count($this->_list);
// $this->_max = count($this->_feed);
return true;
}
/**
Parse Feed to Common Format
*/
function parse()
{
return array(
'meta' => $this->_meta,
'feed' => $this->_feed,
);
}
/**
Array Access Functions
*/
function offsetSet($k, $v) { $this->_data[$k] = $v; }
function offsetExists($k) { return isset($this->_data[$k]); }
function offsetUnset($k) { unset($this->_data[$k]); }
function offsetGet($k) { return isset($this->_data[$k]) ? $this->_data[$k] : null; }
/**
Iterator Access Functions
*/
function current() { return $this->_list[$this->_idx]; }
function next()
{
$r = $this->_list[$this->_idx];
$this->_idx++;
return $r;
}
function key() { return $this->_idx; }
function valid() { return $this->_idx < $this->_max; }
function rewind() { return $this->_idx = 0; }
/**
*/
private function _curl()
{
$ch = curl_init($this->_uri);
// Booleans
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_CRLF, false);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_FILETIME, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FORBID_REUSE, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NETRC, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, false);
// curl_setopt($ch, CURLOPT_BUFFERSIZE, 16384);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 16);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 16);
curl_setopt($ch, CURLOPT_USERAGENT,'Edoceo Radix Feed v2013.19');
$this->_body = curl_exec($ch);
$this->_info = curl_getinfo($ch);
return $this->_info['http_code'];
}
/**
@return canonical mime type for feed
*/
public function _type()
{
$type = strtolower(strtok($this->_info['content_type'],';'));
switch ($type) {
case 'application/atom+xml': // Canonical
case 'atom':
// $this->_data['title'] = strval($this->_xml->title);
// $this->_data['updated'] = strval($this->_xml->updated);
// $this->_data['author'] = strval($this->_xml->author->name);
// $this->_idx = 0;
// $this->_max = count($xml->entry);
return self::MIME_ATOM;
break;
case 'application/rss+xml': // Canonical
case 'application/xml':
case 'text/xml':
case 'rss':
return self::MIME_RSS;
break;
case 'text/html':
// radix::dump($this->_body);
$lead = strtolower(trim(substr($this->_body,0,4)));
switch ($lead) {
case '<rss':
// radix::dump( str_replace('><',">\n<",$this->_body) );
return self::MIME_RSS;
break;
case '<!do':
// HTML, ignore here, handled by exception below
$this->_body = null;
$this->_uri = null;
break;
default:
//radix::dump($this->_body);
// die("Canot read " . html($lead));
}
break;
}
throw new Exception("Cannot read type: $type, perhaps not an Atom or RSS feed?",__LINE__);
return false;
}
}