From: Stricted Date: Thu, 7 Nov 2013 02:41:13 +0000 (+0100) Subject: add Feed reader class X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=27ba32a1f33a1d17da25f05b4c4abdd28560f641;p=Snippets.git add Feed reader class --- diff --git a/Feed.class.php b/Feed.class.php new file mode 100644 index 0000000..7f5f1e6 --- /dev/null +++ b/Feed.class.php @@ -0,0 +1,75 @@ +. + */ +class FeedClass { + public function __construct($url) { + $file = file_get_contents($url); + $xml = simplexml_load_string($file); + $type = $this->getFeedType($xml); + $data = null; + switch($type) { + case 'rss': + $data = $this->parseRSSFeed($xml); + break; + case 'atom': + $data = $this->parseAtomFeed($xml); + break; + default: + return null; + break; + } + return $data; + } + + private function parseRSSFeed($xml) { + foreach ($xml->channel->item as $item) { + $item->pubDate = $this->getTime($item->pubDate); + $item->description = html_entity_decode($item->description); + $data[] = $item; + } + return $data; + } + + private function parseAtomFeed($xml) { + $entries = $xml->children()->entry; + foreach ($entries as $entry) { + $entry->children()->author = $entry->children()->author->name; + $entry->children()->link = $entry->link->attributes()->href; + $entry->children()->updated = $this->getTimeStamp($entry->children()->updated); + $data[] = $entry->children(); + } + return $data; + } + + private function getTime($time) { + return date("d.m.Y H:i:s",intval(strtotime($time)));; + } + + private function getTimeStamp($time) { + return intval(strtotime($time));; + } + + public static function getFeedType($xml) { + $type = false; + if (isset($xml->channel->item)) $type = 'rss'; + if (isset($xml->entry)) $type = 'atom'; + + return $type; + } +} +?> \ No newline at end of file