Last Updated on April 27, 2021 by amitoverflow1
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
Sign in with Twitter PHP demo
Download the source code :
You can download the “Sign in with twitter for PHP” source code as zip file from here and upload it to your webserver using any web based ftp client.
The only one file you need to modify is the index.php .
Open it in your editor and change the Consumer key, Consumer sercret and callback URL with your own .
Update these three lines and you will be ready to use Twitter login on your php website.
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
- 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/login 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/login/callback.php to this field.
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/login” on your root directory. All the source files are located inside the twitter directory.
The only one file that you will need to modifyis index.php. You can find the index.php file in Twitter/login folder of your twitter source code.
Go to /twitter/login on the ftp editor and open index.php for writing.
Index.php contains your consumer token/secret and callback URL
define('CONSUMER_KEY','Add_your_key_here'); define('CONSUMER_SECRET','add_secret_here'); define('OAUTH_CALLBACK', 'https://example.com/twitter/login/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.
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) .