Created
January 14, 2014 11:50
-
-
Save JayKid/8417102 to your computer and use it in GitHub Desktop.
A simple feed parser (tested against WP Feed) in PHP
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 | |
class RSSFeedParser { | |
private $feed_url; | |
public function __construct($feed_url) | |
{ | |
$this->feed_url = $feed_url; | |
} | |
public function getEntries($limit = NULL) | |
{ | |
$rss = new DOMDocument(); | |
$rss->load($this->feed_url); | |
$feed = array(); | |
$entries = $rss->getElementsByTagName('item'); | |
$entries_number = $entries->length; | |
if (is_null($limit) || $limit > $entries_number) | |
$limit = $entries_number; | |
foreach ($entries as $node) | |
{ | |
$item = array ( | |
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, | |
'content' => $node->getElementsByTagName('description')->item(0)->nodeValue, | |
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, | |
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, | |
); | |
array_push($feed, $item); | |
--$limit; | |
if ($limit === 0) | |
break; | |
} | |
return $feed; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment