How to Add Twitter OAuth Login in Website with PHP

Gone are the days when a user needs to fill the registration form, receive an email with an activation link and then activate their account. Popular social sites like Google, LinkedIn, Twitter provides their APIs using which user can log in to your website. This saves time for both users and developers and adds a better user experience. In this article, we study adding Twitter OAuth login on a website using PHP.

You may also want to read our articles:

Get Twitter Credentials

Whenever you want to integrate social login APIs first thing needs to get your credentials. On Twitter also you first need to create your application and then you will get your keys.

  • Go to Twitter developer account and create a new application.
  • Fill out any required fields such as the application name and description.
  • Put your website domain in the Website field.
  • Set the callback URL as YOUR_DOMAIN_URL/index.php.
  • Once you have registered, copy and past the created application credentials (Consumer Key and Secret) which requires in next steps.

Twitter Credentials

As we set callback URL to index.php file, we will handle redirect code in this file. You can set the different URL as per your requirements.

HybridAuth Library Installation

We always recommend our users to use HybridAuth which is an open-source social sign-on PHP library. Using this library, one can easily add social login to their application. You don’t need to scratch your head against the API documentation. Just grab your credentials and HybridAuth does the rest for you.

Hybridauth Social Login PHP Library

Create a composer.json file and add the below code in it.

{
    "require" : {
        "hybridauth/hybridauth" : "~3.0"
    }
}

Next, run the command below which install the HybidAuth library in your application.

composer install

We have copied Twitter credentials which will store in the configuration file. Create a config.php file and write the code below in it.

<?php
require_once 'vendor/autoload.php';
 
$config = [
    'callback' => 'YOUR_DOMAIN_URL/index.php',
    'keys'     => ['key' => 'TWITTER_CONSUMER_API_KEY', 'secret' => 'TWITTER_CONSUMER_API_SECRET_KEY'],
    'authorize' => true
];
 
$adapter = new Hybridauth\Provider\Twitter( $config );

Twitter OAuth Login in Website

We all set with the creating application on Twitter and basic configurations. Now, we can go ahead and add login with Twitter functionality on the website. Create a file called index.php and place the below code in it.

<?php
require_once 'config.php';

try {
    $adapter->authenticate();
    $userProfile = $adapter->getUserProfile();
    print_r($userProfile);
    echo '<a href="logout.php">Logout</a>';
}
catch( Exception $e ){
    echo $e->getMessage() ;
}

Run the URL YOUR_DOMAIN_URL/index.php in the browser, the code in the file first checks whether the user is authenticated with ‘Twitter’ or not. If not, then it redirects to the Twitter log in page. The user has to log in with their Twitter account and then it will redirect to the index.php again. Once user comes to login page we are getting user profile by using the method getUserProfile().

Finally, in the logout.php we should disconnect the adapter(Twitter) which automatically logs the user out of the application.

<?php
require_once 'config.php';
 
try {
    if ($adapter->isConnected()) {
        $adapter->disconnect();
        echo 'Logged out the user';
        echo '<p><a href="index.php">Login</a></p>';
    }
}
catch( Exception $e ){
    echo $e->getMessage() ;
}

That’s it! It is that simple. We hope you got to know about adding Twitter OAuth login on the website with PHP. Please share your thoughts or suggestions in the comment section below.

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

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