Use PHP cURL to get your Google Feedburner subscriber count text, with result caching
filed under: Web Development / PHP Programming
Recently I was looking for a simple, clean way to pull my total feed subscriber count from Google's FeedBurner, so I could style it as I liked. Something like these (and no, I don't have 323 readers, that's just for styling purposes);
- 323 subscribers
- "323 subscribers"
- 323 RSS readers
As always, I searched for an existing solution first, and I arrived at Hongkiat's little code snippet, very useful, uses cURL, almost perfect, except that this code will run on every single page view, that's way too much overhead. Though I can't find any documentation anywhere, I doubt Google's Feedburner updates subscriber counts more than a few times a day, if that.
The solution? Simple, we'll just cache the result to a plain text file, and only use the cURL functions after a period of time. Anyways, here is the modified code, simply edit the highlighted bits to get it the way you want;
<?php //path to file that will store the subscriber count $feed_file = '/home/yoursite/feedcounter.txt'; // update after how many minutes, 15 minutes, 120 minutes, etc $feed_timer = '240'; if (!file_exists($feed_file)) { $time = time() - ($feed_timer * 60) - 60; touch($feed_file, $time); // create the file and set it past the timer so it will update } if (filemtime($feed_file) + ($feed_timer * 60) > time()) { //check if it's old $feedcount = file_get_contents($feed_file); echo $feedcount; } else { //get cool feedburner count $whaturl="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=feedburner-id"; //Initialize the Curl session $ch = curl_init(); //Set curl to return the data instead of printing it to the browser. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set the URL curl_setopt($ch, CURLOPT_URL, $whaturl); //Execute the fetch $data = curl_exec($ch); //Close the connection curl_close($ch); $xml = new SimpleXMLElement($data); $fb = $xml->feed->entry['circulation']; //end get cool feedburner count //echo the subscriber count echo $fb; //save the subscriber count to cache fwrite(fopen($feed_file, "w"), $fb); } ?>
You can paste that code directly inside existing code, or simply save it as a separate file, something like feedcount.php, then include it wherever you'd like the count to appear;
include('feedcount.php');
As DJ Nightlife noted in the comments, if you're getting "permission denied" errors, make sure you've activated "Awareness API" in your feedburner account.
That's it.
6 comments
Add a new comment »Categories
Recent Entries
Recent Comments
- Dede (I checked it today in a shop. GT2 had some troubles with six ...)
- Matt (Bintang, You need to re-direct the url, try ...)
- jesth (Ohh.. why didn't I think of that, thanks alot.)
- Matt (Dede, I don't have Gran Turismo 2, any of the 2nd+ generation ...)
- Matt (Jesth, Just change the if condition, instead of looking for ...)
- jesth (Hi (again) Was wondering, is it possible to make it ...)
- Bintang Sembilan (Matt, thanks for your modd. I have apply it to my ...)
- Dede (Hello there. Can you check something for me? I want to buy ...)
- Matt (I think it's a driver issue Terrence, or it was a driver issue. ...)
Popular Entries
- Light-weight related articles mod for sNews 1.7 (4.5/5)
- Image / math hybrid captcha version 2, vastly improved (4.42/5)
- 1024x600 netbook wallpapers of Evangeline Lilly (4.4/5)
- Compact archives for sNews 1.7 (4.4/5)
- sNews Ajax Polls mod now available (4.38/5)
- Pretty date and comments bars in sNews CMS (4.35/5)
- Page caching mod for sNews 1.7 (4.33/5)
- Gravatar mod for sNews 1.7 (4.29/5)
- An improved tag cloud for sNews 1.7 (4.29/5)
Dec 26th, 2009 at 1:53 pm
Nice Tut !
Jan 7th, 2010 at 12:27 am
Are you sure it is still working ? When I echo $data, I get the message "Permission denied".
Jan 7th, 2010 at 8:00 am
Hi DJ,
Sounds like the webserver doesn't have permission to write to the file maybe?
What is the file giving the error? I mean, is it saying something like;
or something along those lines?
If that's the case, try setting the permissions on feedcount.txt to 777.
If you're using an FTP client, right click feedcount.txt, select permissions and click all the boxes.
If that doesn't fix it, let me know and I'll see what I can do.
Jan 7th, 2010 at 2:55 pm
I found the problem, you need to activate "Awareness API" in feedburner settings in order Google lets you remotely access the xml file.
Jan 7th, 2010 at 2:59 pm
Ahh, yes, that's true, maybe I should note that in the article. Thanks for getting back with the solution, hope it works well for you now.
--Matt
Jun 13th, 2010 at 11:04 am
Nice cool tips. keep them coming.