Use PHP cURL to get your Google Feedburner subscriber count text, with result caching

Use PHP cURL to get your Google Feedburner subscriber count text, with result caching

The problem

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.

The 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, i.e.;

include('feedcount.php');

Notes:

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.

Tags

 

You might like

Comments


Nice Tut !


Are you sure it is still working ? When I echo $data, I get the message "Permission denied".


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;
******
"Warning: fopen(feedcount.txt): failed to open stream: Permission denied
in /home/djnightlife/public_html/index.php"
******
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.


I found the problem, you need to activate "Awareness API" in feedburner settings in order Google lets you remotely access the xml file.


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


Nice cool tips. keep them coming.


(optional, not publicly displayed) (optional)

Pingbacks

  1. Use PHP cURL to get your Google Feedburner subscriber count …

Copyleft 2002 - 2014 Matt Jones
Hand crafted with HTML5 & CSS3
↑ Back to top