<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>AjaxMasters Blog &#187; counter</title> <atom:link href="http://blog.ajaxmasters.com/tag/counter/feed/" rel="self" type="application/rss+xml" /><link>http://blog.ajaxmasters.com</link> <description>Just another WordPress weblog</description> <lastBuildDate>Thu, 24 Jun 2010 11:55:25 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.2</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Twitter followers counter tutorial</title><link>http://blog.ajaxmasters.com/twitter-followers-counter-tutorial/</link> <comments>http://blog.ajaxmasters.com/twitter-followers-counter-tutorial/#comments</comments> <pubDate>Tue, 22 Sep 2009 18:56:38 +0000</pubDate> <dc:creator>Chocksy</dc:creator> <category><![CDATA[Tutorial]]></category> <category><![CDATA[counter]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[php]]></category> <category><![CDATA[twitter]]></category><guid
isPermaLink="false">http://blog.ajaxmasters.com/?p=7</guid> <description><![CDATA[<a
href="http://blog.ajaxmasters.com/twitter-followers-counter-tutorial/"><img
align="left" hspace="5" width="150" height="150" src="http://blog.ajaxmasters.com/wp-content/uploads/2009/09/twitter-poster-170x170.png" class="alignleft wp-post-image tfe" alt="Twitter followers tutorial" title="twitter-poster" /></a>You&#8217;re probably wondering why should i bother if we have TwitterCounter to show us the followers, well what if i don&#8217;t like the way it shows them what if i wanna do a counter with my own graphics , well i&#8217;m gonna show you how.
We&#8217;re gonna need the jpg to show the followers on and 2 classes to read the data.
Let&#8217;s get some twitter icons so that we can create [...]]]></description> <content:encoded><![CDATA[<div
id="attachment_9" class="wp-caption alignleft" style="width: 310px"><img
class="size-full wp-image-9" title="twitter-poster" src="http://blog.ajaxmasters.com/wp-content/uploads/2009/09/twitter-poster.png" alt="Twitter followers tutorial" width="300" height="300" /><p
class="wp-caption-text">Twitter followers tutorial</p></div><p>You&#8217;re probably wondering why should i bother if we have <a
href="http://twittercounter.com/" target="_blank">TwitterCounter</a> to show us the followers, well what if i don&#8217;t like the way it shows them what if i wanna do a counter with my own graphics , well i&#8217;m gonna show you how.</p><p>We&#8217;re gonna need the jpg to show the followers on and 2 classes to read the data.</p><p>Let&#8217;s get some twitter icons so that we can create a simple image in photoshop. With a simple google search we get on this article <a
href="http://www.hongkiat.com/blog/100-remarkably-beautiful-twitter-icons-and-buttons/" target="_blank">100+ Remarkably Beautiful Twitter Icons And Buttons</a> created by <a
href="http://www.hongkiat.com/" target="_blank">www.hongkiat.com</a>.</p><p>I did a simple image that you can use if you want it&#8217;s all added to the download package.</p><p>Now let&#8217;s create the php code that will display that followers number&#8230;</p><p>We&#8217;re gonna need 2 classes that will get our job done easily, a twitter class and a class that will transform the xml output into an array one.</p><div
id="attachment_12" class="wp-caption alignright" style="width: 180px"><img
class="size-full wp-image-12" title="follow chocksy" src="http://blog.ajaxmasters.com/wp-content/uploads/2009/09/follow-chocksy1.jpg" alt="My follow chocksy image" width="170" height="85" /><p
class="wp-caption-text">My follow chocksy image</p></div><p>Also you have to find out your id number that&#8217;s a little tricky and i don&#8217;t understand why twitter is not showing that so that you can find it. So here is how you can find your twitter id:</p><p>Go on your account page and on the right you have a button named RSS, now that button has an url like http://twitter.com/statuses/user_timeline/<strong>18107925</strong>.rss so the bolded number is your id that you&#8217;ll have to use in our php code.</p><pre class="brush: php;">
include('xml2array.php');
include('twitter.lib.php');
$user = 'yournickname';
$password = 'yourpassword';

$twitter = new Twitter($user, $password);

