Create a Custom RSS Feed Button With Your Readers Number
Today, most of websites use the RSS Feed with Feedburner to analyse and to count its readers. In this tutorial, I'll explain how create a custom RSS Feed button with the readers number. You'll use PHP with GD and XML.
The result :
Step 1: Activate the FeedBurner Awareness API
You go in your FeedBurner account, select your feed and in Publicize, you activate the awareness API service. Now you can access to your FeedBurner Data.
Step 2: Get the data on your website
We create a new PHP file named : "feedburner.inc.php".
function feedCount($nameFeed){
// We get the data with Curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.feedburner.com/awareness/1.0/GetFeedData?uri='.$nameFeed);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Récupération de l'URL et passage au navigateur
$file = curl_exec($ch);
$content .= $file;
$parser = xml_parser_create();
xml_parse_into_struct($parser,$content,$vals,$index);xml_parser_free($parser);
if ($vals[0]['attributes']['STAT']=="ok") {
return $vals[2]['attributes']['CIRCULATION'];
}
else {
if ($vals[0]['attributes']['STAT']=="fail") {
return '0';
}
}
return '0';
}
?>
In this file we create a function (feedCount) which return the
number of RSS Readers. It have one parameter $nameFeed which is the
name of your Feed.
For Example, if your Feedburner address is http://feeds.feedburner.com/Cssleak then "Cssleak" is the nameFeed.
In feedCount, we use CURL to get the XML data from FeedBurner with a timeout of 10 seconds. After we use XML_PARSER to parse and to put the data in an array. We could also use DOM or SimpleXML (with PHP5)
If we have an error the function return 0.
Step 3: Create an image with PHP and the GD librairie
We create a new PHP file named : "imageRss.php".
require_once("feedburner.inc.php");
header("Content-type: image/png");
$im = imagecreatetruecolor(250,40);
//We load the RSS icon
$rss = imagecreatefromjpeg("rss.jpg");
$white =imagecolorallocate($im,255,255,255);
$black =imagecolorallocate($im,0,0,0);
imagefilledrectangle($im, 0, 0, 250, 40, $white);
imagecopy ( $im ,$rss ,4,4,0,0, 32, 32);
$numReader = feedCount('Cssleak');
$text=' Readers';
$font = 'SketchRockwell.ttf';
// Ajout du texte
imagettftext($im, 22, 0, 45, 32, $black, $font, $numReader.$text);
imagepng($im);
imagedestroy($im);
?>
So with this file we create a png image. We send the header to specify the nature of the file when it'll be executed.
After, we create an image with our icon and a white background. I put the text (in black) on it.
For this example, I use the feedburner information of http://www.blog.spoongraphics.co.uk.
We choose a custom font for the text (only ttf True Type File will work) : SketchRockwell.ttf downladed on Dafont here.
You need to place the font file in the same directory than imageRss.php.
Using imagepng() results in clearer text compared with imagejpeg().
To display the image, just include the "imageRss.php" file in the img HTML tag
Here the result :
And an other example :
Step 4: Optimize
One of the possible optimization of this script is to save the image on the disk (in cache) and to update it with a cron task once a day.
Our new "imageRss.php".
require_once("feedburner.inc.php");
header("Content-type: image/png");
$im = imagecreatetruecolor(250,40);
//We load the RSS icon
$rss = imagecreatefromjpeg("rss.jpg");
$white =imagecolorallocate($im,255,255,255);
$black =imagecolorallocate($im,0,0,0);
imagefilledrectangle($im, 0, 0, 250, 40, $white);
imagecopy ( $im ,$rss ,4,4,0,0, 32, 32);
$numReader = feedCount('Cssleak');
$text=' Readers';
$font = 'SketchRockwell.ttf';
// Ajout du texte
imagettftext($im, 22, 0, 45, 32, $black, $font, $numReader.$text);
imagepng($im, "imageRss.png");
imagedestroy($im);
?>
We just save the image as imageRss.png. You just need a cron task to execute imageRss.php once a day.
Continue Reading