Recently Twitter announced they are going disable basic authentication mechanism. Basic auth can be used until June 30th 2010, so there is only less then one month left (assuming you are reading this on June 2010). I admit that for a long time i neglected the OAuth Twitter authorization, it seemed complicated and time consuming in comparison basic auth.
However, when i found out they are shutting down the basic api, i had no other choice then to learn how it works. What it turned out is that with the right tools it’s pretty easy to do. Especially if you are (like me) looking to setup application that will be using only single user. For example to automatically update your status.
The setup
With basic authorization all we needed was Twitter login and password, with OAuth it’s more complicated but a lot more secure at the same time, even if your app will be hacked your Twitter credentials are still safe.
Ok let’s get started with it:
- Go to dev.twitter.com sign in with your Twitter credentials, next click “Register an application” link
- Fill the form, it’s automatically accepted so Twitter guys probably don’t check it all, but still … keep it proffesional. One important thing here is to select “Read and Write” on “Default Access type”. Click “Register Application” and you’re done
- Now you will need 4 keys to make your app work. Go to “View Your Applications” and then click “Edit details” on your newly created app. Scroll down and copy (somewhere) “Consumer Key” and “Consumer Secret”
- Next in the right sidebar click link “My access token” and copy both: “Access Token (oauth_token)” and “Access Token Secret (oauth_token_secret)”
Getting nervous? Relax, we are almost done, last thing we need is awesome Abraham Twitter OAuth library (click the link to download it). There few files in the archive, but we will need only two: twitteroauth/twitteroauth.php and twitteroauth/OAuth.php.
Twitter and PHP OAuth integration
Now let’s get straight to the actual code. The good news it’s there almost nothing to do: just include TwitterOAuth library, put keys and tokens you copied earlier into the script and you are done.
<?php require_once 'TwitterOAuth.php'; define("CONSUMER_KEY", "????"); define("CONSUMER_SECRET", "????"); define("OAUTH_TOKEN", "????"); define("OAUTH_SECRET", "????"); $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET); $content = $connection->get('account/verify_credentials'); $connection->post('statuses/update', array('status' => date(DATE_RFC822)));
Pretty easy right? Keep in mind that this is solution for situation were all so called “Twitter actions” are executed by a single user. Other then that OAuth library takes advantage cURL library so you better have it enabled if you would like to Abraham’s library.
Image by factoryjoe source: http://www.flickr.com/photos/factoryjoe/3343062926/in/pool-519057@N23





