Home > Code Workshop > Easy steps… to your Twitter App in PHP (OAuth_Twitter.php)

Easy steps… to your Twitter App in PHP (OAuth_Twitter.php)

[tweetmeme service=”bit.ly”]


Twitter App in php can be done so easily using the   library OAuth_Twitter.php . This library works as a connecting layer between your Application and Twitter API by making use of the package OAuth. If OAuth_Twitter.php is included inside your code, which acts as a gate way for your Application to Twitter API, you can access Twitter data using the methods of this Library

Setup the coding environment

As I stated earlier the library OAuth_Twitter.php works with Zend OAuth package, you have to download and include this package in your code. You can download the same from the Box.net widget of this blog of from the Google code repository. Once you are done this, you can include OAuth_Twitter.php in your code(downloadable from box.net widget in the home page of the blog).  Done!!! Now start playing with twitter…

Register your App with Twitter

For twitter to share its database with your application, you have to register your application with Twitter. The details of the same is there in my previous post You have to use your Application configuration data, provided by twitter once your application is registered, don’t worry. Your headache ends here.  From here onwards all your actions  will be handled by the library, using the its methods
[tweetmeme service=”bit.ly”]
How to use OAuth_Twitter.php

OAuth_Twitter.php library is furnished in object oriented fashion. Once your have included this file to your code, you have to create an object of class OAuth_Twitter. You have to provide your configuration data as a php array to the class

$configuration = array(
‘callbackUrl’ => ‘http://yoursite/callback’,     //callback url for Twitter API
‘siteUrl’ => ‘http://twitter.com/oauth’,
‘consumerKey’ => ‘oPI6Q213VSMd5W84FfHbOA’,            // consumer key provided by twitter
‘consumerSecret’ => ‘0VCb37yQOdfsDSWu50o3XOxA’  // consumerSecret key provided by twitter

$my_twitter = new OAuth_Twitter($configuration);

Here I want to introduce you some of the functions… for all others you  are provided with a detailed documentation.

Sometimes, to retrieve some kind of data (Followers list) twitter will ask the user of your application to authenticate with twitter.  For actions like updating your status is mandatory to authenticate your identity to Twitter. Once you have authenticated, then you can do any further action directly. After successful authentication Twitter will always redirect to the call back url of your application, with an access token for your application. Your application will be using this access token for further access. For your application, you dont have to worry about any of these details. A single function  ” $my_twitter->handleCallback() ” will take care of all thses actions for you 🙂



$my_twitter->handleCallback()    //  You have to call this single function in your call back url. This will take care of setting and managing your access_token and all



Another function which may seem complicated is requestAuth(). This is any user to authenticate with twitter from your application. In high level it does 3 things. Check for already authenticated, if yes return 1, else redirect to twitter to authenticate and on successful authentication, twitter will redirect to your callback url.

Getting Twitter Data

Now you can access any kind of data like, followers, friends , tweets etc… if authentication is required and not done, the function will redirect to authentication page and continue

$my_twitter->updateStatus($status)  –> to update your status

$my_twitter->getFollowersByHandle(‘BasilBThoppil’)  –> to get BasilBThoppil ‘s followers

[tweetmeme service=”bit.ly”]

Categories: Code Workshop
  1. Sami F.
    May 5, 2010 at 9:48 pm

    you should provide some *working* sample code

    vauge descriptions don’t help someone quickly see just how your code works

    is a quick demo.php file too much to ask for?

    • basiL b ThoppiL
      May 6, 2010 at 10:47 am

      Hi Sami,
      Thanks for your suggestion. I have provided a detailed documentation also along with the library, which is furnished mainly for developers. As you said, that wont be that helpful to beginners. I will provide on working example file also ASAP. 🙂

  1. January 25, 2010 at 11:55 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: