Created
October 4, 2011 12:42
-
-
Save ebababi/1261543 to your computer and use it in GitHub Desktop.
Cache FeedBurner circulation (subscribers) results in PHP
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 | |
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); | |
} | |
?> |
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
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/