We developers love a good API. If you aren’t sure what an API is, be sure to check out this lovely page on wikipedia about them. API stands for application programming interface, and it allows developers like us to tie our code to different services that are already built.
This means that we can build programs that interact with other platforms so that they can start to do some amazing things. Some of our favorite APIs are the Google Maps API, which lets us plot and map virtually any data to points on a map, and the Twitter API, which lets us control every aspect of a Twitter account. In this blog post I’ll give you a quick example of working with the Twitter API, to show you just how easy it is to work with.
As we like to work with PHP, I’ll be showing you how to tie Twitter services into your PHP applications. The first thing that you will need is the php-twitter class that is hosted over at Google Code. Next you’ve got to include that class in your application. I’m building a really simple app, where all my code is in one file called twitter.php, and this is in the same folder as class.twitter.php. Some people like to build a folder for all their classes, but as this is a really simple example, I’m just going to have them live in the same folder.
include_once("class.twitter.php");
Now that we have all that code included, we can start to use it.
$t = new twitter(); $t->username = 'USERNAME'; $t->password = 'PASSWORD';
What I just did was created a new object called $t that is our twitter stuff. Now we have to tell twitter a few things about this object. Next I set the username and password for our object. This still isn’t doing anything, so let’s get it to display our latest tweets.
$tweets = $t->userTimeline();
Now we have an object that is holding all of our tweets in it. Twitter actually gives us a lot of information along with the tweets, but all that we want is the content. If you know anything much about computers, you’ll know that they like to start counting from 0. So, your most recent tweet isn’t number 1, it’s number 0. Let’s print out the content for tweet 0.
print $tweets[0]->text;
Beautiful! There is our latest tweet. If we wanted to display our latest tweet in our sidebar or something, then we are done. We wrap some CSS style around it and we can now display the latest twitter update in 5 lines of code. Say we wanted to display the latest 10 tweets. Well, lets build a simple loop to display tweets 0-9 (remember, we start counting at 0).
for($i=0;$i<10;$i++) { print $tweets[$i]->text; }
This is called a for loop. There are different ways to do loops in php, but this is one way that I like to do it. That first line if telling PHP our looping conditions. $i = 0; means that we are building a new variable named $i, and setting it’s value to 0. $i<10 means that we would like this loop to continue as long as $i is less than 10. $i++ means that every time we run the code in our loop, we would like to add 1 to $i. Get it? Then we have an "{" and two lines lower we have "}". In between each of these curly brackets is the code that we want to have run. That line of code looks almost identical to the line that we ran when we only got our most recent tweet, except for one change. Instead of $tweets[0], I used $tweets[$i] because $i is getting bigger inside of that loop. Makes sense?
Here it is all together so it's easier to copy and paste into your own code.
include_once('class.twitter.php'); $t = new twitter(); $t->username = 'USERNAME'; $t->password = 'PASSWORD'; $tweets = $t->userTimeline(); for($i=0;$i<10;$i++) { print $tweets[$i]->text; }
Maybe now you should try and figure out how to get them onto separate lines. Post any questions or comments in the comments below. For more Twitter API awesomeness, check out the Twitter API for Python. Thanks for reading!



