Login with Twitter OAuth in PHP V 1.1 Updated
Today I have worked on Twitter oAuth and faced many problems how to get access token for offline access to my application etc. In this tutorial I will show you how to authorize application and add tweets, remove tweets, follow friend and unfollow friend.
Twitter oAuth V 1.1 Updated.
The script contains two folders called oauth and images with PHP files.
oauth
– OAuth.php // Twitter OAUTH library
images
callback.php //Call back page create permanent credentials
config.php // Configuration
connect.php // Check application credentials
destroysessions.php // Erase all old sessions
html.inc // html design view
index.php // Main index file show data
redirect.php // Redirect to twitter for authorization
Create App on twitter click here
PHP Code
Edit config.php
<?php /** * A single location to store configuration. */ define('CONSUMER_KEY', 'Consumer key'); define('CONSUMER_SECRET', 'Consumer secret'); define('OAUTH_CALLBACK', 'http://demo.phpgang.com/twitter_login_oauth/callback.php');
Index.php
Shows a button for twitter authorization if not authorized.
<?php /** * User has successfully authenticated with Twitter. Access tokens saved to session and DB. */ /* Load required lib files. */ session_start(); require_once('oauth/twitteroauth.php'); require_once('config.php'); if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) { header('Location: ./destroysessions.php'); } $access_token = $_SESSION['access_token']; $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); /* If method is set change API call made. Test is called by default. */ $content = $connection->get('account/verify_credentials'); /* Some example calls */ //$connection->get('users/show', array('screen_name' => 'PHPGang')); //$connection->post('statuses/update', array('status' => "PHP Gang Testing...")); //$connection->post('statuses/destroy', array('id' => 533297770)); //$connection->post('friendships/create', array('id' => 9322192)); //$connection->post('friendships/destroy', array('id' => 9436992)); include('html.inc');
Redirect you to redirect.php generate temporary credentials oauth access token and oauth token secret
<?php /* Start session and load library. */ session_start(); require_once('oauth/twitteroauth.php'); require_once('config.php'); $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);// Key and Sec $request_token = $connection->getRequestToken(OAUTH_CALLBACK);// Retrieve Temporary credentials. $_SESSION['oauth_token'] = $token = $request_token['oauth_token']; $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; switch ($connection->http_code) { case 200: $url = $connection->getAuthorizeURL($token); // Redirect to authorize page. header('Location: ' . $url); break; default: echo 'Could not connect to Twitter. Refresh the page or try again later.'; }
Authorize on twitter and return on callback.php and get permanent credentials oauth access token and oauth token secret you can save them in database and update status any time you want.
Summery
When you authorize application it will return temporary credentials oauth access token, oauth token secret and a oauth verifier with verifier you will get new oauth access token and oauth token secret with this method.
$access_token = $connection->getAccessToken('oauth_verifier');
Now you will be able to get
User credentials
$content = $connection->get('account/verify_credentials');
Show user Information
$connection->get('users/show', array('screen_name' => 'PHPGang'));
Post new tweet
$connection->post('statuses/update', array('status' => "PHP Gang Testing..."));
Delete tweet
$connection->post('statuses/destroy', array('id' => 533297770));
Follow a friend
$connection->post('friendships/create', array('id' => 9322192));
Unfollow a friend
$connection->post('friendships/destroy', array('id' => 9436992));
Tutorial Categories:
this doesn’t gets email id of the twitter user like facebook. how can we get twitter users email id?
The user’s email address can not be retrieved via the API. This is a deliberate design decision by the API team.
Hi Sir ,
Nice Article
im using and tweet with text content working fine how to tweet with image
plz replay me…
Thanks in advance
Thanks @srinuchilukuri:disqus there will be an update soon.
when i click on sign in button this error show:
Could not connect to Twitter. Refresh the page or try again later.
when i click on sign in button this error show:
Could not connect to Twitter. Refresh the page or try again late
same problem like rasez
please help me to solve it
Assuming you’ve not solved this…
Check your keys, public and secret. I had this issue and found that there was an added space at the end of my codes. I removed this space, and poof, no issues. Check also the error code with an “echo”. I did and found the error was 401, no access given.
Hope that helps,
Mac
Helped me in my issue for fetching user data. Thanks 🙂
How can i see name – email with session .. this is with array .. I dont wanna array I wanna name – email bla bla bla
I cannot download the source code even I have subscribed
I have this on my site but there is a single issue. When a returning user loads my site, attempts to login, he is asked if he wants to authorize the app again. This is confusing since he already authorized it a few days prior on his last visit.
What procedure do you suggest that pulls the authorization from DB and let’s Twitter know the user is signed in.
Basically, once he is authorized, he needs to only login the next time not re-authorize. Your example doesn’t do that.
Suggestions?
Mac
Twitter gives this screen again and again not remember users access like Facebook I have tried it many times but no luck if you find some way please share with us.
I’m still looking, but I think the hint is given here:
“Authorize on twitter and return on callback.php and get permanent credentials oauth access token and oauth token secret you can save them in database and update status any time you want.”
Mac
Help! It won’t let me download! I have subscribed but it doesnt seem to recognize my email and therefore wont let me download the script!
Do i require to have read/write permissions to get account/verify_credentials?
Ӊello mates, nice piece of writing ɑnd fastidious urging commented at tҺіs plaсe, I am genuinely enjoying Ьy theѕе.
the result set is in array format.
i want to display the same in string format ex. Username or screen_name etc…
can u pls help…
Undefined index: oauth_token in C:wampwwwliveoauthtwitteroauth.php on line 80