Twitter followers counter PHP script

 

Whats this?

We have created simple PHP Script to count your Twitter followers and cache the result. As you can see the script is very simple and it can be used in every application. It returns text value of the number of followers for given Twitter username.

 

  1.  
  2. /**
  3.  * Fetch the number of followers from twitter api
  4.  *
  5.  * @author Peter Ivanov
  6.  * @copyright http://www.ooyes.net
  7.  * @version 0.2
  8.  * @link http://www.ooyes.net
  9.  * @param string $username
  10.  * @return string
  11.  */
  12. function twitter_followers_counter($username) {
  13.  
  14. $cache_file = CACHEDIR . 'twitter_followers_counter_' . md5 ( $username );
  15.  
  16. if (is_file ( $cache_file ) == false) {
  17. $cache_file_time = strtotime ( '1984-01-11 07:15' );
  18. } else {
  19. $cache_file_time = filemtime ( $cache_file );
  20. }
  21.  
  22. $now = strtotime ( date ( 'Y-m-d H:i:s' ) );
  23. $api_call = $cache_file_time;
  24. $difference = $now - $api_call;
  25. $api_time_seconds = 1800;
  26.  
  27. if ($difference >= $api_time_seconds) {
  28. $api_page = 'http://twitter.com/users/show/' . $username;
  29. $xml = file_get_contents ( $api_page );
  30.  
  31. $profile = new SimpleXMLElement ( $xml );
  32. $count = $profile->followers_count;
  33. if (is_file ( $cache_file ) == true) {
  34. unlink ( $cache_file );
  35. }
  36. touch ( $cache_file );
  37. file_put_contents ( $cache_file, strval ( $count ) );
  38. return strval ( $count );
  39. } else {
  40. $count = file_get_contents ( $cache_file );
  41. return strval ( $count );
  42. }
  43. }

 

 

How to use it?

 

  1. print twitter_followers_counter('massmediagroup') ?> followers

 

Get it!

Click here to download

Add your comment 5 Comments

xccccccccccc

2011-06-22 02:33:21


cccccccccccccccccc

Klaudiho

2011-04-16 01:25:55


I would like to know * which * twitters friends are online in this moment ... It is possible??? my twitter is @klaudiho .

Evgrniy

2009-12-04 07:00:21


Many thanks for article that I searched

Peter

2009-07-20 06:20:10


yes, $api_time_seconds = 1800; //this is the refresh time in seconds

Scott Wiley

2009-07-18 01:10:06


Does this refresh, can it be set to refresh frequently?

Post your opinion


Post your message and we'll contact you immediately.
Thank you for your desire to work with us.

Please, fill out the required fields!

Close
OK