Installation and Environment: The main page for Arc90 provides a very basic introduction on how to install and use the package, which is sufficient for getting your PHP environment ready to use. So, for the purpose of this, I will assume that your PHP environment is all set, your includes are in order and cURL is ready to go. If you need further help with any of those areas, feel free to leave a comment below. So, I will assume you have a set up like the following:
- <?php
- require_once('Arc90/Service/Twitter.php');
- $twitter = new Arc90_Service_Twitter('username', 'password');
- try
- {
- // Gets the users following you, response in JSON format
- $response = $twitter->getFollowers();
- // Store the JSON response
- $theData = $response->getData();
- // If Twitter returned an error (401, 503, etc), print status code
- if($response->isError())
- {
- echo $response->http_code . "\n";
- }
- }
- catch(Arc90_Service_Twitter_Exception $e)
- {
- // Print the exception message (invalid parameter, etc)
- print $e->getMessage();
- }
- ?>
Important Note Regarding API Rate Limits: If you are unfamiliar with Twitter and its associated API calls, you need to make sure that you don't overdo it on the number calls you make. By default, Twitter allows you to have 100 API calls per hour (this includes using third party applications like TweetDeck or Nambu), so you'll need to keep an eye on how many calls you're using. If you go over that limit, you can potentially be blacklisted -- which makes no one happy. You have a couple of options: 1) Request to be whitelisted using this form. This will allow you to use 20,000 requests per hour, or 2) Watch your rate limit using a built-in API method which monitors your status, and conveniently does not cost you an API call to use. You can find more info regarding limits, whitelisting and blacklisting here. Also, during development, I've gone and saved the output to a particular API call to a text document that I can parse while I test. It doesn't provide real-time data, but it allows me to work on it as many times as I want without a single API call.
Using the Data: Now that we have our data response in JSON format to work with, let's get to it! To make good use of the data returned, I chose to put it in an associative array for organized access. To do this, we use the json_decode method built into PHP as follows:
$dataArray = json_decode($theData, true);
To view an example of the kind of data that a getFollowers API call will return, we can view this method page in the documentation. Also, for a detailed description of each key that can possibly be returned from any of the various API calls, you'll want to keep the complete list handy. I ended up making a spreadsheet with the various API calls that I tested along with the associated return values, for a quick reference -- so you may want to do the same.
Let's say that we just want to loop through our followers (or at least the first 100) and print out some data about each one, line by line. All we have to do is iterate through the array, pulling out the data we want and display it. I stuck with a basic HTML table for this, but all you CSS gurus out there can have way more fun with this. This assumes that you already have your associative data array, dataArray, ready to go:
- <html>
- <head>
- <title>Test</title>
- </head>
- <body>
- <h1>Followers</h1>
- <table border="1">
- <tr>
- <td>Image</td>
- <td>Screen Name</td>
- <td>Name</td>
- <td>Description</td>
- <td>URL</td>
- <td>Location</td>
- <td>Following</td>
- <td>Followers</td>
- <td>Status</td>
- <td>Status Count</td>
- </tr>
- <?php
- $numItems = count($dataArray);
- for($i = 0; $i < $numItems; $i++){
- $currentItem = $dataArray[$i];
- $screenName = $currentItem["screen_name"];
- $description = $currentItem["description"];
- $followersCount = $currentItem["followers_count"];
- $url = $currentItem["url"];
- $name = $currentItem["name"];
- $status = "";
- if(isset($currentItem["status"]))
- {
- $status = $currentItem["status"]["text"];
- }
- $friendsCount = $currentItem["friends_count"];
- $profileImage = $currentItem["profile_image_url"];
- $location = $currentItem["location"];
- $statusCount = $currentItem["statuses_count"];
- echo "<tr><td><a href='http://www.twitter.com/".$screenName."'><img height='48' width='48' src='".$profileImage."'></a></td><td>".$screenName."</td><td>".$name."</td><td>".$description."</td><td>".$url."</td><td>".$location."</td><td>".$friendsCount."</td><td>".$followersCount."</td><td>".$status."</td><td>".$statusCount."</td></tr>";
- }
- ?>
- </table>
- </body>
- </html>
If you have any trouble working through this, or have any questions, just let me know!