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.
/** * Fetch the number of followers from twitter api * * @author Peter Ivanov * @copyright http://www.ooyes.net * @version 0.2 * @link http://www.ooyes.net * @param string $username * @return string */ function twitter_followers_counter($username) { $cache_file = CACHEDIR . 'twitter_followers_counter_' . md5 ( $username ); if (is_file ( $cache_file ) == false) { $cache_file_time = strtotime ( '1984-01-11 07:15' ); } else { $cache_file_time = filemtime ( $cache_file ); } $now = strtotime ( date ( 'Y-m-d H:i:s' ) ); $api_call = $cache_file_time; $difference = $now - $api_call; $api_time_seconds = 1800; if ($difference >= $api_time_seconds) { $api_page = 'http://twitter.com/users/show/' . $username; $xml = file_get_contents ( $api_page ); $profile = new SimpleXMLElement ( $xml ); $count = $profile->followers_count; if (is_file ( $cache_file ) == true) { unlink ( $cache_file ); } touch ( $cache_file ); file_put_contents ( $cache_file, strval ( $count ) ); return strval ( $count ); } else { $count = file_get_contents ( $cache_file ); return strval ( $count ); } }
How to use it?
print twitter_followers_counter('massmediagroup') ?> followers
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?