Update Your Linux Box Status to Twitter Using oAuth
Twitter recently announced (source) that they will be shutting down basic authentication on the Twitter API. So all applications, need to switch to using Oauth, which means the earlier article on Update Your Linux Box Status to Twitter will not work for the readers.
This article aims to fill up the gap by implementing oAuth using PHP . So before starting, lets check the perquisites for proper working of this article .
System Perquisites:
A Linux Box PHP5 with php-curl ( php5-curl ) installed A working Internet Connection
The first step involve in having a oAuth Twitter app is to register your application with twitter.
Steps for Registering Twitter Application :
- Visit http://dev.twitter.com
- Click on Register Your App Link
- Fill the Form with App Name, Description, Website link ( if you dont have any link put your profile link instead ), Organization
- Select Application Type: “Client”
- Select Default Access type: “Read & Write”
- Read/listen captcha and click on register application
- Accept the Terms and Conditions of Twitter API usage
Next step involve extracting information regarding your application which will be needed by PHP script. Here are steps for extracting information :
- visit http://dev.twitter.com/apps
- Select your application
- Copy/Note Down the following informationa) Consumer keyb) Consumer secret
- Now click on “My Access Token” link
- Copy/Note Down the following informationa) Access Token (oauth_token)b) Access Token Secret (oauth_token_secret)
Now users need to download the oAuth PHP library ( We have used @abraham’s Twitter OAuth library for PHP )
Download @abraham’s Twitter OAuth library for PHP from here
Extract twitteroauth/ directory to a specified folder
now create a tweet.php file with following code, in the specified folder
<?php
require_once('twitteroauth/twitteroauth.php');
$ckey = "Place_Your_Consumer_key_here";
$csecret = "Place_Your_Consumer_Secret_here";
$oauthtoken = "Place_Your_oauth_token_here";
$oauthtokensecret = "Place_Your_oauth_token_Secret_here ";
$message = exec('uptime'); //Command to be executed
$connection = new TwitterOAuth ($ckey ,$csecret , $ oauthtoken , $ oauthtokensecret );
$connection->post('statuses/update', array('status' => $message));
?>Replace the variables above and save the file
Now test the script by passing following command
php /path/to/script/tweet.phpIf everything is running fine, then users can copy paste the command in cronjob. After this, the machine will tweet its uptime regularly
Please let me know if you are facing any problem with this script by leaving your comment(s)






September 3rd, 2010 at 4:19 pm
Thanks! Finally my Nagios Notifications work again, after Twitter disabled oAuth!
September 3rd, 2010 at 4:23 pm
I mean they disabled Basic-Auth. Sorry for that. But thanks again
September 3rd, 2010 at 4:30 pm
Cool
Glad to know it helped you
September 15th, 2010 at 4:51 am
I have this error
PHP Fatal error: Call to undefined function json_decode() in /root/Desktop/abraham-twitteroauth-76446fa/twitteroauth/twitteroauth.php on line 159
Any suggestions man?
September 15th, 2010 at 5:04 am
I think you need to install PHP XML-RPC package
September 15th, 2010 at 12:59 pm
Already installed still the same error! I found this!
http://blog.silviasoft.com/2009/11/19/fatal-error-call-to-undefined-function-json_decode-php-version/
I’m not sure where is the generate.php I’m a php noob, also I want to try this for Nagios notifications coz I haven’t found a method that works with th new OAuth api update :yet (
September 15th, 2010 at 1:24 pm
hey if you don’t mind can you mail me whole script ( after removing your consumer key and secrets ) so that I can check where the problem is
mail me at kunalgautam.mjh at gmail dot com