HangmanI did something weird with the Twitter API. A while back I used tweets to form the word puzzles for a Hangman game. You can feed the puzzles from a user’s stream by putting the user’s twitter handle in the URL, like ?twitter=mvs202. Oddly enough, this cool game never went viral, and it sat unloved and unplayed for a long time. When I tried it myself recently, it had stopped working. Here’s what happened and how I fixed it.

The original Twitter did not require authentication for the most basic searches. The API handled API calls just like it handled web searches, so the API version of twitter.com/search?q=test was simply search.twitter.com/search.atom?q=test. This worked great back when I wrote Game-ifying Twitter with Hangman two years ago. The new API requires developers to sign up. Plus, the response format has been totally changed to improve object consistency.

I wrote the game before discovering the joys of AJAX. Back then I consumed synchronous APIs by making the whole program in PHP. That means that the web page doesn’t render until the API call is complete so the server can wrap up the HTML code. Not a problem with Twitter, which has a speedy API, but still. The bigger problem is the readability of my own code. Mingling HTML and PHP makes for a difficult read, made worse by having “echo” statements generating HTML and Javascript on the fly.

Luckily there is an easy-to-use PHP library: TwitterAPIExchange, by J7mbo. I was able to insert this in my PHP code and let ‘er rip. I copied J7mbo’s PHP code to my server’s “lib” folder, and referenced it with this simple PHO statement:


Here’s how I search for tweets:

$settings = array(
    'oauth_access_token' => "Access token",
    'oauth_access_token_secret' => "Access token secret",
    'consumer_key' => "Consumer key",
    'consumer_secret' => "Consumer secret");   
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield('?q=from%3A' . $_GET["twitter"] . '&count=100')->
  buildOauth('https://api.twitter.com/1.1/search/tweets.json', 'GET')->
$tweets = json_decode($json);

The $_GET “superglobal” variable fetches the parameter from the URL. I use the isset function beforehand to ensure it has a value. The GET search/tweets API requires all four keys & tokens, which you can get from your “My applications” console.

I use my $tweets object to build a thumbnail image of the user. Because I queried for tweets from a single user, I can get user info from the first tweet, element [0]:

echo '<img width=48 height=48 src=' . 
  $tweets->statuses[0]->user->profile_image_url . 
  ' title="' . 
  $tweets->statuses[0]->user->name . 

Looping through the stream of tweets is pretty straightforward

foreach ($tweets->statuses as $tweet) { 
  $phrase = (string) $tweet->text;
  // do stuff with $phrase

If I got to do it over again, I would use PHP solely as a proxy script to query Twitter and just return the results, with minimal processing. The web page then would have just HTML and Javascript, using AJAX to ping the proxy script and react when the results come in.

This may not be the most exciting use of the Twitter API, but it was a good way to get my feet wet. And it turns out tweets aren’t good material for hangman puzzles. Puzzles with 140 characters are way too easy to solve, and most people don’t use standard English in their tweets, further complicating things.

If you know someone who sends out very short tweets with regular English, try playing Hangman with their accounts. Here are a few accounts I’ve tested: @meganamram, @justinbieber, @CNN.

Updating the API Approach

One Response to “Updating the API Approach”