Sign in with Twitter using PHP
With millions of registered users worldwide, twitter is one of the most used social networking website on the internet, we cannot overlook its importance, it can really boost registrations rate in your site. In this tutorial, we will be using Twitter API to register users on your website.
I have created 3 PHP files for the tutorial : configuration, Login and process PHP files.
We also need Abraham William’s Twitter PHP Library, which is a widely used and known for its simplicity. I have included this library in downloadable file along with tutorial files below.
Configuration file stores your Twitter Customer key, secret and callback URL. If you haven’t created Twitter application for your website, go to Twitter developer page and create one here. Once you finish creating Twitter App, you need to get your Customer key & Secret, and replace config variables in config.php file.
Login Page (index.php) contains a login button, but you can put login button anywhere in your website. Once user clicks login, user must be redirected to process.php, from where user is sent to Twitter Auth page to obtains a request token, and again user is redirected back to process.php. On successful authorization, process.php sets details in session variables which will be used later in other pages to make GET/POST requests.
Trick is very simple, if this session is not set login button must be displayed in order to redirect use to Twitter authorization page.
Complete code of login page.
Main task of process.php is to compare variables and redirect user back and forth. When user clicks on login button in index.php, user is sent to process.php, then it obtains a request token which is passed to Twitter Authorize page as oauth_token parameter. Once user signs in, user is authenticated and returned to the callback URL.
I hope this tutorial will help you, Once you understand the flow, it will be easier to implement it on your website. any feedback or comment is appreciated, Good luck!
Saran Chamling. (He loves the web technology, and enjoys designing, exploring and writing about it @saaraan.com).