Rss

Create a Custom RSS Feed Button With Your Readers Number

del.icio.us Design float Facebook Technorati StumbleUpon Additious Rss Feed


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 :

result.jpg

See the live demo

Step 1: Activate the FeedBurner Awareness API

feedburner.jpg

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".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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 rss 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

1
2
3
4
<a href="http://feeds.feedburner.com/" title="">
	<img src="imageRss.php" alt="My Rss Feed"/>
</a>
 

Here the result :

result.jpg

And an other example :

result2.jpg

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".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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.

See the live demo

Download the source files




del.icio.us Design float Facebook Technorati StumbleUpon Additious Rss Feed

5 Comments

Thomas :
There was a bug on the comment submission. I corrected it.

Max :
Nice Tut, Cool design too, faved!

Shreemani :
cool tutorial for rss feeds.

Tutorials Room :
Good Tutorial! It was chosen for the home page of http://www.tutorialsroom.com Waiting for your new tutorials :)

Thomas :
Thanks for your comments :)



Leave a Reply on Create a Custom RSS Feed Button With Your Readers Number