Last active
August 29, 2015 14:11
-
-
Save YupItsZac/15a9206ea1017c677815 to your computer and use it in GitHub Desktop.
RSS Parser Class - Basic
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 feedParse { | |
private $feed; | |
private $truncate; | |
private $feedData; | |
public function __construct($feed, $truncate) { | |
$this->feed = $feed; | |
$this->truncate = $truncate; | |
} | |
public function setHeader() { | |
header('Content-Type: text/html; charset=utf-8'); | |
} | |
private function loadFeed() { | |
$this->feedData = simplexml_load_file($this->feed); | |
} | |
public function parseFeed() { | |
$this->loadFeed(); | |
$objArray = array(); | |
foreach($this->feedData->channel->item as $item) { | |
$itemArray = array(); | |
$itemArray['title'] = $item->title; | |
$itemArray['author'] = $item->author; | |
$links = $item->children('http://rssnamespace.org/feedburner/ext/1.0'); | |
$itemArray['link'] = $links->origLink; | |
$itemArray['raw_date'] = $item->pubDate; | |
$itemArray['pub_date'] = str_replace('+0000', '', $item->pubDate); | |
$desc = $item->description; | |
if($this->truncate > 0) { | |
if(strlen($desc) > $this->truncate) { | |
$desc = substr($desc, 0, $this->truncate).'...'; | |
} | |
} | |
$itemArray['description'] = $desc; | |
array_push($objArray, $itemArray); | |
} | |
return $objArray; | |
} | |
} | |
//Usage | |
require_once('feedParse.class.php'); | |
$feedUrl = '...'; | |
$truncate = 50; | |
$feed = new feedParse($feedUrl, $truncate); | |
$feed = $feed->parseFeed(); | |
//parseFeed returns array of post data from RSS feed | |
?> |
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
include 'feedParse.class.php'; | |
$parser = new feedParse(); | |
$opts = array(); | |
//Set the feed path as global var | |
$opts['feed'] = 'URL TO FEED'; | |
//set the truncate length as global var | |
$opts['truncate'] = 350; | |
//Pass options array to initialize the parser | |
$parser->init($opts); | |
//Load the RSS feed into the parser | |
$parser->loadFeed(); | |
//Parse the feed into a nested array of objects | |
$feeddata = $parser->parseFeed(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment