*/ class Feed { /** * Constructs a new instance of Feed class. * * @param string $url * @return array */ public function __construct($url) { $file = file_get_contents($url); $xml = simplexml_load_string($file); $type = $this->getFeedType($xml); $data = array(); switch($type) { case 'rss': $data = $this->parseRSSFeed($xml); break; case 'atom': $data = $this->parseAtomFeed($xml); break; } return $data; } /** * parse rss feed * * @param object $xml * @return array */ 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; } /** * parse rss feed * * @param object $xml * @return array */ 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; } /** * get feed time * * @param string $time * @return integer */ private function getTime($time) { return date("d.m.Y H:i:s", $this->getTimeStamp($time)); } /** * get feed timestamp * * @param string $time * @return integer */ private function getTimeStamp($time) { return intval(strtotime($time)); } /** * get feed type * * @param object $xml * @return array */ public static function getFeedType($xml) { $type = ''; if (isset($xml->channel->item)) { $type = 'rss'; } else if (isset($xml->entry)) { $type = 'atom'; } return $type; } } ?>