$friends = $twitter-&gt;showUser('xml','yourid_number');
$xml=new XML2Array();
$arr=$xml-&gt;parse($friends);
$followers=$arr['user']['followers_count']['value'].' &lt;span style='font-size:12px;'&gt;followers&lt;/span&gt;';
</pre><p>We will now show the image with the number in the right place. We&#8217;ll link the image to our account so that people can follow us and place the followers in the div we have between the a tags.</p><pre class="brush: xml;">
&lt;div&gt;
&lt;a href=&quot;http://www.twitter.com/yournickname&quot; target=&quot;_blank&quot; style=&quot;text-decoration:none;&quot;&gt;&lt;div class=&quot;twitter&quot; id=&quot;tell_followers&quot;&gt;&lt;?=$followers?&gt;&lt;/div&gt;&lt;/a&gt;
&lt;/div&gt;
</pre><p>Here we have the css that takes care of the followers number display.</p><pre class="brush: css;">
.twitter{
color:#FFFFFF;
font-family:Tahoma;
font-size:24px;
line-height:14pt;
background:url(follow_me.png) no-repeat;
height:85px;
width:170px;
padding:35px 0 0 57px;
text-decoration:none;
cursor:pointer;
}
</pre><p><strong
class="in_title">UPDATE (for blogers with a lot of visitors)</strong></p><p>Since we use this script to make requests to the twitter API we have to be careful because the API does not allow us to make more than 100 requests per hour so if you have more than 100 visitors per hour you have to make this changes:</p><p>First of all let&#8217;s create a MYSQL table here is the SQL code for the table, a very simple one :</p><pre class="brush: sql;">
 CREATE TABLE `ajaxmast_blog`.`twitter_counter` (
`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`followers` BIGINT NOT NULL ,
`last_set` DATETIME NOT NULL
) ENGINE = MYISAM
</pre><p>Ok after we&#8217;ve done that let&#8217;s connect to the db.</p><pre class="brush: php;">
//DATA FOR SQL
define(USERNAME,'db_username');
define(PASSWORD,'db_password');
define(DATABASE,'db_name');
//END CONF

//connect to db
@mysql_connect('localhost',USERNAME,PASSWORD);
@mysql_select_db(DATABASE);
$db_query=mysql_query('SELECT * FROM twitter_counter LIMIT 1');
</pre><p>We&#8217;ll put the request to the twitter API in a function so we don&#8217;t have a very heavy code:</p><pre class="brush: php;">
//function to get the followers nore easily
function get_followers($user,$password){
$twitter = new Twitter($user, $password);

$friends = $twitter-&gt;showUser('xml','18107925');
$xml=new XML2Array();
$arr=$xml-&gt;parse($friends);
$nr_followers=$arr['user']['followers_count']['value'];

return $nr_followers;
}
</pre><p>After that we create a difference function so we can calculate the minutes that have passed since we checked the twitter API:</p><pre class="brush: php;">
function dateDiff($date, $date2)
{
    if($date2 &gt; $date) {
        $tmp = $date2 - $date;
    }

    else {
        $tmp = $date - $date2;
    }

    $seconds = $tmp;

// Relative ////////
    $minutes = floor($tmp/60);
    $tmp -= $minutes * 60;

    // Total ///////////
    $tminutes = floor($seconds/60);

return $tminutes;
}
</pre><p>And for the final touch we check to see if the time has passed so we can call again the twitter API or if not we should display the followers from our database:</p><pre class="brush: php;">
$curent_time=date('Y-m-d H:i:s');
$info=mysql_fetch_array($db_query);
$last_set=$info['last_set'];

$dif=dateDiff(strtotime($curent_time),strtotime($last_set));

//check for last request
if(mysql_num_rows($db_query)==0){
$nr_followers=get_followers($user,$password);

mysql_query(&amp;quot;INSERT INTO twitter_counter (followers,last_set) VALUES('&quot;.$nr_followers.&quot;',NOW())&quot;);
}else if($dif&lt;=10){
$nr_followers=get_followers($user,$password);

mysql_query(&quot;UPDATE twitter_counter SET followers='&quot;.$nr_followers.&quot;',last_set=NOW()&quot;);
}else
$nr_followers=$info['followers'];

$followers=$nr_followers.' &lt;span style=&quot;font-size:12px;&quot;&gt; followers&lt;/span&gt;';
</pre><p>And that&#8217;s it the rest is the same. I will place this version in the .rar file too so you can use it as you may like tell me if you want any changes done to it or anything.  I would like to thank smashing magazine for the feature  here is the article: <a
href="http://www.smashingmagazine.com/2009/03/02/twitter-web-designer-and-developer-toolbox-api-and-tutorials/" target="_blank">ARTICLE</a>.</p><p><a
href="http://blog.ajaxmasters.com/wp-content/uploads/2009/09/twitter_followers1.rar">Download SOURCES</a></p><p>You have in the download package the PSD file and the 2 classes that will help you work this out, also there is a test file too. Hope you find this useful.<br
/> Thanks for reading.</p> ]]></content:encoded> <wfw:commentRss>http://blog.ajaxmasters.com/twitter-followers-counter-tutorial/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 6/27 queries in 0.025 seconds using disk

Served from: blog.ajaxmasters.com @ 2010-07-31 13:30:11 -->