Twitter OAuth Integration using PHP
anoopsachari | Apr 16, 2010 | Comments 25
This article would help you to integrate twitter using OAuth . I had to implement twitter integration using OAuth but couldn’t find a good tutorial but did it successfully after long research. Thus I am sharing you step by step explanation on it .
Step 1 : Login to your twitter account and register a new application .
It’s under Settings > Connections > Developers
OR
Follow the link http://twitter.com/apps
Register you application using application name and a logo.
Select Application Type : Browser.
Default Access Type : Read & Write.
Use Twitter for Login : Activate the check-box.
Set Callback URL to http://www.myapplicationname.com/index.php .
The application redirects after successful authentication to callback URL specified.
After registering the application you would get a Consumer Key and Consumer Secret key as shown below .
Step 2 : Download twitter client library from here
Step 3 : Edit secret.php with the new Consumer key and Consumer secret .
Step 4 : Create a file named index.php and add the following code.
Source code of : index.php
<?php session_start();
include 'lib/EpiCurl.php';
include 'lib/EpiOAuth.php';
include 'lib/EpiTwitter.php';
include 'lib/secret.php';
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
$oauth_token = $_GET['oauth_token'];
if($oauth_token == '')
{
$url = $twitterObj->getAuthorizationUrl();
echo "<div style='width:200px;margin-left:auto;margin-right:auto'>";
echo "<a href='$url'>Sign In with Twitter</a>";
echo "</div>";
}
else
{
$twitterObj->setToken($_GET['oauth_token']);
$token = $twitterObj->getAccessToken();
$twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);
$_SESSION['ot'] = $token->oauth_token;
$_SESSION['ots'] = $token->oauth_token_secret;
$twitterInfo= $twitterObj->get_accountVerify_credentials();
$twitterInfo->response;
$username = $twitterInfo->screen_name;
$profilepic = $twitterInfo->profile_image_url;
echo "Twitter Handle : ".$username; echo '<br />';
echo "Profile Picture : <img src='$profilepic' /> ";
echo "<br /><br />";
echo "<label>Update Twitter Timeline</label>";
echo "<form method='post' action='index.php'>";
echo "<input type='text' name='tweet' id='tweet' />";
echo "<input type='submit' value='Tweet' name='submit' id='submit' />";
echo "</form>";
}
if(isset($_POST['submit']))
{
$msg = $_REQUEST['tweet'];
$twitterObj->setToken($_SESSION['ot'], $_SESSION['ots']);
$update_status = $twitterObj->post_statusesUpdate(array('status' => $msg));
$temp = $update_status->response;
echo "<div align='center'>Updated your Timeline Successfully .</div>";
}
?>
You are done !
I have packed all basic features into a single file you can use it in separate file if needed .
Hope the tutorial helped you !
You can download the complete application code here
Leave a comment if u need any help .
About the Author: a holistic web developer , movie buff and technical blogger from queen of arabian sea.











Thanks !
i test your code, is great !
thanks a lot.
MP
Thank you Mariano .
Olá como faço para postar alguma mensagem no twitter do meu usuario quando ele enviar alguma informação ?
Preciso armazenar algum retorno do twitter no banco de dados quando ele aceitar minha integração ?
Obriagdo !
Sup
Thanks a mil man, epic code helped me a lot no hassles .
Great article! Keep this coming
Thank You @Logesh
Thanks a lot … @Logesh
Hi Anoop,
Thanks for the update Buddy!
I really needed this! especially with the pics!
hi, im using this but i get an error:
Undefined index: port in EpiOAuth.php
Undefined variable: args EpiTwitter.php
A session had already been started – ignoring session_start() in /EpiOAuth.php
so what you suggenst???
Ji Anoop,
thanks for this, very well explained, had to retweet you now
Hi, I see your application very helpful I tried to use it but I got this errors
Notice: A session had already been started – ignoring session_start() in /…/lib/EpiOAuth.php on line 2
Notice: Undefined index: oauth_token in /…/index.php on line 9
Notice: Undefined index: port in /…/EpiOAuth.php on line 146
Fatal error: Call to undefined function hash_hmac() in /…/EpiOAuth.php on line 195
would you help me please!!!!
@carlos . i did not find error executing the code. i think you have some error creating session.
I fixed some errors but still got this one
Notice: Undefined index: oauth_token in C:\…\index.php on line 9
and I had another trouble, and is that the application seems to be in a cycle, I mean. I click on the buttom sign on twitter, after that, go to my applicattion, click on allow and finally it must be on the message page but don´t it came back to the button sign in on twitter why this happen???
Forget I solved it, but now this happens
Fatal error: Call to undefined function json_decode() in /…/tweets/lib/EpiTwitter.php on line 95
How can I put the json on the code???
@carol . update the php version . old php versions does not support json .
could you pls post how you solved the other issues .
Well the oauth_token issue happens cause I pass a variable in $_POST and it is $_GET, the port was for an undeclared array();
HOw can I do it without touching the server???, Cause I can´t
oh! u dont have access to your server to update php @carols .
I have a cuestion Why always the application ask for permission…. everytime I run the application twitter shows me the allow, deny page If I allow it once! why I have to do it again???..
@carols a good qns . use getAuthenticateUrl(); instead of getAuthorizationUrl(); also download the latest updated lib from http://www.jaisenmathai.com/articles/twitter-php-oauth.html
I need your help to integrate Twitter Oauth to my website, please email me your cost and time to do this job. Thanks
Excellent article indeed. My teacher has been waiting for this content.
Nice/ Handy / Clear code
hi,
i have created the twitter application.
i am getting the errors the following errors
Notice: Undefined index: oauth_token in D:\wamp\www\twitter\index.php on line 8
Notice: Undefined index: port in D:\wamp\www\twitter\lib\EpiOAuth.php on line 146
Please help me.
Hi Anoop Achari,
Great of your article with helpful suggestion.
I liked your article and explanation.
I am working as a PHP Web developer.
If i have any technical doubt can contact through mail.
Hi Anoop,
plz can u provide some help for ‘Twitter OAuth using Javascript’? Specially on the following issues:
1. When we want to use our own button for Login and not the regular ‘Sign in with Twitter’ button provided
2. Sign Out functionality
3. When we want to retrieve user info from twitter like email (which is not currently allowed directly through the API)