Fetch the number of followers from twitter api


/ Published in: PHP
Save to your folder(s)



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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.