-
Notifications
You must be signed in to change notification settings - Fork 2
/
FeedParser.php
554 lines (493 loc) · 15.5 KB
/
FeedParser.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<?php
/*=======================================================================+
| PHP Universal Feed Parser |
+------------------------------------------------------------------------/
Author : Anis uddin Ahmad <[email protected]>
Web : http://www.ajaxray.com
Publish Date : March 24, 2008
LICENSE
----------------------------------------------------------------------
PHP Universal Feed Parser 1.0 - A PHP class to parse RSS 1.0, RSS 2.0 and ATOM 1.0 feed.
Copyright (C) 2008 Anis uddin Ahmad <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
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.
To read the license please visit http://www.gnu.org/copyleft/gpl.html
=======================================================================
HOW TO USE
-----------------------------------------------------------------------
It's very easy to use. Just follow this 3 steps:
1. Include the file
include('FeedParser.php');
2. Create an object of FeedParser class
$Parser = new FeedParser();
3. Parse the URL you want to featch
$Parser->parse('http://www.sitepoint.com/rss.php');
Done.
Now you can use this functions to get various information of parsed feed:
1. $Parser->getChannels() - To get all channel elements as array
2. $Parser->getItems() - To get all feed elements as array
3. $Parser->getChannel($name) - To get a channel element by name
4. $Parser->getItem($index) - To get a feed element as array by it's index
5. $Parser->getTotalItems() - To get the number of total feed elements
6. $Parser->getFeedVersion() - To get the detected version of parsed feed
7. $Parser->getParsedUrl() - To get the parsed feed URL
=======================================================================
IMPORTANT NOTES
-----------------------------------------------------------------------
1. All array keys are must be UPPERCASE
2. All dates are converted to timestamp
3. Attributes of a tag will be found under TAGNAME_ATTRS index
example: Attributes of $item['GUID'] will be found as $item['GUID_ATTRS']
4. The tags which have subtags will be an array and sub tags will be found as it's element
example: IMAGE tag in RSS 2.0
========================================================================
EXAMPLES
-----------------------------------------------------------------------
To see more details and examples, please visit:
http://www.ajaxray.com/blog/2008/05/02/php-universal-feed-parser-lightweight-php-class-for-parsing-rss-and-atom-feeds/
========================================================================
*/
/**
* PHP Univarsel Feed Parser class
*
* Parses RSS 1.0, RSS2.0 and ATOM Feed
*
* @license GNU General Public License (GPL)
* @author Anis uddin Ahmad <[email protected]>
* @link http://www.ajaxray.com/blog/2008/05/02/php-universal-feed-parser-lightweight-php-class-for-parsing-rss-and-atom-feeds/
*/
class FeedParser{
private $xmlParser = null;
private $insideItem = array(); // Keep track of current position in tag tree
private $currentTag = null; // Last entered tag name
private $currentAttr = null; // Attributes array of last entered tag
private $namespaces = array(
'http://purl.org/rss/1.0/' => 'RSS 1.0',
'http://purl.org/rss/1.0/modules/content/' => 'RSS 2.0',
'http://www.w3.org/2005/Atom' => 'ATOM 1',
); // Namespaces to detact feed version
private $itemTags = array('ITEM','ENTRY'); // List of tag names which holds a feed item
private $channelTags = array('CHANNEL','FEED'); // List of tag names which holds all channel elements
private $dateTags = array('UPDATED','PUBDATE','DC:DATE');
private $hasSubTags = array('IMAGE','AUTHOR'); // List of tag names which have sub tags
private $channels = array();
private $items = array();
private $itemIndex = 0;
private $url = null; // The parsed url
private $version = null; // Detected feed version
/**
* Constructor - Initialize and set event handler functions to xmlParser
*/
function __construct()
{
$this->xmlParser = xml_parser_create();
xml_set_object($this->xmlParser, $this);
xml_set_element_handler($this->xmlParser, "startElement", "endElement");
xml_set_character_data_handler($this->xmlParser, "characterData");
}
/*-----------------------------------------------------------------------+
| Public functions. Use to parse feed and get informations. |
+-----------------------------------------------------------------------*/
/**
* Get all channel elements
*
* @access public
* @return array - All chennels as associative array
*/
public function getChannels()
{
return $this->channels;
}
/**
* Get all feed items
*
* @access public
* @return array - All feed items as associative array
*/
public function getItems()
{
return $this->items;
}
/**
* Get total number of feed items
*
* @access public
* @return number
*/
public function getTotalItems()
{
return count($this->items);
}
/**
* Get a feed item by index
*
* @access public
* @param number index of feed item
* @return array feed item as associative array of it's elements
*/
public function getItem($index)
{
if($index < $this->getTotalItems())
{
return $this->items[$index];
}
else
{
throw new Exception("Item index is learger then total items.");
return false;
}
}
/**
* Get a channel element by name
*
* @access public
* @param string the name of channel tag
* @return string
*/
public function getChannel($tagName)
{
if(array_key_exists(strtoupper($tagName), $this->channels))
{
return $this->channels[strtoupper($tagName)];
}
else
{
throw new Exception("Channel tag $tagName not found.");
return false;
}
}
/**
* Get the parsed URL
*
* @access public
* @return string
*/
public function getParsedUrl()
{
if(empty($this->url))
{
throw new Exception("Feed URL is not set yet.");
return FALSE;
}
else
{
return $this->url;
}
}
/**
* Get the detected Feed version
*
* @access public
* @return string
*/
public function getFeedVersion()
{
return $this->version;
}
/**
* Parses a feed url
*
* @access public
* @param srting teh feed url
* @return void
*/
public function parse($url)
{
$this->url = $url;
$URLContent = $this->getUrlContent();
if($URLContent)
{
$segments = str_split($URLContent, 4096);
foreach($segments as $index=>$data)
{
$lastPiese = ((count($segments)-1) == $index)? true : false;
xml_parse($this->xmlParser, $data, $lastPiese)
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xmlParser)),
xml_get_current_line_number($this->xmlParser)));
}
xml_parser_free($this->xmlParser);
}
else
{
die('Sorry! cannot load the feed url.');
}
if(empty($this->version))
{
die('Sorry! cannot detect the feed version.');
}
}
// End public functions -------------------------------------------------
/*-----------------------------------------------------------------------+
| Private functions. Be careful to edit them. |
+-----------------------------------------------------------------------*/
/**
* Load the whole contents of a RSS/ATOM page
*
* @access private
* @return string
*/
private function getUrlContent()
{
if(empty($this->url))
{
throw new Exception("URL to parse is empty!.");
return false;
}
if($content = @file_get_contents($this->url))
{
return $content;
}
else
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if(empty($error))
{
return $content;
}
else
{
throw new Exception("Erroe occured while loading url by cURL. <br />\n" . $error) ;
return false;
}
}
}
/**
* Handle the start event of a tag while parsing
*
* @access private
* @param object the xmlParser object
* @param string name of currently entering tag
* @param array array of attributes
* @return void
*/
private function startElement($parser, $tagName, $attrs)
{
if(!$this->version)
{
$this->findVersion($tagName, $attrs);
}
array_push($this->insideItem, $tagName);
$this->currentTag = $tagName;
$this->currentAttr = $attrs;
}
/**
* Handle the end event of a tag while parsing
*
* @access private
* @param object the xmlParser object
* @param string name of currently ending tag
* @return void
*/
private function endElement($parser, $tagName)
{
if (in_array($tagName, $this->itemTags))
{
$this->itemIndex++;
}
array_pop($this->insideItem);
$this->currentTag = $this->insideItem[count($this->insideItem)-1];
}
/**
* Handle character data of a tag while parsing
*
* @access private
* @param object the xmlParser object
* @param string tag value
* @return void
*/
private function characterData($parser, $data)
{
//Converting all date formats to timestamp
if(in_array($this->currentTag, $this->dateTags))
{
$data = strtotime($data);
}
if($this->inChannel())
{
// If has subtag, make current element an array and assign subtags as it's element
if(in_array($this->getParentTag(), $this->hasSubTags))
{
if(! is_array($this->channels[$this->getParentTag()]))
{
$this->channels[$this->getParentTag()] = array();
}
$this->channels[$this->getParentTag()][$this->currentTag] .= strip_tags($this->unhtmlentities((trim($data))));
return;
}
else
{
if(! in_array($this->currentTag, $this->hasSubTags))
{
$this->channels[$this->currentTag] .= strip_tags($this->unhtmlentities((trim($data))));
}
}
if(!empty($this->currentAttr))
{
$this->channels[$this->currentTag . '_ATTRS'] = $this->currentAttr;
//If the tag has no value
if(strlen($this->channels[$this->currentTag]) < 2)
{
//If there is only one attribute, assign the attribute value as channel value
if(count($this->currentAttr) == 1)
{
foreach($this->currentAttr as $attrVal)
{
$this->channels[$this->currentTag] = $attrVal;
}
}
//If there are multiple attributes, assign the attributs array as channel value
else
{
$this->channels[$this->currentTag] = $this->currentAttr;
}
}
}
}
elseif($this->inItem())
{
// If has subtag, make current element an array and assign subtags as it's elements
if(in_array($this->getParentTag(), $this->hasSubTags))
{
if(! is_array($this->items[$this->itemIndex][$this->getParentTag()]))
{
$this->items[$this->itemIndex][$this->getParentTag()] = array();
}
$this->items[$this->itemIndex][$this->getParentTag()][$this->currentTag] .= strip_tags($this->unhtmlentities((trim($data))));
return;
}
else
{
if(! in_array($this->currentTag, $this->hasSubTags))
{
$this->items[$this->itemIndex][$this->currentTag] .= strip_tags($this->unhtmlentities((trim($data))));
}
}
if(!empty($this->currentAttr))
{
$this->items[$this->itemIndex][$this->currentTag . '_ATTRS'] = $this->currentAttr;
//If the tag has no value
if(strlen($this->items[$this->itemIndex][$this->currentTag]) < 2)
{
//If there is only one attribute, assign the attribute value as feed element's value
if(count($this->currentAttr) == 1)
{
foreach($this->currentAttr as $attrVal)
{
$this->items[$this->itemIndex][$this->currentTag] = $attrVal;
}
}
//If there are multiple attributes, assign the attribute array as feed element's value
else
{
$this->items[$this->itemIndex][$this->currentTag] = $this->currentAttr;
}
}
}
}
}
/**
* Find out the feed version
*
* @access private
* @param string name of current tag
* @param array array of attributes
* @return void
*/
private function findVersion($tagName, $attrs)
{
$namespace = array_values($attrs);
foreach($this->namespaces as $value =>$version)
{
if(in_array($value, $namespace))
{
$this->version = $version;
return;
}
}
}
private function getParentTag()
{
return $this->insideItem[count($this->insideItem) - 2];
}
/**
* Detect if current position is in channel element
*
* @access private
* @return bool
*/
private function inChannel()
{
if($this->version == 'RSS 1.0')
{
if(in_array('CHANNEL', $this->insideItem) && $this->currentTag != 'CHANNEL')
return TRUE;
}
elseif($this->version == 'RSS 2.0')
{
if(in_array('CHANNEL', $this->insideItem) && !in_array('ITEM', $this->insideItem) && $this->currentTag != 'CHANNEL')
return TRUE;
}
elseif($this->version == 'ATOM 1')
{
if(in_array('FEED', $this->insideItem) && !in_array('ENTRY', $this->insideItem) && $this->currentTag != 'FEED')
return TRUE;
}
return FALSE;
}
/**
* Detect if current position is in Item element
*
* @access private
* @return bool
*/
private function inItem()
{
if($this->version == 'RSS 1.0' || $this->version == 'RSS 2.0')
{
if(in_array('ITEM', $this->insideItem) && $this->currentTag != 'ITEM')
return TRUE;
}
elseif($this->version == 'ATOM 1')
{
if(in_array('ENTRY', $this->insideItem) && $this->currentTag != 'ENTRY')
return TRUE;
}
return FALSE;
}
//This function is taken from lastRSS
/**
* Replace HTML entities &something; by real characters
*
*
* @access private
* @author Vojtech Semecky <[email protected]>
* @link http://lastrss.oslab.net/
* @param string
* @return string
*/
private function unhtmlentities($string)
{
// Get HTML entities table
$trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES);
// Flip keys<==>values
$trans_tbl = array_flip ($trans_tbl);
// Add support for ' entity (missing in HTML_ENTITIES)
$trans_tbl += array(''' => "'");
// Replace entities by values
return strtr ($string, $trans_tbl);
}
} //End class FeedParser
?>