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 provide their APIs through which users 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, you need to get your API credentials. On Twitter, you first need to create your application and then you will get your keys.

  • Go to the Twitter developer account and create a new application.
  • Fill out the 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 the application credentials (Consumer Key and Secret) which require in the next steps.
Twitter Credentials

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

Configure HybridAuth Library

In order to integrate social login, I always recommend using 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.

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

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

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

composer install

Create a config.php file and write the code below in it. We are also adding Twitter credentials in the configuration file.

<?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 are done with all basic configurations. Now, we can go ahead and add login with Twitter functionality on the website. Create a file called index.php and add the below code to 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, it redirects to the login page of Twitter. The user has to complete their authentication. After authentication, the user comes back to the index.php. After this, we can get user’s information 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! I hope you got to know about adding a 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.