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"
Thanks for your reply Greg,
That does give an output but gives you a lot more information then required is there a way to specify the output?
Thanks
Sorry, i couldnt understand where i put that code
get(‘account/verify_credentials’);
$connection->post(‘statuses/update’, array(‘status’ => date(DATE_RFC822)));
couldnt understand where i put that code
I got all that, but all I want to do is post a single tweet to my Twitter account from my php web application.
When someone wins at a certain game I want to tweet “john doe won at checkers today!” via MY twitter account.
How can I use the oAuth (which is now required) to just post one tweet via my account with php?
Everything I find online is the old CURL method which does not work due to oAuth.
FOUND THE PROBLEM FOLKS!! Thanks to POPO!!
For starters, the required .php file should be all lowercase, since that’s the format of the file.
<?php
require_once 'twitteroauth.php';
and it WORKED PERFECT!
What if I wanted to include several tweets in an array? How can I do that in the $connection->post(‘statuses/update’, array(‘status’ => date(DATE_RFC822))); line?
Never mind. I figured it out…kinda.
$input = array (“tweet1″, “tweet2″, “tweet3″);
$num = array_rand($input);
$message = $input[$num];
$connection->post(‘statuses/update’, array(‘status’ => $message));
Wicked thanks, used used that to update our Twitter status when we import new ringtones to our site.
Appreciated, simple and quick!
Can I use this same files if I wanted to let others use my site to update their statuses? Do you know how to authorize these users so that they can post status directly from my site?
I have a script that updates Twitter with the song just played on the station, every 8 songs. It’s worked fine now for about 6 months – now suddenly in the last say 20 days – it no longer updates.
Just gives the 401 error code.
I haven’t changed a single thing anywhere. and I can’t figure out why it no longer works, other than that Twitter may have changed something to do with oAuth?
any ideas anyone?
Thanks
==============
Current Time Referenced To Twitter Server
Sun Mar 20 11:51:00 +0000 2011
[Attempting Update] – ( 82-chrs Used / 58-chrs Remaining )
Casta Diva(Extended) from Norm by Filippa Giordano… on http://Quantum-Radio.net
Update Failed – [Return Code] = 401 – Unauthorized
==============
Hello,
Nice to read your tutorial..
I have some query as below.. IF possible then please reply me…
I have multiple account on twitter.
Now a days i want to share the post on twitter account automatically from my website for selected twitter account.(using new rule – Twitter-OAuth)
So how i can do it without login a twiiter each time?
It should be done after just passing some required parameters from my website to twitter’s URL for particular twitter account.
if i do it using http://github.com/abraham/twitteroauth tutorial then , it must necessary to register application for each twitter account and get secret key & public key and all.
But i don’t want to make application registration for each twitter account rather than my single account.
so please tell me proper guidance to do that….How it will be done?
hope you may help me as soo…
Thanks
Praful
Great tutorial.
Worked nearly straight away, but for the filename needed to be lowercase.
Thanks
I have the same situation as Praful. I have multiple twitter accounts and i do not want to register an application for each account.
So how do i go about it?
Thanks for this, that was stupid-easy.
hey guys
tried it out but am i too stupid? it doesnt seem to post to my site ?
why is that?
any clue?
please let me know!
wouter
Many thanks for this. After a bit of messing with the access codes (had to resubmit them), this worked flawlessly. If people want to get feedback on how the submission went, just put
var_export($connection->http_info);
on the last line. This is equivalent to using curl get_info and gives you a nice array of values to help you debug.
Thanks alot!