67 Comments on "Twitter, PHP oAuth: update status"
Great writup here, very strong advice. I like your TwitterOAuth library and will continue reading! Thanks!
tnkss very god admin.
Tried to use it but it didn’t work. Made a correction to require_once ‘TwitterOAuth.php’; and changed to require_once ‘twitteroauth.php’; still didn’t work. Does it really work?
Sure, what error are you getting?
It doesn’t work, I get a blank?
Check your Twitter status rather then script output. It doesn’t print anything on the screen it just updates your profile status.
Very helpful info, saved my time! Thanks!
Hi there and thank you very much for sharing your code
Unfortunately it does not work for me
Not your code only, but ALL the automatic status update (php) script based on twitteroauth.php file that I have tried in the past 2 weeks! :-O
With your code I call the web page, the php script runs, then it stops. Blank web page (correct!), but NO twitter status updating :___(((
Could be that it is my web hosting fault?
:-\
Thank you in advanced for your kind help
P.S. I’m a ‘little’ bit frustrated: it seems so easy… but it does not work >:(
Used the old xml and curl technique and I thought it would go by the wayside. However, it’s still churning along. I did convert to your code above and it works sweet.
I do have a software that runs on server and I need to distribute to 100′s users with “their” auth info. I store in a ini file that the php references. So each user then will have to sign up as a twitter developer to get the keys for the software to work for them? Can their be dupes in the software names?
Thanks man.
Thank you very much Greg Winiarski, I was looking for this….
great find! thanks Greg!
anybody else having trouble passing parameters in the ‘statuses/update’ part of the code? specifically, ‘in_reply_to_status_id’ doesn’t show up when I update a status?
e.g.
$update = $connection -> post(‘statuses/update’ , array(‘in_reply_to_status_id’ =>10000000000, ‘include_entities’ => 1 , ‘status’ => ‘the status text’));
Thanks
Hi Greg,
This lib works great, was hoping you could help with one issue, I can’t seem to get the upload of a new avatar to work, the twitter endpoint http://api.twitter.com/version/account/update_profile_image.json
Any ideas on what I need to do?
-Paul
I get:
Warning: parse_url() expects exactly 1 parameter, 2 given in OAuth.php on line 231
Fatal error: Call to undefined function hash_hmac() in OAuth.php on line 116
Can anyone help?
Update. It’s cos we’re using PHP 5.0.4 and the second parameter in parse_url wasn’t added until 5.1.2. Anyone know a workaround?
Good stuff. Very simple and should work for my (very simple) application.
One question, though… Is there a way to test for errors and out put them to a log (I have the logging functionality already)
Thanks man
Just wanted to thank you for this script, made the conversion from the old XML API to OAuth very simple. However, I did run into a bit of a problem with the script where it failed to update…but it seemed that removing the line:
$content = $connection->get(‘account/verify_credentials’);
resolved the issue. Not sure why, but as long as it works, Many Thanks!
thank you! easiest way to update twitter status from site!
OMG. Thank you. You just saved my day. Fantastic.
well, i owe you A BIG THANK YOU!
IT WORKS!
Damned! Is there any way to update twitter status without using this curl-library? I hav’nt this library on my server… help!
lg
Fm
Do not know why but I can not get this to work. script runs, blank page – but twitter shows nothing.
No status update.
All I want to do is post twitter updates using a single account
how difficult is this proving to be compared to Basic Auth. Urgh.
Working!
Thanks. Works fine.
Hi.
Thank you for this article. But It doesn’t work for me. I have made all steps and got error like “Invalid / used nonce”. Please can anyone help me?
Thank you for this tutorial. It works fine for us!
Well Done!
I have found problem and successfully resolved it. My problem is described there http://blainegarrett.com/2009/07/14/failed-to-validate-oauth-signature-and-token-on-twitter-oauth-check-your-cloc/. Hope it can be helpful for someone yet. Thanks.
thank you very much. i already knew the library but needed help with the automatic update. with your help i wrapped it all up in less than 5 minutes. thanks again, you’re super.
Thanks mate, I got you integrated in about 20 minutes for my daily mayan horoscope on twitter!
Thank you Greg for sharing this.
One question: does anybody know how I can get the ID of the last status update? Thx in advance!
Sorry for what will probably be an incredibly stupid question, but is the code in the box all that’s needed?
Does the status updating come from the line:
$connection->post(‘statuses/update’, array(‘status’ => date(DATE_RFC822)));
Or should there be something added afterwards? I registered an application and got the right codes put into the script, but it doesn’t seem to be sending any updates.
Any help would be great, thanks!
I tried and I tried and I tried and it just won’t update my stat
produces no error, prints nothing after execution, yet it doesn’t seem to work. What do I do wrong ?
I dont know how to send parameters like in_reply_to_status_id or in_reply_to_user_id
Any clue?
Thanks…
Trying this
$connection->post(‘statuses/update’, array(‘status’ => $status),array(‘in_reply_to_status_id’ => $id),array(‘in_reply_to_user_id’ => $userid));
I am kind a little bit “stupid” saw the solution, right after posting my comment.
Here is the correct way…
$connection->post(‘statuses/update’, array(‘status’ => $status, ‘in_reply_to_status_id’ => $id, ‘in_reply_to_user_id’ => $userid));
Still no updating twitter status. Solution?
Got same problem / solution as “Runescape Grand Exchange” (btw you have a great name
I removed the line also:
$content = $connection->get(‘account/verify_credentials’);
And it worked (but not with this line), could it be that this line makes a HTTP 200 OK and the next instruction is not processed?
Cheers buddy, great information
Has anybody gotten this script to work?
For starters, the required .php file should be all lowercase, since that’s the format of the file.
But even after that change was made, I get zero status updates (and no errors).
I assume that the script above would just post a ‘date’ as the sample status message, correct?
This script seems incomplete — and has at least one obvious error. Judging from the responses, I don’t see any that suggest that it works for anyone.
Hi Greg,
I tried to use your scripts on my site.
The problem is that i have PHP version 4.4.9 and is not working. I know it works for versions > 5.0, but my hosting will not change the actual version of php.
Any ideas?
Thank you very much!
Great information Dear… Thank you so much…
So, Greg, any ideas for those who use Php version under 5.0?
Thanks mate!
Adrian
@Adi my code is PHP4 friendly, so the “problem” are libraries, however i would rather advise to change hosting that does not support PHP5 rather then modify libs.
Thank you, SPOT ON !!!! I was worried I’d have to write 50 lines of code.
Why Do I Need To register :’(
It only update my own status, It can’t update other people status, is this normal? =(
@Elvi, yes, if you want to update someone else status you need to login as that person.
Thanks a lot. Exactly what I needed. Works like a charm.
How to update status in twitter with PHP?
Hi,
This great and very simple to use, but is there a way to return if the tweet was successful or not?
Thanks
I do not really know but you can do
print_r($connection->post(‘statuses/update’, array(‘status’ => date(DATE_RFC822))));
and see what it returns.