This article is a combination of two old articles. Neither of the old articles addressed the changes made by Google when they moved FeedBurner and one of them really didn't solve the problem of using PHP4 instead of PHP5. I've received enough email to tell me that I need to do this or continue to be asked for corrections.
Single or Multiple Feeds
I have three feeds because I'm using the WordPress DualFeeds plugin. The plugin supports a full feed, a partial feed and a comments feed. I'm really only concerned about the first two feeds because it's a true total of all my feed subscribers. Well, as true as the reporting by Google can be.
In April of 2008, I accidentally discovered "How To Get Your Feedburner Count As Plain Text" while looking for something else. It was a PHP5 script designed to pull a single feed count from FeedBurner (when it wasn't being run by Google). I changed it so it would retrieve multiple feed counts from FeedBurner, instead of just one. You can still use it if you only want one feed count, especially if you don't like the FeedBurner chicklet.
First Things First
You have to enable the "Awareness API" for each of your feeds, or this won't work at all. It's on the "Publicize" tab at FeedBurner. I'm furnishing two scripts: One for PHP5 and one for PHP4. There are no error checking routines, caching routines or anything else included with these simple scripts. You'll either get a real count or a big fat zero (during testing and while using it on my blog, I never received a zero). I have tested both versions to make sure they work as I say they do.
Step 1 – Fetch the Data
You can copy and paste either of these scripts since I use the Unfancy Quote plugin to remove the fancy quotes put in place by WordPress. For "$feeds", make sure you enter the part after "http://feeds.feedburner.com/" or "http://feeds2.feedburner.com/" (whichever you're using) for each of your feeds. You can reduce the array size to one or increase it beyond three by editing the first line of either script. The "$path" line must be the full path to where you're going to save the feed count text file. I recommend a directory just off the root of your site.
For PHP5:
<?php
$feeds = array('feed1', 'feed2', 'feed3');
$path = '/document_root/directory/filename.txt';
$fb = 0;
foreach ($feeds as $feed) {
$url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=$feed";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$fb += $xml->feed->entry['circulation'];
}
file_put_contents($path, $fb);
?>
For PHP4:
<?php
$feeds = array('feed1', 'feed2', 'feed3');
$path = '/document_root/directory/filename.txt';
$fb = 0;
foreach ($feeds as $feed) {
$url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=$feed";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$p = xml_parser_create();
xml_parse_into_struct($p, $data, $vals, $index);
xml_parser_free($p);
$fb += $vals[2]['attributes']['CIRCULATION'];
}
$f = fopen($path, 'w');
fwrite($f, $fb);
fclose($f);
?>
Step 2 – Setup a Cron Job
If you have access to cron on at your web host, have it run the script like this:
php /document root/directory/scriptname.php
Of course, you need to use the full path to the script, not just the path from your website root directory. If you don't have access to cron, then you can use the service at Uptime (for host uptime monitoring) to query the script — you just have to follow their instructions. Uptime queries the URL to the script, not the server path.
You have to decide on the best time to run the cron job or have Uptime do its thing. I don't know what time the FeedBurner data is updated (and I don't think it's at the same time every day). I would just set it for around midnight and be done with it.
Step 3 – Retrieve the Data
I use a standard text sidebar widget which allows PHP to be executed because I have the Exec-PHP WordPress plugin installed. If you have the plugin installed, you can include this code in a widget, a post or a page. If not, you can use it in any template file.
For PHP5 or PHP4:
<?php
echo file_get_contents('/document_root/directory/filename.txt');
?>
That's it! You can surround the result in any way you see fit.
The Wrap-Up
Does this seem like too much work? It may look like it, but it really isn't. If you want to do this and you're having problems, feel free to comment or use the contact page to email me.
You can always use a FeedBurner chicklet for each single feed, but if you want two or more feed counts combined, you won't be able to get anything directly from FeedBurner that'll do it. By the way, the email subscriptions are included in the feeds, so you don't need that data through this code or any chicklet.
Special Thanks
Thanks to Jakob of JD-Blog for bringing the new API code to my attention and to Steve Martile of Freedom Education for letting me know that my original PHP4 code didn't work right.
Similar Articles:
- Why aren't you offering RSS feeds by email?
- Splog Wars I – The War Begins
- How to Kill Website Spambots Dead
- Mint: My Favorite Analytics Program
- 3 Plugins to Promote your Facebook Page
This article is published as: Updated: FeedBurner Plain Text Count for Single or Multiple Feeds
The PHP5 scripting are just the same with PHP4. But the PHP5 scripting is much shorter.
I agree with Offshore. The PHP 5 coding is shorter but I should point out that PHP 4 still gets the job done. I guess it depends on one's skillset and the online tutorials that are available.
I actualy read your 2 articles before and I like the way you have combined them.
Oh dear…and I thought I could go a day without having to look at any code. Oh well.