OAuth PHP Twitter Class
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.
June 22nd, 2010 at 15:03
[…] *So* scripted « Geeking out with diagrams in ASCII OAuth PHP Twitter Class […]
March 1st, 2011 at 13:29
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”.
March 1st, 2011 at 13:34
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
March 1st, 2011 at 14:57
Well my test.php lookes like this.
tweet(“Hello World”);
?>
March 1st, 2011 at 15:00
Ok test.php looked like this:
http://paste.bradleygill.com/index.php?paste_id=214756
March 1st, 2011 at 15:10
run:
touch /home/xyz/.twitter_tokens
and try again. You need OAuth installed and then hopefully you’ll get a bit further
March 1st, 2011 at 15:17
My mistake, that die() statement totally kills the setup for this. I’ve updated the class. Try it now.
March 1st, 2011 at 20:45
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
March 1st, 2011 at 21:23
Seems unnecessary, but did you also restart Apache?
sudo /etc/init.d/apache2 restart
or
sudo apache2ctl restart