add some changes
[Snippets.git] / Feed.class.php
CommitLineData
27ba32a1 1<?php
a4685b95 2/**
5f6347bf
S
3* @author Jan Altensen (Stricted)
4* @copyright 2013-2014 Jan Altensen (Stricted)
5* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
6*/
701c06d4
S
7class Feed {
8 /**
9 * Constructs a new instance of Feed class.
10 *
11 * @param string $url
12 * @return array
13 */
27ba32a1 14 public function __construct($url) {
5f6347bf
S
15 $file = file_get_contents($url);
16 $xml = simplexml_load_string($file);
17 $type = $this->getFeedType($xml);
701c06d4
S
18 $data = array();
19
5f6347bf
S
20 switch($type) {
21 case 'rss':
22 $data = $this->parseRSSFeed($xml);
23 break;
701c06d4 24
5f6347bf
S
25 case 'atom':
26 $data = $this->parseAtomFeed($xml);
27 break;
5f6347bf 28 }
701c06d4 29
5f6347bf
S
30 return $data;
31 }
701c06d4
S
32
33 /**
34 * parse rss feed
35 *
36 * @param object $xml
37 * @return array
38 */
5f6347bf 39 private function parseRSSFeed($xml) {
27ba32a1
S
40 foreach ($xml->channel->item as $item) {
41 $item->pubDate = $this->getTime($item->pubDate);
42 $item->description = html_entity_decode($item->description);
43 $data[] = $item;
44 }
45 return $data;
5f6347bf 46 }
701c06d4
S
47
48 /**
49 * parse rss feed
50 *
51 * @param object $xml
52 * @return array
53 */
5f6347bf 54 private function parseAtomFeed($xml) {
27ba32a1
S
55 $entries = $xml->children()->entry;
56 foreach ($entries as $entry) {
57 $entry->children()->author = $entry->children()->author->name;
58 $entry->children()->link = $entry->link->attributes()->href;
59 $entry->children()->updated = $this->getTimeStamp($entry->children()->updated);
60 $data[] = $entry->children();
61 }
62 return $data;
5f6347bf 63 }
701c06d4
S
64
65 /**
66 * get feed time
67 *
68 * @param string $time
69 * @return integer
70 */
5f6347bf 71 private function getTime($time) {
701c06d4 72 return date("d.m.Y H:i:s", $this->getTimeStamp($time));
27ba32a1 73 }
701c06d4
S
74
75 /**
76 * get feed timestamp
77 *
78 * @param string $time
79 * @return integer
80 */
5f6347bf
S
81 private function getTimeStamp($time) {
82 return intval(strtotime($time));
27ba32a1 83 }
701c06d4
S
84
85 /**
86 * get feed type
87 *
88 * @param object $xml
89 * @return array
90 */
5f6347bf
S
91 public static function getFeedType($xml) {
92 $type = '';
93 if (isset($xml->channel->item)) {
94 $type = 'rss';
95 }
96 else if (isset($xml->entry)) {
97 $type = 'atom';
98 }
27ba32a1 99
5f6347bf
S
100 return $type;
101 }
27ba32a1
S
102}
103?>