Skip to content

Instantly share code, notes, and snippets.

@ebababi
Created October 4, 2011 12:42
Show Gist options
  • Save ebababi/1261543 to your computer and use it in GitHub Desktop.
Save ebababi/1261543 to your computer and use it in GitHub Desktop.
Cache FeedBurner circulation (subscribers) results in PHP
<?php
function get_feedburner_circulation($feedburner_id, $ttl = 86400) {
$cache = dirname(__FILE__) . '/cache/feedburner.txt';
// Check cache validity
if ( !file_exists($cache) || ( filemtime($cache) + $ttl - time() < 0 ) ) {
// try to get the feed data
$curl = curl_init('https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' . $feedburner_id);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
// unless valid query web service
if ( $xml = @simplexml_load_string($result) ) {
$circulation = $xml->feed->entry['circulation'];
// and unless zero, cache result
if ( $circulation > 0 ) file_put_contents($cache, "$circulation");
}
}
// return cached result
return file_get_contents($cache);
}
?>
@tcope25
Copy link

tcope25 commented Dec 21, 2011

Appreciate this code. This solves the 0 problem that everyone experiences. You should also build in comma delimiters like the prettynumbers() script - http://tympanus.net/codrops/2010/01/03/prettynumber-a-jquery-plugin-for-formatting-numbers/

@ebababi
Copy link
Author

ebababi commented Dec 22, 2011

Thank you for your comment, treycopeland. I believe it is better for the function to return unformatted output and the formatting to happen afterwards:

<?php echo number_format( get_feedburner_circulation($feedburner_id) ); ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment