-
-
Save xeoncross/3280952 to your computer and use it in GitHub Desktop.
Parse rss or atom feeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function parseRss($feedUrl) { | |
$result = array(); | |
// "@" is for error handling | |
$feed = @simplexml_load_file($feedUrl); | |
//atom | |
if (isset($feed->entry)) { | |
$result['title'] = (string)$feed->title; | |
$result['link'] = (string)$feed->link->attributes()->href; | |
foreach ($feed->entry as $e){ | |
$title = (string)$e->title; | |
$link = (string)$e->link['href']; | |
if (!empty($e->issued)) { | |
$date = date('Y-m-d H:i:s', strtotime($e->issued)); | |
} else if ($e->updated) { | |
$date = date('Y-m-d H:i:s', strtotime($e->updated)); | |
} | |
$result['items'][] = array('title' => $title, 'link' => $link, 'date' => $date); | |
} | |
//rss1.0 | |
} elseif (isset($feed->item)) { | |
$result['title'] = (string)$feed->title; | |
$result['link'] = (string)$feed->link; | |
foreach ($feed->item as $i){ | |
$title = (string)$i->title; | |
$link = (string)$i->link; | |
$dc = $i->children('http://purl.org/dc/elements/1.1/'); | |
$date = date('Y-m-d H:i:s', strtotime($dc->date)); | |
$result['items'][] = array('title' => $title, 'link' => $link, 'date' => $date); | |
} | |
//rss2.0 | |
} elseif (isset($feed->channel->item)) { | |
$result['title'] = (string)$feed->channel->title; | |
$result['link'] = (string)$feed->channel->link; | |
foreach ($feed->channel->item as $i){ | |
$title = (string)$i->title; | |
$link = (string)$i->link; | |
$date = date('Y-m-d H:i:s', strtotime($i->pubDate)); | |
$result['items'][] = array('title' => $title, 'link' => $link, 'date' => $date); | |
} | |
} else { | |
return false; | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment