I’ve written before about how great it is to work with a service that has a great API. Twitter was one of the tools that I talked about in that post and today I’m going to show you the basics of working with their API.
PHP is a great language for web applications, and it’s available with just about any web host. If you aren’t familiar with how PHP works, this tutorial will probably be a poor place to start. Today we are going to talk about how to work with the Twitter API and PHP, and you’ll need to know a few things about how PHP works to keep up. For a good refresher course on PHP, click here.
Click through and let’s get started!
First lets get an awesome open source class that pulls all the goods from twitter into a nice handy PHP object. Go to http://code.google.com/p/php-twitter/ and grab the latest version. Put that file into a folder by its self. Now let’s make a new file called index.php in that same directory. Open that file up.
The first thing that we need to do is include that super awesome class file. Start your php code with the open tag and then include the twitter class.
1 2 | <?php include_once("class.twitter.php"); |
Now that the class is included, let’s make a new object to work with. Once that object is created, lets set a few important variables for this object.
4 5 6 | $t = new twitter(); $t->username = 'USERNAME'; $t->password = 'PASSWORD'; |
If we want to get our latest tweets, we can use the method “userTimeline” and it will return a lot of data about your recent tweets. Lets store that data in a variable and display some tweets.
8 9 10 11 12 13 14 | $tweets = $t->userTimeline(); for($i=0; $i<5; $i++) { //loop to print out our twitter avatar and the latest 5 tweets print "<img src='".$tweets[$i]->user->profile_image_url."' alt='".$tweets[$i]->user->screen_name."' />".$tweets[$i]->text."<br />\n"; } //end php ?> |
Now if you upload these files to your webserver and browse to that directory, you should see your avatar and your 5 latest tweets.
Lets do something a little bit more fun than just display your latest tweets. Let’s build a form that updates your twitter status. You can then use this form to update instead of going to twitter.com or using your mobile device. First let’s add a form to the index page below the 5 tweets we are displaying with the above code.
16 17 18 19 | <form action='update.php' method='post'><input type="hidden" name="phpMyAdmin" value="h1cjFojtxzm4Mfa3dkNBgxO52Fe" /> New twitter status: <input type='text' name='tweet' value='' /><br /> <input type='submit' value='Update Twitter' /> </form> |
We have that form processing to a page called update.php. Let’s make that file and have it process your update, then return you to your index page. Here’s the code for update.php.
1 2 3 4 5 6 7 8 9 10 11 12 | <?php include_once("class.twitter.php"); $t = new twitter(); $t->username = 'USERNAME'; $t->password = 'PASSWORD'; $t->update($_POST['tweet']); header("Location: index.php"); ?> |
One word of advice, don’t use this code just anywhere. If you put this form on your homepage, then anyone can type into that field and update your twitter! Because you tell twitter who you are with this code, it doesn’t really care who fills out this form. This could give spammers access to post tweets as you, so be cautious as to how you use this. You’ve been warned.
I’ve attached a zip file with all the necessary files to make this work on your own. Just be sure to edit the files to use your real username and password, or none of this will work. Leave a comment if you found this useful, or have any questions. Thanks for reading!




I was looking for a simple tutorial. Perfect