Created
June 21, 2011 04:58
-
-
Save alchemycs/1037274 to your computer and use it in GitHub Desktop.
Sample blog 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
<?php | |
/* | |
* This class pulls and caches a blog feed from a remote site and displays the content locally. | |
* | |
*/ | |
/** | |
* @author Michael McHugh | |
* @version 1.0 2009-10-14 - Initial code | |
* @version 2.0 2011-06-22 - Updated to allow 0-indexed article offset and standalone display | |
* | |
* xmlns:content="http://purl.org/rss/1.0/modules/content/" | |
* | |
*/ | |
if (!class_exists('BlogFeed')) { | |
class BlogFeed { | |
protected $feedXML = null; | |
protected $liveSource = null; | |
protected $ttl = 600; //10 minutes = 600 seconds | |
public function __construct($feedPath, $liveSource) { | |
if (!file_exists($feedPath)) { | |
$this->downloadFeed($feedPath, $liveSource); | |
} else { | |
if (time()-filemtime($feedPath)>$this->ttl) { | |
$this->downloadFeed($feedPath, $liveSource); | |
} | |
} | |
if (!is_file($feedPath) || !is_readable($feedPath)) { | |
throw new Exception('Feed path specified is not a file or is not readable.'); | |
} | |
$this->feedXML = @simplexml_load_file($feedPath); | |
if ($this->feedXML===false) { | |
throw new Exception('Unable to parse file'); | |
} | |
} | |
public function downloadFeed($feedPath, $liveSource) { | |
return @copy($liveSource, $feedPath); | |
} | |
public function getTitle($index=0) { | |
return $this->feedXML->channel->item[$index]->title; | |
} | |
public function getDescription($index=0) { | |
return $this->feedXML->channel->item[$index]->description; | |
} | |
public function getEncoded($index=0) { | |
$children = $this->feedXML->channel->item[$index]->children('http://purl.org/rss/1.0/modules/content/'); | |
$encoded = $children->encoded; | |
return $encoded; | |
} | |
public function getLink($index=0) { | |
return $this->feedXML->channel->item[$index]->link; | |
} | |
public function count() { | |
return count($this->feedXML->channel->item); | |
} | |
public function showArticle($index=0) { | |
?> | |
<h2><?=$this->getTitle($index);?></h2> | |
<p> | |
<?=$this->getDescription($index);?> | |
<?=$this->getEncoded($index);?> | |
<a href="<?=$this->getLink($index);?>">more »</a> | |
</p> | |
<? | |
} | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf8"/> | |
<title>CompliSpace Blog Feed</title> | |
</head> | |
<body> | |
<h1>CompliSpace Blog Feed</h1> | |
<?php | |
try { | |
$feed = new BlogFeed('/tmp/CompliSpaceBlog.xml', 'http://complispace.wordpress.com/feed/'); | |
for ($index = 0; $index < $feed->count(); $index++) { | |
$feed->showArticle($index); | |
} | |
} catch (Exception $e) { | |
printf("Feed not available at this time.\n<!-- Blog Feed:\n %s\n -->\n", $e->getMessage()); | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment