I have about 30 websites that communicate with Twitter in one way or another and, until a few months ago, I did so via the API that they retired on September 1st. Previous means of sending tweets to Twitter no longer work. The only method of integrating applications now is via OAuth – a far more robust and secure protocol. What I’m providing below is basic instructions on how to set up a basic application on Twitter using OAuth to post a tweet from within your application using PHP.
If you’re interested in the nitty-gritty of OAuth (not required to implement what I’ll show you below) you can read about it at OAuth.net or at Twitter.com.
Note: Updated ‘Twitter using OAuth’ Instructions
[Update 4th Febuary 2012] I re-wrote this post today because the way the twitter interface is presented has changed slightly causing a few problems for a few readers.
The Twitter OAuth Class
Originally developed by Abraham Williams, TwitterOAuth handles the generation, registration, and authentication of OAuth requests to interface with Twitter. For the purpose of this tutorial, and thanks to the comprehensive class, it’s not really necessary to understand the numerous complexities of OAuth, keys, tokens and so on… but you will have to download and use the library. We will only need two files “OAuth.php” and “twitterOAuth.php” – both can be downloaded here.
Unzip the files and upload to your server (perhaps into a directory called oauth).
Step 1: Log into dev.twitter.com
Log into dev.twitter.com using the credentials associated with the Twitter account you’ll be using.
Step 2: Fill out the application details.
Click the “settings” tab and you’re presented with a screen requiring various details. You’re required to fill out a name for the application (this will be the name that’ll appear under each tweet.. Sent from ApplicationName. You must provide a description for the application and a website associated with the account. Callback URL should be left blank. Sending a single tweet, a user won’t directly interact via a browser. Fill in the form, fill out the Captcha and agree to the terms and conditions…
Step 3: Application Type
On the same screen as the Application Details (under the settings tab), you must select the option for “Read and Write“. Your giving the application permission to write to your Twitter timeline. A common error is leaving the application type as ‘Read only’. If this is the case, nothing will be sent to Twitter.
Note: Read-only removes the ability to post tweets or add/remove friends, while Read & Write allows complete access. Read, Write and Access direct messages would give the application further access to the direct messages of a user.
Again, Callback URL should be left blank. If we were building a web-based service, this is the URL that Twitter would send the user (with their access tokens) after they authorize your application.
Click the button at the bottom of the screen – “Update this Twitter application’s settings” – otherwise nothing will save!
Step 4: Application Type
Now, and only now, go back to the “Details” tab and scroll down to the bottom of the screen. You’ll see a button that says, “Recreate my access token” under the the heading of ‘Your access token’. Click it: this will generate the necessary Access tokens.

Step 5: Copy your consumer keys and access tokens
If you now select the “OAuth tool” tab you’ll be presented with a screen that has your keys and tokens populated. Copy these keys into the PHP script.
Step 6: Copy your keys and tokens into the PHP code
Insert the keys and tokens from step 4 into the following code.
<?php
// Insert your keys/tokens
$consumerKey = 'insert-your-consumer-key';
$consumerSecret = 'insert-your-consumer-secret';
$OAuthToken = 'insert-your-OAuthToken';
$OAuthSecret = 'insert-your-OAuthSecret';
// Full path to twitterOAuth.php (change OAuth to your own path)
require_once($_SERVER['DOCUMENT_ROOT'].'/OAuth/twitterOAuth.php');
// create new instance
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $OAuthToken, $OAuthSecret);
// Your Message
$message = "This is a test message.";
// Send tweet
$tweet->post('statuses/update', array('status' => "$message"));
How is it used?
The $message variable in the PHP code is sent to Twitter.
I have used this code for several months and it’s always worked flawlessly. It’s intended to be used in your own applications so it’s not overly robust if you’re looking at a genuine tool for other people to interact with.
In coming weeks I’ve got a lot of code scheduled – including one handy snippet that will allow you to automatically post an RSS feed to Twitter. In fact, I’ve got a number of people testing a service I’ve written at Twytter.net that’ll be available – at this stage anyhow – exclusively to my readers.
Common Errors
Here are some common errors I’ve encountered from people thus far.
- Requires PHP5 (check with your host if you get lots of errors generated on the screen).
- Under the ‘settings’ tab, you must select ‘Read and Write‘ (this is the most common error).
- Twitter requires text sent to them to be UTF-8 encoded. Read more about UTF-8 encoding with PHP here.
- Make sure you copy the keys and tokens exactly as they’re presented to you.
- Server should have CURL libraries installed. Check with your host if you get errors.
- It’s best to download the PHP code rather than trying to copy it from the post. Many people have copied with various errors.
- From the ‘Details’ tab, if you click “Recreate my access tokens”, you will have to copy the new tokens into the PHP code. There’s generally no need to recreate them after you have generated them the first time.
Support
At the time of updating this post, I’m about to embark on a three-month project that’ll severely limit my ability to provide assistance. I’ll do the best I can. The best way of getting in touch is via Skype; my screen name is “martinkhoury”. Make sure you let me know why you’re making the request.
Let me know if you have problems.
|
|
Download: Twitter OAuth |
If you liked this article, you may also like:





