Twitter Followers Image List


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

A simple script to show a followers list in Wordpress. (It can be used in other places also, you have to change the WP specific variables). The script needs to create a cache file to not overload Twitter API.


Copy this code and paste it in your HTML
  1. <?php
  2. //replace $options['twitter'] with the twitter Username you want to use.
  3. ?>
  4.  
  5. <?php
  6. $xmlfollowersUrl = 'http://api.twitter.com/1/statuses/followers/'.$options['twitter'].'.xml';
  7.  
  8. if (file_exists(WP_CONTENT_DIR.'/uploads/twitter_followers.xml')) {
  9. $data = unserialize(file_get_contents(WP_CONTENT_DIR.'/uploads/twitter_followers.xml'));
  10. if ($data['timestamp'] > time() - 120 * 60) {
  11. $twitter_followers = $data['twitter_result'];
  12. }
  13. }
  14.  
  15. if (!$twitter_followers) { // cache doesn't exist or is older than 10 mins
  16. $twitter_followers = file_get_contents($xmlfollowersUrl); // or whatever your API call is
  17.  
  18. $data = array ('twitter_followers' => $twitter_followers, 'timestamp' => time());
  19. file_put_contents(WP_CONTENT_DIR.'/uploads/twitter_followers.xml', serialize($data));
  20. }
  21.  
  22.  
  23. $dom = new DOMDocument;
  24. $s = simplexml_load_string($twitter_followers);
  25. $count = 0;
  26. echo '<ul class="seguidores">';
  27. foreach($s->user as $follower):
  28. if(++$count > 50){
  29. break;
  30. }
  31. echo '<li><a target="_blank" href="http://www.twitter.com/'.$follower->screen_name.'"><img width="32" height="32" src="'.$follower->profile_image_url.'" alt="'.$follower->name.'" title="'.$follower->name.': '.$follower->status->text.'"/></a></li>';
  32. endforeach;
  33. echo '</ul>';
  34. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.