In this article, I am going to explain “Twitter Login in CodeIgnitor”. I have already explain Twitter oAuth API integration in PHP. So now we don’t need to explain “How create Twitter APP”. If you don’t know So please just go through the link “Twitter oAuth login api using php” and get full specification with step by step. In this post explanation in short because I have already explain this topic.
Twitter oAuth is a third party API and we already know that CodeIgnitor has a third_party directory where we can manage all third party API. Whenever we need to integrate any third party API. In CodeIgnitor third party integration is very simple.
Let’s Start Coding
Step 1:- Before start coding we need to download twitter PHP library, I will use TwitterOAuthas the most popular and easy to use. We can install it from the command line with Composer in codeignitor third party directory.
1 2 3 | composer require abraham/twitteroauth |
Step 2 :- Now after installation we need go to the constant.php file and define Consumer Key and Consumer Secret
application/config/constant.php
1 2 3 4 | define("Consumer_Key", "put your consumer key"); define("Consumer_Secret", "put your consumer secret"); |
Step 3:- Create a new file in libraries folder for custom twitter auth library like TwitterAuth.php, If you don’t know “How to create custom Library in CodeIgnitor” then just go through the link and read how to create library and load in codeIgnitor here
application/libraries/TwitterAuth.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); require_once APPPATH.'third_party/twitter-login/lib/autoload.php'; use Abraham\TwitterOAuth\TwitterOAuth; class TwitterAuth { protected $CI; public function __construct() { $this->CI =& get_instance(); $this->CI->load->library('session'); $this->connection= new TwitterOAuth(Consumer_Key, Consumer_Secret); } public function getLogin(){ $this->request_token = $this->connection->oauth("oauth/request_token", array("oauth_callback" => base_url()."Dashboardblog/twitterResponse")); // print_r($this->request_token); $_SESSION['oauth_token'] = $this->request_token['oauth_token']; $_SESSION['oauth_token_secret'] = $this->request_token['oauth_token_secret']; $url = $this->connection->url("oauth/authorize", array("oauth_token" => $this->request_token['oauth_token'])); $this->CI->session->set_userdata('oauth_token', $this->request_token['oauth_token']); $this->CI->session->set_userdata('oauth_token_secret',$this->request_token['oauth_token_secret']); redirect($this->connection->url('oauth/authorize', array('oauth_token' => $this->request_token['oauth_token']), 'refresh')); } public function checkAuth(){ $this->connection = new TwitterOAuth(Consumer_Key, Consumer_Secret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); $this->access_token = $this->connection->oauth('oauth/access_token', array('oauth_verifier' => $_REQUEST['oauth_verifier'], 'oauth_token'=> $_GET['oauth_token'])); $this->connection = new TwitterOAuth(Consumer_Key, Consumer_Secret, $this->access_token['oauth_token'], $this->access_token['oauth_token_secret']); $this->user_info = $this->connection->get('account/verify_credentials'); return $this->user_info; } } |
Step 4:- Now go to your controller file and create any function and create view file for display twitter login button.
1 2 3 4 5 | function index(){ $this->load->view('twitter_auth'); } |
Now create twitter_auth.php file in view direactory, So just copy below code and paste in your view file.
application/views/twitter_auth.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <!doctype html> <html> <head> <meta charset="utf-8"> <title>Twitter Login using PHP</title> <style> .wrapper { width: 800px; margin:0 auto; } h1 { font-size:28px; font-family:Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif; } </style> </head> <body> <div class="wrapper"> <h1><a href="https://webprepration.com/twitter-login-using-php/">Twitter Login using TwitterOAuth and PHP</a></h1> <a href='<?php echo base_url(); ?>Dashboardblog/twitterAuth'><img src='<?php echo base_url(); ?>/demo/img/twitter_login_button.png' style='width: 47%;'></a> </div> </body> </html> |
Step 5:- Now create a new function twitterAuth in controller and load TwitterAuth Library and call login function of library.
1 2 3 4 5 6 7 8 9 | public function twitterAuth(){ $this->load->library('TwitterAuth'); $this->twitterauth->getLogin(); } |
After that create a new function twitterResponse in controller for handle twitter response and insert user data in database.
1 2 3 4 5 6 7 8 9 10 11 12 | public function twitterResponse(){ $this->load->library('TwitterAuth'); $this->load->model('User_model'); $twitter_data = $this->twitterauth->checkAuth(); $res = $this->User_model->insert_userdata($twitter_data); if($res){ $userdata = array('id'=>$res->id,'firstname'=>$res->firstname,'lastname'=>$res->lastname,'email'=>$res->email,'password'=>$res->password,'user_type'=>$res->user_type); $this->session->set_userdata($userdata); redirect(base_url('Dashboard/index')); } |
Now create User_model in model directory for insert or update user details in database for proper login and set user data in session.
application/models/User_model.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | function check_tuser($data){ $check = $this->checkTuser($data->id); $insertdata = array('twitter_id'=>$data->id, 'firstname'=>$data->screen_name, 'location'=>$data->location, 'profile_pic'=>$data->profile_image_url, ); if(!empty($check) && count($check) > 0 ){ $this->db->where('id',$check->id); $this->db->update('register',$insertdata); return $check; }else{ $this->db->insert('register',$insertdata); return $this->checkTuser($this->db->insert_id()); } } function checkTuser($id){ $this->db->select('*'); $this->db->from("register"); $this->db->where('twitter_id',$id); $qry = $this->db->get(); return $qry->row(); } |
I hope you might have understood it properly, keep reading our other blogs posts for more coding tricks.
Thanks!!
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.