Skip to content

Instantly share code, notes, and snippets.

@YupItsZac
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YupItsZac/15a9206ea1017c677815 to your computer and use it in GitHub Desktop.
Save YupItsZac/15a9206ea1017c677815 to your computer and use it in GitHub Desktop.
RSS Parser Class - Basic
<?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
?>
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