Sign in with twitter for php websites | Demo and source zip code| amitoverflow blog

Sign in with twitter for PHP websites

With Twitter APIs you can log twitter users to your website. This helps reduce the signup process and enables twitter functionalities such as “post tweets” , “follow users” directly from your own web server. You can put a “login with twitter” button on your website to securely log twitter users to your website.

In this post, we will learn how to use twitter login for a PHP websites.

With the source code provided on this post you will be able to use a “sign in with twitter” button on your website that will allow your twitter users to log in to your site using their twitter credentials.

See the demo

Download the source code :

You can download the “Sign in with twitter for PHP” source code as zip file from here .

What you need to get started

  • PHP (5.4 or above)
  • Custom domain with SSL cert.
  • Twitter API App
  • Twitter oauth source code

PHP server :Your php version must be 5.4 or greater to use twitter login.

Custom domain with SSL cert :In order to make secure API calls from your website to twitter, you need an SSL certificate for your domain. Make sure your URLs use https perfix.

Twitter API app : you need a twitter developer API Api to get Consumer token and secret. See instructions bellow on how to create one.

Twitter oauth source code : Download and the zip source code file . Upload and unzip it to your web directory.

Create a twitter developer API app

API app will provide you “consumer token” and “consumer secret” that will be used to verify your app on the twitter oauth code.

In order to be able to create an application on the twitter developer platform, first of all you need to submit an developer application and once its approved you can then create your first App.

To create your first app , go to https://developer.twitter.com/en/apps and click on the crete a new app button.

Creating an app on the developer platform is easy and it only takes a few seconds to make the app Live.

Fill up all the required app details

Twitter api app details
  • App name : The name of your Application ie: “Demo app”.
  • App description : The short description of your Application in 10 to 200 characters. Write a short description about your application.
  • Website URL : Add the URL of your website ie https://exmple.com/ . You can add the URL on which you are hosting the twitter oauth code. Add https://yoursitename.com/twitter if you are using the oauth source code provided on this post.
  • Click on the “enable sign in with twitter checkbox. If you do not check mark this then your application will not be able to authorize twitter login requests.
  • Callback URL : callback URL is the URL where twitter users will get redirected to after completing the twitter login or after pressing the “Authorize app” button.

    If you are using the source code provided by us then add http://yoursitename.com/twitter/callback.php to this field.

Twitter api app
The last three fiels except the “Tell us how your app will be used” are optional fields you can leave them blank.
You need to write a short description about how you will use the application. Example : Write “I want use this for a simple login” . This field is required and only visible to twitter employees.

Finally click on the create button. This will create your app and you can now copy the “customer token and secret ” from the settings. You need these app token and secret to update the config.php file of the source code.

Download the Twitter oauth source code

Now that you have app token and secret your next step is to download the source code zip file from this link. Upload and unzip it to your document root folder. The zip file will create a new folder named “twitter” on your root directory. All the source files are located inside the twitter directory.

The only two files that you will need to modify are config.php and callback.php . You can find the config.php file in files folder of your twitter source code.
Go to /twitter/files on the ftp editor and open config.php for writing.

config.php contains your consumer token/secret and callback URL

<?php
define('CONSUMER_KEY','Add_your_key_here');
define('CONSUMER_SECRET','add_secret_here'); 
define('OAUTH_CALLBACK', 'https://example.com/twitter/callback.php');

Replace define values with your own values .
You can find your application consumer key and secret on Keys and token menu of your Twitter API app.

Twitter api key and secret

callback.php
You can find this file in home folder of your twitter sign in code. The location of the callback.php file is /twitter/callback.php .

Here is how it looks :

<?php
session_start();
require 'autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
if (isset($_REQUEST['oauth_verifier'], $_REQUEST['oauth_token']) && $_REQUEST['oauth_token'] == $_SESSION['oauth_token']) {			   //In project use this session to change login header after successful login 
	$request_token = [];
	$request_token['oauth_token'] = $_SESSION['oauth_token'];
	$request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret'];
	$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']);
	$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));
	$_SESSION['access_token'] = $access_token;
	// redirect user back to index page
	header("location:/twitter_demo/twitter/index.php");
}

The line that needs to be updated is

// redirect user back to index page
	header("location:/twitter/index.php");
}

Change the header location to the location of your callback.php file. If you are using the source code provided by us then you will not need to do anything as the location is already set in the source file. However if you want to use your own directory for hosting the project files then it is important to set the location manually.


That’s it.
Now you can go to “yoursite.com/twitter/” to check your sign in with twitter demo.

Twitter user properties

User properties are used to display user informations on a web page. To display profile informations of logged in twitter users you can use the following properties provided by Twitter API.

echo "User name ".$user->name;

The php code above will print twitter name of the user who is logged in to your page using Twitter API.

A list of twitter user properties:

  • name – the full name of the user
  • screen_name – screen name of the twitter user ie. “foobar” without the leading “@”.
  • profile_image_url_https – URL of the twitter user profile pic with secure https scheme.
  • statuses_count – the number of tweets posted by the user.
  • followers_count : follwers count of the user.
  • friends_count – following count of the user.

To see a complete list of Twitter user properties, you can print the $user variable (after authorising the user) .

print_r($user);

Posts Tagged with…

Amit

Amit

web developer, designer and an active contributor on stackoverflow.com.

Reader Comments

  1. starve io mods 2020

    I absolutely love your blog and find most of
    your post’s to be precisely what I’m looking for. Would you offer guest writers to write content for
    you? I wouldn’t mind creating a post or elaborating on a lot
    of the subjects you write concerning here. Again, awesome web log!

Write a Comment

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