Can’t get this to work. Error message:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /home/content/b/y/b/bybbyb/html/OAuth/twitteroauth/twitteroauth.php on line 21
the line of code referenced is
public $http_code;
Resolved. Requires php 5. Sorry I was using an old server still on php 4.
Hi there,
thanks for the great tutorial…
-i’ve downloaded the oauth library from the link provided above..
-applied my consumer key/secret, oauth token/secret from dev.twitter.com to the example.php file
-the file runs but no update on my twitter stream…
-i’ve triple checked my credentials… but same results..
please help… it worked before, but realised that my last successful update to twitter was sent on march 8th. did twitter change any anything recently? do you know..
Thanks in advance!
If it was working previously and doesn’t now, then I’m not sure what could have changed on your end. If you would like some help, send me an email and I’ll talk to you over Skype or email.
Perfectly working, am a newbie I have few Questions?
1) How do I post to twitter? (Presently i am changing the $message and typing the url in browser e.g http://www.xyz.com/oauth/example.php).
2) can u give me any about idea of multiple tweets? ( If Application Type is Browser then waht to do)
May this question seems foolish though i want details
thanks in advance
Sorry about the late reply – I’ve been out of the country. The question isn’t foolish at all!
First, the easiest way of sending your message to Twitter is via the URL integrated into a workable script. It’s likely that your web host will allow you to set up a ‘cron job’ to run the script you have written at various times throughout the day… although, if they don’t give you this functionality, you can accomplish the same via a third party provider (see my review of WebBasedCron.com)
To send multiple tweets you will have to write a script (or, if you like, I’ll write a quick one for you) that will either select the message from an array, a database, an RSS feed or some other source. Once you have the data, you can then send it to Twitter as a “$message”. Keep in mind that you’ll have to cut the string of text to a suitable length and you’ll probably want to truncate your URL as well.
Email me if you would like some help and I’ll call you on Skype.
Thanks Marty for replying,
1) My Web Host Support “Cron Jobs” but i have no idea about this.
2) As u mentioned that for multiple tweets its very helpful for me if u create one, coz i have no idea about php scripts.
I can’t find your email any where so dont mind for not mailing u, u can send me mail on : m[.]rupanjan[@]gmail[.]com or above mentioned mail address.
Acctually i need a script for my own posting purpose in twitter, as well as Facebook. facebook is not nessecary. so need your help
Thanks in Advance
Manik
Hi Manik. I’ve just sent you an email with my personal Skype address (I hope you’re on Skype??) so I can assist. I have a few hundred automated Twitter accounts. As an example, the most recent (about a month old) is http://twitter.com/AviationMedia … they don’t look overly automated – but they are.
Hi there!
I’m having some trouble with OAuth. Some times my status update through “tweet->post” goes ok but in the most cases it simply doesn’t! The strange is I get a “connection ok” doing:
$result = $tweet->get(‘account/verify_credentials’, array());
if(property_exists($result, ‘error’)){ echo “error!”; } else { echo “ok!”; }
Could you give me a hand here, plz? Thx!
First thought is that you’re sending duplicate messages. I’ve not seen this problem before but I’d like to do a few tests to work out what’s going on! What are the details of your server? What version of PHP do you have installed? Send me detils to marty@ this website and I’ll write a quick script to try and print any errors.
Hi Marty!
First of all, thx for the fast answer and attention. Second… can you believe that what was making it not work properly was, apparently, the fact of I trying to post words with accent! :\
Still, there are some instability, but it’s working now (in an overall point of view). ^^
Thx again.
P.S.: if you don’t mind, could you share with me your Skype?
P.S.2: sorry about my bad spelling! I’m from Brasil ;)
Hi V. I’m glad you worked out your problem… I suspected it would be something simple. I can help with the ‘accent’ issues if you like. A quick expression to convert characters should be enough.
Just emailed you my Skype details.
mailed u with the details, after talking in skype, still not recieved any updates, thinking ur busy with some work.
Thanks in advance
Hi Manik. Yes, free “support” takes me up to a week.
Hi Marty! Yeah yeah… I solved it with a simple utf8_encode function ;) have a nice week!
Brilliant. Talk soon.
Hi! I have the script working. Sorry if this is obvious, but how do I use the script to get and display my latest tweets?
The script won’t get your latest tweets… you’ll need something else to accomplish that. If you let me know where and how you want the tweets displayed I can point you in the appropriate direction for a solution.
Actually, I think I know what you’re after now. Do you want to retrieve text and them send those to Twitter? Where do you want to get your tweets from? RSS feed? Database? Text file?
Hey Marty, I’m new to this whole PHP process. I’ve uploaded the two files to my website’s server but when I go in to edit the files, I’m not seeing where to insert the keys and tokens. Any help would be greatly appreciated.
Hi Matt. In the zip file, there’s a file called example.php. On line numbers three to six you’ll see options to enter the keys you get from Twitter.
If you have any serious problems, I’ll be happy to Skype you through your issues. Use the contact form above and I’ll reply via email if that’s easier.
Hi,
i upload this file in my website but no result appear , can you help me please
an i try to get the top ten trends to my website that appear before one month but now “for-each problem” appear how i can fix it
thanks a lot.
This code won’t render Twitter trends onto your website; although I do plan on posting that code very shortly. I’m not entirely sure what you’re asking for…
Hello,
is it possible to edit the message before posting it. So after the login the user will get the edti input make any changes he wants, and then by clicking a Tweet button to actually post it.
Thanks
Hello. Yes, very easy to do. You’ll need some means of authentication because you obviously don’t want to make access to your account public. You’ll need to create a simple form that will pass the ‘message’ from one page to the next. Instead of defining the message in the script (which wasn’t ever intended to be used ‘as-is’), get it from the POST data. If you send me an email or message via the contact tab I may be able to help.
If I have time over the next couple of days I’ll post a quick example you can use to steer you in the right direction.
Thanks for the great article. It worked like a charm!
I’ve followed all of the listed steps:
1.) Registered application
2.) Set keys/tokens
3.) Uploaded files to server
4.) executed example.php
And have been unable to get this to work. Is there something I am missing?
Hi Chris. The steps are there (assuming they were done correctly). Were there any errors rendered on the screen? If you get in touch with me via email/Skype I’ll be able to help. There could be any one of a thousand reasons it’s not working.
Hi,
I’ve tried this a few times and it doesn’t work. I have keys correct and no errors on the example page.
I tried to put https://api.twitter.com/oauth/authorize
instead of https://twitter.com/oauth/authorize as it says so in my twitter app account but still no success :(
Any suggestions?
Hi. Send me your code and I’ll see what the problem is. You can trust me :)
I have a question concerning multiple tweets. For example I have a form that when it is updated I want to send a tweet out on behalf of another user. I’m storing the Token and the Token Secret of the user in a database. I’m having to login to twitter each time before I can send the tweet. When I say login I mean request a new token. I would think that I could just send the tweet again. Am I missing something?
/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['twitter_oauth_token'], $_SESSION['twitter_oauth_token_secret']);
/* If method is set change API call made. Test is called by default. */
$content = $connection->get(‘account/verify_credentials’);
$connection->post(‘statuses/update’, array(‘status’ => date(DATE_RFC822)));
I’ve just re-read over the oAuth documentation to try and find an answer.
First, are you sending your OAuth request to https://twitter.com/oauth/authenticate or https://twitter.com/oauth/authorize? The former will grant access until they revoke your permissions.
An uneducated thought… but should you remove the ['twitter_ part from your session details; so it's just $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); That’s something I’d try (a random reference in the manual made me think it may be necessary).
Can you let me know how you go? I’d like to know what the problem was myself.
hi there,this works like a charm, but can you suggest a way to post messages to tweeter not only to my account (by knowing the OAuthToken and OauthSecret and storing them in variables). When a user presses a button the tweeter login should pop up,he logs, and then by authenticating i get those tokens. I know this is discussed on a number of websites,but for some reason i cant get it work from there.
I’ll post a full working example (that I use myself) next week. Until then, it’s documented quite nicely on Dev.twitter.com.
Hello Marty please help me how to send a single twitter message through my twitter application to all the members who use my application or accessed it. Is it possible or not? can u guide me?
Sorry about the crazy late reply. get in touch with me via email — marty@[this domain name].com and I’ll see if it’s easy enough to help with straight away. Setting up a broader user-based application is very easy. I’ve got a bunch of posts in ‘draft’ on the subject. Unfortuately, the last six months, and the next three months, are consumed by other projects.
Hi,
I installed the code as instructed but no tweets appearing. As a developer myself I am reasonably familiar with php so I added some diagnostics into the 3 files just to prove that the “requires” were working properly and that the code was being invoked. Code all seems to be being executed OK but not producing the expected results.
Can you suggest any additional diagnostics I can insert into any of the 3 php files to track down exactly where the process is failing?
MY example.php SHOWN BELOW WITH DIAGNOSTICS CODE.
<?php
// Insert your keys/tokens
$consumerKey = 'dtGnetc….blahblah';
$consumerSecret = 'z4m9P….blahblah';
$OAuthToken = '39328…blahblah';
$OAuthSecret = 'ql2FyUx….blahblah';
// Full path to twitteroauth.php (change oauth to your own path)
require_once($_SERVER['DOCUMENT_ROOT'].'/oauth/twitteroauth.php');
echo "about to create tweet instance”;
// create new instance
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
echo “tweet instance created”;
// Your Message
$message = “Champagne, in victory one deserves it, in defeat one needs it … according to Napoleon.”;
// Send tweet
echo “about to tweet ” . $message . “”;
$response = $tweet->post(‘statuses/update’, array(‘status’ => “$message”));
echo “tweet completed”;
echo ‘Current PHP version: ‘ . phpversion();
?>
DIAGNOSTICS PRODUCED SHOWN BELOW
Into twitteroauth.php
into OAuth.php
out of OAuth.php-2
Out of OAuth 3
out of twitteroauth.php 1
about to create tweet instance
tweet instance created
about to tweet Champagne, in victory one deserves it, in defeat one needs it … according to Napoleon.
tweet completed
Current PHP version: 5.2.17
Sorry for the late reply. I suspect you’ve already fixed your problem but, for you, and anybody else, I would assume you need to set ‘Read and Write’ in the application settings (as defined in step 3). The server will issue a ’200′ response even if the tweet doesn’t send. If this is done, and it’s still not working, please get in touch via email (we’ll publish the solution here).
Hi Marty,
Is there some debug-info or a way to figure out if status/update has succeeded?
I try to post, all seems to work – no errors, but the tweet is not visible
Dennis
Hi Dennis. Make sure you’ve registered the application type as ‘Read and Write’ as described in step 3. This is the most common error.
i execute the example.php file from my localhost and i get this error:
Parse error: syntax error, unexpected T_VARIABLE and refers to the below line of code:
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $OAuthToken, $OAuthSecret);
You’ll want to send me your entire block of code. It’s usually an error in the line before. Email to marty@ [this domain name].com
Fatal error: Call to undefined function curl_init() in C:\wamp\www\InfoBot2012\oauth\twitteroauth.php on line 199
Call Stack
# Time Memory Function Location
1 0.0007 673928 {main}( ) ..\example.php:0
2 0.0327 1111952 TwitterOAuth->post( ) ..\example.php:18
3 0.0327 1112032 TwitterOAuth->oAuthRequest( ) ..\twitteroauth.php:157
4 0.0338 1115672 TwitterOAuth->http( ) ..\twitteroauth.php:188
so annoying!!! @@@@
CURL must be enabled on your server. You can check by creating a PHP file with
phpinfo();in it (details here at PHP.net). Search for CURL and see if’s accompanied by text that says ‘enabled’. If you’re not sure, call your host and ask them.Also, and it’s a long shot, make sure your referencing the oauth library correctly (from line 10); this means checking case sensitivity and file locations. An undefined function may just be that our PHP code is not including the OAuth library correctly.
Sorry about the crazy late reply. Busy…
I can also see you’re working locally on a test server? If this is the case, upload to your live server and see if it works there.
Hi
I have a question regarding the tweet.
I want to add a link to my website from the tweet. how can i do this? please help
i add but it appears as text.
Hi Rajesh, it’s likely that you didn’t include a WebSite correctly when you registered your account. Under the ‘Settings’ table, make sure you have included a full website address (example: http://http://www.internoetics.com)
How do i implement this in my website. I have got the secret key and all. I have download the required files. After that what do i have to do?
I suspect you’re talking about automatically submitting your posts to Twitter? Join my mailing list. I generally send the good stuff out to those on the list well before it ever makes it onto the website. I’ve sent out three scripts now to folks on the list that will do what you have described.
what change has to be done to sign in before posting the tweet?
I get the following error
Fatal error: Call to undefined function curl_init() in C:\xampp\htdocs\communityblog\system\net\twitteroauth.php on line 199
Help please! :)
CURL must be enabled on your server. You can check by creating a PHP file with
phpinfo();in it (details here at PHP.net). Search for CURL and see if’s accompanied by text that says ‘enabled’. If you’re not sure, call your host and ask them.Also, and it’s a long shot, make sure your referencing the oauth library correctly (from line 10); this means checking case sensitivity and file locations. An undefined function may just be that our PHP code is not including the OAuth library correctly.
Does this Post to Twitter using OAuth still work? I think I followed directions. No errors, but nothing gets posted at twitter.
Also at http://dev.twitter.com/, I did not see “Application Type”. Everything else seems to be et ok.
Ideas anyone?
Thnaks.
Hi Clinton. I’ve just updated the whole post. Twitter changed the way the screen presents which confused more than a few people. You’ll want to check on ‘Application Type’ as I’ve described in Step 3: Application Type. I suspect that’ll fix your problem.
Works perfect. Thank you very much!
Hi Marty,
i followed all your steps and kept the callback URL empty and tested the application with no errors but can’t find the tweets on twitter. can you please advise how can i find them or what is the problem.
i appreciate your prompt reply thank you for the helpful post.
Hi John. I note you’re in Lebanon. Are you trying to tweet Arabic text? If so, you’ll need to consider UTF-8 encoding as described in ‘common errors’ above. Let me know if this makes a difference.
Hi Marty
Great script worked first time no problems many thanks.
Just a quick question, I have reasonable php mysql knowledge but would love it if you had a helpful script that automated the posting task, just to save me time developing one, lazy I know but time is short at present.
again thanks for a brilliant script
What is it exactly that you want to do? I’ll be posting a whole bunch of applications over coming weeks. If you’re on the mailing list you would have already received a few scripts – such as RSS to Twitter, text file to Twitter and so on.
Great guide, thanks. Got it working in 20 miuntes. Very easy.
Will this work for when I upload a file to a directory on my site it will post a tweet to twitter with the file name?
Will this work for when I upload a new file to a directory it will post a tweet to twitter with the file name that was uploaded?
Hi D. Sorry about the late reply – busy at work. No, it will post a message to Twitter that’s referenced by the $message variable in the code. In the example above – which was not intended to be used as a standalone solution – you will have to use the URL simply to call the script. What exactly are you trying to do?
I’ve got a Cydia Repository and would like a tweet posted to twitter when I upload a updated file or add a new file so my followers will know to check Cydia
Get in touch with me via the contact form with details. I’ll be able to lend a hand at the end of the month. I hope that’s not too late for you.
which ever works for you I’m ok with
Great little snippet that may well come in handy for part of what I do.
now what do i do with these files?!?! should i upload them into my website? please help
Yes, you upload them all to your web server. The code is intended to be integrated into other applications… some of which I’ll probably have to share quite soon given the number of requests. As a standalone tool, it doesn’t do much at all.
Hi, i have a facebook page which is in category “Business”, it is not a personal page, it is a business facebook page. In twitter, i already set when post to twitter, it will post in facebook also, but when i use the above php code to run and post, it successfully post up to tweeter, but….. facebook page does not get the post on the wall? Any idea why?
Thanks.
Regards.
hii marty thanks for the great. Post..
can u tell me how to send auto welcome tweet when someone follow you on twitter..
example:- hii thanks for the following me..
2, ther’s any script for auto follow back people on twitter instanly..
thanks in advance..
I’m writing something now. I’ll post it soon.
I don’t have work to say thanks to u. really you are great.. genius.
Hi Marty,
Really i am so happy to get this simple and easy code. but can i post image as well as text also.
thanks and regards
Sandeep
Just thought I’d say thanks for this, great tutorial, better than all the rest. No code changes (‘cept for the API tokens). Thanks!
Hi,
I put this code in my php script, and after a little wrangling it seems to run without reporting any errors that I can see. But no tweet shows up in my account.
This should result in the message text being tweeted to my own account, right, the one I registered? Any idea how to track down why it didn’t work?
Thanks
Bob
Without knowing exactly what you’ve done it’s hard to very specific (send me an email if you like and we’ll work it out). Yes, it should send to the account attached to the application you set up as per the above instructions.
Make sure you’ve registered your application properly (with read/write permissions). That seems to be one of the more common errors.
Make sure the text you’re sending is under the 140 character limit. Long shot, but one gentleman I worked with recently hosted with one of the big providers and their IP address was one banned from Twitter (it’s unlikley, but possible, that this could be your problem. Consider it as a possibility if you’re hosting with somebody like BlueHost or HostGator).
marty {at} internoetics {dot} com
i tried and working good, thanx for the code..
how to get sending status, whether it have been sent or not?
i got the status report
$response=$tweet->post(‘statuses/update’, array(‘status’ => “$message”));
$response->error;
thx
Simple and easy! This got me on the right track for writing my app which includes following and unfollowing. Thanks for sharing this and getting me on my way.
Hi
I have your script working well on its own.
I modified it to operate within a page on my website such that when someone reads an article on my site, your modified script is included and sends a Tweet that someone is reading the article. Great, except that it produces about 20 Tweets and my account gets locked out for a few hours.
I tried ‘include_once’ but that doesn’t work. Have you any ideas what is going on? Bit embarrassing when I’m using my live twitter account for testing
Regards
Paul
Hi Paul. I’d really have to see your code (you can send it to me if you want). I’d advise against it, though. Wouldn’t it just pollute your Twitter stream with useless info?
The Twitter function would be called once in the main loop and executed. Perhaps you’d want to limit the number of tweets by IP address (in case somebody ready multiple pages) or only send one tweet for every 100/1000 page views etc?
Email me!
One more thing… perhaps you should register another account for testing??
Thanks for the code, it helps me for the first time to tweet my own timeline from application. ;-)
pls if i want to send a direct tweet/message to a user
Hi Seun. A new post is coming soon. Marty.
Man i get this error… i already did it with another twitter account but i try a new one and it doesnt work…
TwitterOAuth Object ( [http_code] => [url] => [host] => https://api.twitter.com/1/ [timeout] => 30 [connecttimeout] => 30 [ssl_verifypeer] => [format] => json [decode_json] => 1 [http_info] => [useragent] => TwitterOAuth v0.2.0-beta2 [sha1_method] => OAuthSignatureMethod_HMAC_SHA1 Object ( ) [consumer] => OAuthConsumer Object ( [key] => ########### [secret] => ########## [callback_url] => ) [token] => OAuthConsumer Object ( [key] => ############## [secret] => ############[callback_url] => ) )
Obiously the ##### are my secrey keys….. cheers!
Hi, thanx for the post. I have followed all the steps but when i run it ,it doesn’t show anything on page. I don’t know why? help needed.
Works beautifully… Thanks man .. I was struggling with abham’s code. This one exactly fits my requirement.
This is the best example of a twitter script i have seen! Simple to understand and does the job. Thanks a lot man