Few days back I had connected labs.9lessons application to twitter API via OAuth (open protocal secure authorization). In this post I want to explain how to store twitter oauth_token and secret values in to database and how to update status with your own web application.

Download Script
Live DemoDatabase
Here storing twitter authorization token keys. MySQL users table columns uid, uname, passcode, oauth_token, oauth_token_secret
CREATE TABLE users
(
uid INT PRIMARY KEY AUTO_INCREMENT,
uname VARCHAR(50) UNIQUE,
passcode VARCHAR(50),
oauth_token VARCHAR(90),
oauth_token_secret VARCHAR(90)
);
(
uid INT PRIMARY KEY AUTO_INCREMENT,
uname VARCHAR(50) UNIQUE,
passcode VARCHAR(50),
oauth_token VARCHAR(90),
oauth_token_secret VARCHAR(90)
);
How it works at labs.9lessons
Take a look at this video.
confirm.php
Callback URL - return to after successfully authentication?
include 'db.php';
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'secret.php';
$Twitter = new EpiTwitter($consumerKey, $consumerSecret);
if(isset($_GET['oauth_token']) || (isset($_COOKIE['oauth_token']) && isset($_COOKIE['oauth_token_secret'])))
{
// Twitter user accepted access
if( !isset($_COOKIE['oauth_token']) || !isset($_COOKIE['oauth_token_secret']) )
{
// user comes from twitter
$Twitter->setToken($_GET['oauth_token']);
$token = $Twitter->getAccessToken();
setcookie('oauth_token', $token->oauth_token);
setcookie('oauth_token_secret', $token->oauth_token_secret);
$Twitter->setToken($token->oauth_token, $token->oauth_token_secret);
}
else
{
// user switched pages and came back or got here directly, stilled logged in
$Twitter->
setToken($_COOKIE['oauth_token'],$_COOKIE['oauth_token_secret']);
$user= $Twitter->get_accountVerify_credentials();
$oauth_token=$_COOKIE['oauth_token'];
$oauth_token_secret=$_COOKIE['oauth_token_secret'];
// Storing token keys
$sql=mysql_query("update users SET oauth_token='$oauth_token',
oauth_token_secret='$oauth_token_secret' where username='$user_session'");
header('Location: abc.php'); //Redirecting Page
}
echo "Please re-connect once again <a href=''start.php">click here</a>";
}
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'secret.php';
$Twitter = new EpiTwitter($consumerKey, $consumerSecret);
if(isset($_GET['oauth_token']) || (isset($_COOKIE['oauth_token']) && isset($_COOKIE['oauth_token_secret'])))
{
// Twitter user accepted access
if( !isset($_COOKIE['oauth_token']) || !isset($_COOKIE['oauth_token_secret']) )
{
// user comes from twitter
$Twitter->setToken($_GET['oauth_token']);
$token = $Twitter->getAccessToken();
setcookie('oauth_token', $token->oauth_token);
setcookie('oauth_token_secret', $token->oauth_token_secret);
$Twitter->setToken($token->oauth_token, $token->oauth_token_secret);
}
else
{
// user switched pages and came back or got here directly, stilled logged in
$Twitter->
setToken($_COOKIE['oauth_token'],$_COOKIE['oauth_token_secret']);
$user= $Twitter->get_accountVerify_credentials();
$oauth_token=$_COOKIE['oauth_token'];
$oauth_token_secret=$_COOKIE['oauth_token_secret'];
// Storing token keys
$sql=mysql_query("update users SET oauth_token='$oauth_token',
oauth_token_secret='$oauth_token_secret' where username='$user_session'");
header('Location: abc.php'); //Redirecting Page
}
echo "Please re-connect once again <a href=''start.php">click here</a>";
}
This below status(tweet) updated with labs.9lessons.info

Here $message="Your Status update";
include 'db.php';
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'secret.php';
$tw_sql=mysql_query("select oauth_token,oauth_token_secret from users where user='$user_session'");
$row=mysql_fetch_array($tw_sql);
$oauth_token=$row['oauth_token'];
$oauth_token_secret=$row['oauth_token_secret'];
if(strlen($oauth_token)>0 || strlen($oauth_token_secret)>0 )
{
$Twitter = new EpiTwitter($consumerKey, $consumerSecret);
$Twitter->setToken($oauth_token,$oauth_token_secret);
//$message Status update
$status=$Twitter->post_statusesUpdate(array('status' => $message));
$status->response;
}
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'secret.php';
$tw_sql=mysql_query("select oauth_token,oauth_token_secret from users where user='$user_session'");
$row=mysql_fetch_array($tw_sql);
$oauth_token=$row['oauth_token'];
$oauth_token_secret=$row['oauth_token_secret'];
if(strlen($oauth_token)>0 || strlen($oauth_token_secret)>0 )
{
$Twitter = new EpiTwitter($consumerKey, $consumerSecret);
$Twitter->setToken($oauth_token,$oauth_token_secret);
//$message Status update
$status=$Twitter->post_statusesUpdate(array('status' => $message));
$status->response;
}
If any queries just ask me at facebook.com/srinivas.tamada
db.php
PHP database configuration file
<?php
$mysql_hostname = "Host name";
$mysql_user = "UserName";
$mysql_password = "Password";
$mysql_database = "Database Name";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>
$mysql_hostname = "Host name";
$mysql_user = "UserName";
$mysql_password = "Password";
$mysql_database = "Database Name";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>
Why OAuth
At the time of developing I found some better Twitter API tutorials. Very nice explanation with illustrations
- OAuth FAQ - Twitter Wiki
- Writing your first Twitter application with OAuth
- Twitter OAuth Sign In Tutorial
- How to quickly integrate with Twitter’s OAuth API using PHP
Book : Twitter API: Up and Running: Learn How to Build Applications with the Twitter API










![srinivas.tamada[at]gmail.com](http://lh4.ggpht.com/_N9kpbq3FL74/SgknVlmcrAI/AAAAAAAABns/OhTsS0WO_Sw/gtalk.png)






Thanks, awesome!
Very nice, as always.
Reequest to be the Facebook connect tutorial. I heard it boosts registration by 15%! A lot of people sign up using Facebook connect if it's on their profile. :-P
Nonetheless, you're amazing. I am gonna subscribe to you in every way possible! Twitter, Facebook, this, RSS, etc.
Keep rocking,
UrbanTwitch
Again a useful post Sri congrats , which library you are using for it??
Awesome, thanks a lot.. I will try.. btw, u r using IE? cant believe..
I want to use Facebook connect thru PHP. Please provide some tutorial on that, if possible. it will be of great use.. Thanks.
Great post and very usefull,thank you!
Hi, why do not use Abraham Twitteroauth library ?
Epi*.php is better ?
An awesome tut !!!
grt :) thanks for posting this :)
Very cool. Even more so that I show up in your friends list in the youtube.
- How can I get list of (the people I follow) && (who have authorized this API) ?
Can you make an full user system based on this? I have a script called Zubee Tube wich we'll badly need this!
hi srinivas, thanks for the wondeful post.
but, unfortunately it is giving me an error.
Even in the live demo on your site getting the same error. After I click on the "Allow" green button it is taking me to the following url: http://twitter.com/oauth/authorize and getting "temporarly down" error. If I refresh I am getting the below effort.
"Woah there!
This page requires some information that was not provided. Please return to the site that sent you to this page and try again … it was probably an honest mistake."
Really thanks for this srinivas.
@Vijay
Try once again.
Wow...it actually worked. I didn't change anything I believe. But I am trying from work this time.
Thank you so much, Srinivas.
BTW, in confirm.php I don't see Insert code so added
mysql_query("insert into twitter_user(uname,oauth_token,oauth_token_secret)values('vj','$oauth_token','$oauth_token_secret')");
But, trying to make update.php to work for posting a test message.
Thank you once again :)
nice post bro i have also created one without oauth and needs no user password just the username try this out..that script is just for getting timeline of that profile check it out here
http://phpprogs.blogspot.com/2010/05/getting-twitter-homeline-using.html
@Srinivas Tamada, what do one has to do in order to keep the user's data, such that we don't have to get to twitter everytime to authenticate, just like it happens on the labs.9lessons.info section, i tried everytime i have to get to twitter for oauth authentication..
thanks for the help
I wanna backup @ebottabi ... how do you setup an local cookie for an user that has authentificated trought Twitter?
uhm...i got a error message.
"This page is no longer valid. It looks like someone already used the token information you provided. Please return to the site that sent you to this page and try again … it was probably an honest mistake."
What is it?
thank you, this is very nice tutorial. and i like it.
But I still do not understand about oauth_token and oauth_token_secret which had been entered into the database. is it the same between oauth_token with consumer_key and consumer_secret with oauth_token_secret?
thank you and so sorry if my english is bad,
:(
i use google translate to translate my language to english, i'm from indonesia. thank you..
@Hariez
consumer_key and consumer_secret is the access credentials for your App on Twitter. Where as the oauth_token and oauth_token_secret is the credentials for the twitter user that is return by Twitter after authentication.
Every time when you have to push a status update for a user, u don't need his username/password u have to use the oauth_token and oauth_token_secret.
Srinivas, Thanks a bunch. You saved me a lot of time.
Regards,
Akram