December 28, 2023 5:03 am

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.

[wpdm_file id=12]DEMO

The script contains two folders called oauth and images with PHP files.


– OAuth.php // Twitter OAUTH library


callback.php //Call back page create permanent credentials

config.php // Configuration

connect.php // Check application credentials

destroysessions.php // Erase all old sessions // 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

 * A single location to store configuration.
define('CONSUMER_KEY', 'Consumer key');
define('CONSUMER_SECRET', 'Consumer secret');
define('OAUTH_CALLBACK', '');


Shows a button for twitter authorization if not authorized.

 * User has successfully authenticated with Twitter. Access tokens saved to session and DB.

/* Load required lib files. */

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


Redirect you to redirect.php generate temporary credentials oauth access token and oauth token secret


/* Start session and load library. */

$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); 
    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.


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


Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.

Tutorial Categories:

18 responses to “Login with Twitter OAuth in PHP V 1.1 Updated”

  1. Karan Banerjee says:

    this doesn’t gets email id of the twitter user like facebook. how can we get twitter users email id?

    • Huzoor Bux says:

      The user’s email address can not be retrieved via the API. This is a deliberate design decision by the API team.

  2. srinuchilukuri says:

    Hi Sir ,
    Nice Article
    im using and tweet with text content working fine how to tweet with image
    plz replay me…

    Thanks in advance

  3. rasez says:

    when i click on sign in button this error show:
    Could not connect to Twitter. Refresh the page or try again later.

  4. ashish says:

    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

    • Mac Senour says:

      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,

  5. Amrendra Singh says:

    Helped me in my issue for fetching user data. Thanks 🙂

  6. How can i see name – email with session .. this is with array .. I dont wanna array I wanna name – email bla bla bla

  7. I cannot download the source code even I have subscribed

  8. Mac Senour says:

    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.



    • huzoorbux says:

      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.

      • Mac Senour says:

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


  9. Joshua Sedore says:

    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!

  10. Paresh says:

    Do i require to have read/write permissions to get account/verify_credentials?

  11. Ӊello mates, nice piece of writing ɑnd fastidious urging commented at tҺіs plaсe, I am genuinely enjoying Ьy theѕе.

  12. dev_ronak says:

    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…

  13. Jainendra Pal says:

    Undefined index: oauth_token in C:wampwwwliveoauthtwitteroauth.php on line 80

Leave a Reply

Your email address will not be published. Required fields are marked *