Loading Searchbox
9lessons programming blog logo
Friday, February 26, 2010

Connect Twitter API with OAuth using PHP.

21 comments
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.

Connect Twitter API with OAuth using PHP.

Download Script     Live Demo

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



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>";
}

This below status(tweet) updated with labs.9lessons.info
update.php
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;
}

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

Why OAuth
At the time of developing I found some better Twitter API tutorials. Very nice explanation with illustrations
Other technologies code here Oauth.net Code

Book : Twitter API: Up and Running: Learn How to Build Applications with the Twitter API
Sponsored Links

Recent Posts

Share this post

Subscribe to my feeds

Subscribe
Comments
21 comments
victor said...

Thanks, awesome!

UrbanTwitch said...

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

Motyar said...

Again a useful post Sri congrats , which library you are using for it??

Karthik said...

Awesome, thanks a lot.. I will try.. btw, u r using IE? cant believe..

Karthik said...

I want to use Facebook connect thru PHP. Please provide some tutorial on that, if possible. it will be of great use.. Thanks.

Anonymous said...

Great post and very usefull,thank you!

sid said...

Hi, why do not use Abraham Twitteroauth library ?
Epi*.php is better ?

Najeeb Puthiyallam said...

An awesome tut !!!

Siddharth said...

grt :) thanks for posting this :)

organicit said...

Very cool. Even more so that I show up in your friends list in the youtube.

shivani said...

- How can I get list of (the people I follow) && (who have authorized this API) ?

Marius said...

Can you make an full user system based on this? I have a script called Zubee Tube wich we'll badly need this!

Vijay said...

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.

Srinivas Tamada said...

@Vijay

Try once again.

Vijay said...

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

kuldeep singh sadioura said...

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

ebottabi said...

@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

Marius Patrascu said...

I wanna backup @ebottabi ... how do you setup an local cookie for an user that has authentificated trought Twitter?

Anonymous said...

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?

Hariez said...

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

Akram Quraishi said...

@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

Post a Comment

Orkut | FacebookAbout Me

Subscribe now!Feeds RSS

Subscribe now!Recent Posts

Subscribe now!Categories

Subscribe now!Comments

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Join into my community

Labs ProfileRelease

My ProfileTwitter