Twitter, PHP oAuth: update status

Written by on June 7, 2010 in General - 67 Comments

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

About the Author

Greg Winiarski is a freelance PHP and JavaScript programmer. He specializes in web applications and WordPress development.

67 Comments on "Twitter, PHP oAuth: update status"

  1. Sean Torry June 11, 2010 at 9:17 am ·

    Great writup here, very strong advice. I like your TwitterOAuth library and will continue reading! Thanks!

  2. tuning June 14, 2010 at 12:48 am ·

    tnkss very god admin.

  3. Zee June 21, 2010 at 3:54 pm ·

    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?

  4. Greg Winiarski June 22, 2010 at 4:15 pm ·

    Sure, what error are you getting?

  5. Justin Streuper June 26, 2010 at 2:14 pm ·

    It doesn’t work, I get a blank?

  6. Greg Winiarski June 28, 2010 at 7:43 pm ·

    Check your Twitter status rather then script output. It doesn’t print anything on the screen it just updates your profile status.

  7. Manx July 5, 2010 at 3:55 pm ·

    Very helpful info, saved my time! Thanks!

  8. Paolo Frattini August 3, 2010 at 6:23 pm ·

    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 >:(

  9. Chris August 8, 2010 at 12:32 am ·

    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.

  10. Rau; August 9, 2010 at 2:40 pm ·

    Thank you very much Greg Winiarski, I was looking for this….

  11. bill August 13, 2010 at 12:32 am ·

    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

  12. Paul September 2, 2010 at 4:38 am ·

    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

  13. Simon September 2, 2010 at 2:34 pm ·

    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?

  14. Simon September 2, 2010 at 3:24 pm ·

    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?

  15. Dan September 2, 2010 at 10:33 pm ·

    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

  16. Runescape Grand Exchange September 3, 2010 at 4:55 am ·

    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!

  17. Dr.Death September 5, 2010 at 1:51 pm ·

    thank you! easiest way to update twitter status from site!

  18. tim September 8, 2010 at 6:41 pm ·

    OMG. Thank you. You just saved my day. Fantastic.

  19. antoniom September 8, 2010 at 9:34 pm ·

    well, i owe you A BIG THANK YOU!
    IT WORKS!

  20. Fm September 9, 2010 at 9:12 pm ·

    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

  21. Mike September 10, 2010 at 2:52 pm ·

    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.

  22. Mike September 10, 2010 at 3:03 pm ·

    Working!

  23. Greg Tallent September 11, 2010 at 9:35 am ·

    Thanks. Works fine.

  24. Dmitry September 14, 2010 at 4:11 pm ·

    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?

  25. AndroidZoom September 16, 2010 at 1:13 pm ·

    Thank you for this tutorial. It works fine for us!

  26. AG September 17, 2010 at 3:18 am ·

    Well Done!

  27. Dmitry September 17, 2010 at 9:15 am ·

    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.

  28. shayuna September 19, 2010 at 7:39 am ·

    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.

  29. mayan calendar September 20, 2010 at 10:21 am ·

    Thanks mate, I got you integrated in about 20 minutes for my daily mayan horoscope on twitter!

  30. Polskaya September 22, 2010 at 10:40 pm ·

    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! :)

  31. Gary September 30, 2010 at 5:41 pm ·

    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!

  32. Pepe Manshon October 6, 2010 at 12:18 pm ·

    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 ?

  33. American October 7, 2010 at 3:16 pm ·

    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));

  34. American October 7, 2010 at 3:19 pm ·

    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));

    :)

  35. drinkits October 18, 2010 at 12:13 am ·

    Still no updating twitter status. Solution?

  36. Clemens October 26, 2010 at 2:24 am ·

    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?

  37. James October 26, 2010 at 11:42 am ·

    Cheers buddy, great information

  38. Popo November 3, 2010 at 11:23 am ·

    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.

  39. Adi November 11, 2010 at 2:16 am ·

    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!

  40. akash November 12, 2010 at 2:27 pm ·

    Great information Dear… Thank you so much…

  41. Adi Sandu November 13, 2010 at 8:30 pm ·

    So, Greg, any ideas for those who use Php version under 5.0?

    Thanks mate!

    Adrian

  42. Greg Winiarski November 15, 2010 at 8:11 pm ·

    @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.

  43. Mudgee Web Designer November 29, 2010 at 3:38 pm ·

    Thank you, SPOT ON !!!! I was worried I’d have to write 50 lines of code.

  44. ubinkayu December 2, 2010 at 7:08 am ·

    Why Do I Need To register :’(

  45. Elvi December 3, 2010 at 9:45 pm ·

    It only update my own status, It can’t update other people status, is this normal? =(

  46. Greg Winiarski December 5, 2010 at 10:13 am ·

    @Elvi, yes, if you want to update someone else status you need to login as that person.

  47. Kajetan December 14, 2010 at 2:25 am ·

    Thanks a lot. Exactly what I needed. Works like a charm.

  48. ayauho January 5, 2011 at 2:20 pm ·

    How to update status in twitter with PHP?

  49. deej January 6, 2011 at 12:29 pm ·

    Hi,

    This great and very simple to use, but is there a way to return if the tweet was successful or not?

    Thanks

  50. Greg Winiarski January 7, 2011 at 4:14 pm ·

    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.

Leave a Comment