• home
  • forum
  • my
  • kt
  • download
  • Create a Custom RSS Feed Button With Your Readers Number

    Author: 2009-03-09 09:28:44 From:

    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.

    Download the source files

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Ad Management (6)
      Calendars (3)
      Chat Systems (8)
      Content Management (41)
      Cookies and Sessions (12)
      Counters (15)
      Database Related (34)
      Date and Time (15)
      Development (22)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (14)
      Error Handling (8)
      File Manipulation (36)
      Flash and PHP (6)
      Form Processing (22)
      Guestbooks (12)
      Image Manipulation (25)
      Installing PHP (7)
      Introduction to PHP (23)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (53)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot