OAuth PHP Twitter Class

June 22nd, 2010 by Strawp

After posting my temporary twitter unfollow script the other week I was reminded that Twitter is supposed to be dumping basic auth (sending user name and password in each request to Twitter) this month in favour of the more secure, widely adopted Open Authentication. It turned out Twitter has postponed what they have dubbed the “oauthcalypse” due to the world cup but if you haven’t already, now is the time to get rid of your neat little one-line curl scripts using basic auth (a shame I know but it’s for the best) and find an OAuth alternative in the language of your choice.

I have created a simple TwitterClient class (available under SVN) based on my previous set of basic auth and curl-using functions from a couple of years back. I have left it with my app key in their on purpose, so tweets from it will appear to come from “Strawp’s PHP lib”. Register your own app if you want and use those instead, of course. On the first use of a method you will be prompted to authenticate the class against your twitter login. The secret keys will then be stored in your home folder.

Example usage:

include( "twitter.class.php" );
$tw = new TwitterClient();
$tw->tweet( "I am using oAuth now!" );

The unfollow script has also been updated to use this class.

9 Responses to “OAuth PHP Twitter Class”

  1. Temporary Twitter unfollow script | Strawp.net Says:

    […] *So* scripted « Geeking out with diagrams in ASCII OAuth PHP Twitter Class […]

  2. scoobay Says:

    It looks really nice. But I have no clue how to get the things with the tokens right. If I am the only one who uses that app. Can I insert them on my own? (Those found under My Access Token). But what is with that file .twitter_tokens?
    I also tried to run that skript in command line mode on my home server. Of course “php5 twitter.class.php” is insufficient . But running a test file only gets me “No tokens file at /home/xyz/.twitter_tokens”.

  3. Strawp Says:

    What is the script you’re trying to run? twitter.class.php won’t do anything on its own, it’s just a class for other scripts to use to interact with Twitter, e.g. http://svn.strawp.net/scripts/unfollow

  4. scoobay Says:

    Well my test.php lookes like this.

    tweet(“Hello World”);

  5. scoobay Says:

    Ok test.php looked like this:


  6. Strawp Says:


    touch /home/xyz/.twitter_tokens

    and try again. You need OAuth installed and then hopefully you’ll get a bit further

  7. Strawp Says:

    My mistake, that die() statement totally kills the setup for this. I’ve updated the class. Try it now.

  8. scoobay Says:

    I (hopefully) installed OAuth correct*. At least phpinfo() names it as enabled. But now it says Class OAuth not found in line 98.

    *My Steps to Install OAuth:
    sudo apt-get install php5-dev
    sudo apt-get install libpcre3 libpcre3-dev
    sudo pecl install OAuth
    sudo gedit /etc/php5/apache2/php.ini //Add line extension=oauth.so

  9. Strawp Says:

    Seems unnecessary, but did you also restart Apache?

    sudo /etc/init.d/apache2 restart


    sudo apache2ctl restart