Installation note: All of the Twitter API tools are built around a set of common library files that must be downloaded and installed before running the code shown here.
For my first tool I’m going to keep it simple. This one should demonstrate my goals for these tools. They should be useful, single purpose, simple to include in existing apps, and informative about the quirks and issues that may not be documented for the API request being used.
This tool gets the data on a user’s last tweet, and returns it as an array. All of the tools are delivered as a function in a file with the same name. This allows you to use this code from any script with a require(), and then call the function. For example, this tool is in a file named user_tweet.php with a user_tweet() function. Let’s look at the tool script first, then we can test it out and examine the possible results.
user_tweet.php
<?php // Return an array of data from a user's last tweet // Copyright (c) 2014 Adam Green. All rights reserved. // Contact info: http://140dev.com, @140dev, adam@140dev.com // Released as open source under MIT license // $type is a string with either 'screen_name' or 'user_id' // $value is the matching account value // For example a calling script would use user_tweet('screen_name','140dev') function user_tweet($type, $value) { // Connect to API with OAuth require('oauth_lib.php'); $connection = get_connection(); // Get the account profile for this user $connection->request('GET', $connection->url('1.1/users/show'), array($type => $value)); if($connection->response['code'] == 200) { $response = json_decode($connection->response['response']); if (empty($response->protected)) { // If an account is not protected, // the protected element is blank $protected = 0; // The details of the last tweet are in $response->status $tweet_id = $response->status->id_str; $created_at = $response->status->created_at; $tweet_text = $response->status->text; } else { // If an account is protected, // $response->status is not included $protected = 1; $tweet_id = 0; $created_at = ''; $tweet_text = ''; } $results = array('code' => $connection->response['code'], 'protected' => $protected, 'tweet_id' => $tweet_id, 'created_at' => $created_at, 'tweet_text' => $tweet_text); } else { $results = array('code' => $connection->response['code'], 'response' => $connection->response['response']); } return $results; } ?>
Each tool in this series will include a matching test script to show how the tool can be called, and the type of results to expect.
user_tweet_test.php
<?php require('user_tweet.php'); print_r(user_tweet('screen_name','justinbieber')); ?>
You can run this test script from a web browser or the command line of a telnet or SSH client. I don’t include any HTML formatting, so the web browser output may be sloppy. Here is a sample run from the command line:
php user_tweet_test.php Array ( [code] => 200 [protected] => 0 [tweet_id] => 433469700228411392 [created_at] => Wed Feb 12 05:16:44 +0000 2014 [tweet_text] => Love both these people. Go see @KevinHart4real's movies. Thanks guys. http://t.co/5tkATy6Urg )
Notice that the output includes the API response code. Your code should check this code element before using any of the other data. If the code is not 200, you have an API error, and the returned array will also contain the full error message in a response element for debugging.
This tool also handles the issue of a protected account. If the account requested is protected, the last tweet is not included in the results, and the array has empty values for the tweet data. Your code that calls user_tweet() should check the protected element before trying to use the results. Here is a sample run with a protected account:
Array ( [code] => 200 [protected] => 1 [tweet_id] => 0 [created_at] => [tweet_text] => )
Feedback on this and and any other tools can be posted on the Google Group.