-
Notifications
You must be signed in to change notification settings - Fork 4
FeedBurner Library
Category:Contributions::Libraries::Networking
This library provides a simple way to get the number of subscribers of a FeedBurner account through the FeedBruner Awareness API. The library provides also a file based caching system since an extra HTTP request is needed to get the FeedBurner account data.
The library is also available as a PHP5 class for non CodeIgniter applications.
Licensed under GNU Lesser General Public License.
Please note that this class requires PHP 5.1.0 with SimpleXML and libxml and the cURL extension.
For more details, support, feature request or packaged downloads please see the FeedBurner Library for PHP and CodeIgniter dedicated page on my blog.
FAQ
-
If a path related error occurs when enabling cache, please ensure to add the final trailing slash to $path (e.g. ./folder/sub-folder/final-folder/).
-
Some users on WAMP could experiment a problem with cURL in local. First try your application online to check if your code works on a production server.
How to use :
<?php
class Test extends Controller {
function Test()
{
parent::Controller();
}
function index()
{
$config = array();
$config['username'] = 'your_feed_url_username';
$config['cache'] = TRUE;
$config['path'] = './tmp/';
$config['filename'] = 'feedburner.xml';
$config['ttl'] = 60;
$this->load->library('feedburner', $config);
echo $this->feedburner->getCirculation();
}
}
/* End of file test.php */
/* Location: ./system/application/controllers/test.php */
Library's code (system/application/libraries/Feedburner.php):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Please visit http://www.kromack.com/feedburner-library-for-php-and-codeigniter for full details and support about this library.
*
* Please specify your FeedBurner username, for example http://feeds2.feedburner.com/my_blog, your username is "my_blog".
*
* This class include a file based caching system since an extra HTTP request is required to retrieve FeedBurner account information.
*
* Using cache is more than 5 time faster.
*
* In order to activate the caching system, please set $cache to TRUE and specify the $path (and optionally the $filename to use)
* where the caching file should be written.
*
* Please ensure to add the final trailing slash to $path (e.g. ./folder/sub-folder/final-folder/).
*
* You can also specify the time to live (in minutes) of the stored xml cached file, for example, set to 60 to keep alive the stored response
* for one hour.
*
* @author Samuel Sanchez <[email protected]>
* @link http://www.kromack.com/feedburner-library-for-php-and-codeigniter
* @license http://www.gnu.org/copyleft/lesser.html
* @copyright Copyright (c) 2009, Samuel Sanchez
* @version 1.0
* @param string $username
* @param bool $cache
* @param string $path
* @param string $filename
* @param int $ttl
*/
class Feedburner {
public $username = '';
public $cache = FALSE;
public $path = './';
public $filename = 'feedburner.xml';
public $ttl = 60;
protected $response = '';
protected $xml = '';
function Feedburner($props = array()) {
if (count($props) > 0)
{
$this->initialize($props);
}
log_message('debug', "Feedburner Class Initialized");
}
/**
* Initialize preferences
*
* @access public
* @param array
* @return void
* @package CodeIgniter
*/
function initialize($config = array())
{
$defaults = array(
'username' => '',
'cache' => FALSE,
'path' => './',
'filename' => 'feedburner.xml',
'ttl' => 60,
'response' => '',
'xml' => ''
);
foreach ($defaults as $key => $val)
{
if (isset($config[$key]))
{
$method = 'set_'.$key;
if (method_exists($this, $method))
{
$this->$method($config[$key]);
}
else
{
$this->$key = $config[$key];
}
}
else
{
$this->$key = $val;
}
}
}
function getData() {
$url = 'https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' . $this->username;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$this->response = curl_exec($ch);
curl_close($ch);
}
function setData() {
if($this->cache === TRUE) {
$this->manageCache();
} else {
if(empty($this->xml)) {
$this->getData();
$this->xml = new SimpleXMLElement($this->response);
}
}
}
function manageCache() {
if(!is_dir($this->path)) {
die('The path you specified to the FeedBurner library is not valid, please check if the directory exists before enable caching.');
}
if(!is_writable($this->path)) {
die('The path you specified to the FeedBurner library is not writable, please check its permissions or CHMOD 777 it.');
}
if(file_exists($this->path . $this->filename)) {
if(strlen(file_get_contents($this->path . $this->filename)) > 0) {
//The file must content valid XML datas but we have to prevent errors reporting
if($xml = simplexml_load_file($this->path . $this->filename,null, LIBXML_NOERROR)) {
$this->xml = $xml;
$ttl = $this->xml->data->attributes();
$ttl = $ttl[0];
$interval = (time() - $ttl) / 60;
//Refreshing the XML stored file
if($interval > $this->ttl) {
$this->setCache();
}
} else {
$this->setCache();
}
} else {
$this->setCache();
}
} else {
$this->setCache();
}
}
function setCache() {
$this->getData();
$this->xml = new SimpleXMLElement($this->response);
//We adding a timestamp to the response in order to calculate the time to live of the cached response
$data = $this->xml->addChild('data');
$data->addAttribute('timestamp', time());
$handle = fopen($this->path . $this->filename, 'w+');
fwrite($handle, $this->xml->asXML());
}
function getCirculation() {
$this->setData();
return $this->xml->feed->entry['circulation'];
}
}
?>