Get Your Latest Tweets The Smart Way

Posted on 22. Nov, 2010 by in Social Media

If you’ve ever used the Twitter API to get the number of your Twitter followers, your latest status updates, or something similar using the Twitter API you may have come across a little problem that has been bugging me for a while. Twitter has a limit on the number of API requests per hour. So, If you have a high traffic blog or other website you may find that your scripts tapping into their API don’t always return with info from Twitter.

How to solve this? A solution I came up with recently involves storing the data from the Twitter API in a text file, then simply calling the file to get the data. This saves API calls and keeps you from timing out, unless of course you’re calling it somewhere else on your site.

Let’s say you want to get the number of followers you have on Twitter and display it on your WordPress blog. Copy the following code and paste it into your functions.php file.

<?php
  1.  
  2. // get and store # of Twitter followers
  3. function get_followers($twitterid){
  4.  
  5.  //get number of Twitter followers from Twitter website
  6.  //http://www.catswhocode.com/blog/php-<a href="http://wpshout.com/10-code-snippets-for-faster-wordpress-coding/"target="_blank"title="WordPress Code Snippets" >snippet</a>s-to-interact-with-twitter
  7.  $xml=file_get_contents('http://twitter.com/users/show.xml?screen_name='.$twitterid);
  8.  if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
  9.   $tw['count'] = $match[1];
  10.  }
  11.  $strfollowers = $tw['count']; //get current number of followers from Twitter
  12.  
  13.  //check the text file for last # of followers and when
  14.  $myFile = "followers.txt";     //which text file?
  15.  $fh = fopen($myFile, 'r') or die("error");   //open file or error msg
  16.  $today = date("Y/m/d");      //find today's date
  17.  $currecord = fgets($fh);     //get the first line of text file
  18.  $arrayCurRecord = explode("::", $currecord);   //split first line into separate records
  19.  $lastdate = $arrayCurRecord[0];     //retrieve date
  20.  $lastfollowers = $arrayCurRecord[1];    //retrieve number of files
  21.  fclose($fh);       //close connection to text file
  22.  
  23.  //decide wether to update text file or use current records
  24.  if ($lastdate < $today) {
  25.   //if text file is old we need to update it
  26.   $stringData = $today . "::" . $strfollowers . "\n"; //next record in text file is current date + number of followers
  27.   $fh2 = fopen($myFile, 'w') or die("error");  //open file or error msg
  28.   fwrite($fh2, $stringData);    //write to text file
  29.   fclose($fh2);      //close connection to text file
  30.   return $strfollowers;
  31.  } else {
  32.   //if text file is from today then it's good and we use it
  33.   return $lastfollowers;
  34.  }
  35.  
  36. }
  37.  
  38. ?>

Then copy and paste this line into wherever you want your number of followers to show up. Don’t forget to take out my Twitter handle and put in yours instead.

  1. <?php echo get_followers('angelagiese'); ?>

The function above stores the total of Twitter followers once per day. Whenever your first visitor sees the webpage the code is on in their browser the code finds the text file, reads the date, and if it’s a date before today it asks Twitter for the current number of followers. It then stores today’s date and the number in the text file. Each time any page calling the function is loaded after that for the current day it references the text file.

You’ll want to make sure the text file has permission to write on your server by opening your FTP client, right clicking on the file, then chosing the appropriate permissions. I’m no security expert, so you’ll have to decide for yourself whether you want to allow this solution.

Angela Giese is a web developer from the Seattle area who loves making super-useful websites with PHP, jQuery, and content management systems such as WordPress.

This is a guest post! After my cry for help a couple of weeks ago Angela very kindly offered to submit a guest post. If you would like to write a guest post for WPShout then please leave a comment below and I’ll drop you an email. It’d be absolutely wonderful to have more posts on the site to keep the content coming!

Follow on Twitter! Subscribe!

WPShout is hosted by the fine folks at WPWebHost.

You can get exactly the same hosting as WPShout has for $7.95/month with WPWebHost's Freedom Plan.

Plus get 30% off the Freedom Plan with the code WPSHOUT.

Visit WPWebHost

Alex's Gravatar

Alex Denning is the founder of WPShout. A WordPress developer from London, Alex is a keen musician and freelance writer and developer.

You can find Alex on Twitter.

2 Responses to “Get Your Latest Tweets The Smart Way”

  1. Gilbert

    24. Nov, 2010

    I had the same problem and came up with a similar solution to this one. I created a PHP class called simpleCache which caches 3rd party api calls. You should check it out: https://github.com/gilbitron/PHP-SimpleCache

    Reply to this comment
  2. rfmeier

    24. Nov, 2010

    Nice. If using WordPress, wouldn’t the Transients API work well for this too?

    Reply to this comment

Leave a Reply

Please use your real name when commenting. Wrap code in <code> tags and make sure HTML is encoded. You can use regular HTML like <a href="... etc.

Get yours questions answered quicker

If you're using this post for paid work and have a question of any complexity then please ask at WPQuestions where you'll get a prompt response.