Today I will show you the easiest way to integrate google API PHP client version 2 in your CodeIgniter project. You need to create an app in google API console before using google API. You can create an app for free easily by visiting following link.
Now we need to install google API client library for PHP using composer.
composer require google/apiclient:^2.0
If you don't know how to add composer in your CodeIgniter project visit my tutorial here.
Now coding parts.
In login view page
<p>
<a href="<?php echo base_url();?>login/glogin">Login with Google</a>
</p>
In glogin function in login controller
function glogin()
{
// Fill CLIENT ID, CLIENT SECRET ID, REDIRECT URI from Google Developer Console
$client_id = 'your client id';
$client_secret = 'your secret';
$redirect_uri = base_url('login/gcallback');;
//Create Client Request to access Google API
$client = new Google_Client();
$client->setApplicationName("Yourappname");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("email");
$client->addScope("profile");
//Send Client Request
$objOAuthService = new Google_Service_Oauth2($client);
$authUrl = $client->createAuthUrl();
header('Location: '.$authUrl);
}
Above function will redirect you to google authentication section and then return you to gcallback function in login controller. The code for gcallback is given below.
function gcallback()
{
// Fill CLIENT ID, CLIENT SECRET ID, REDIRECT URI from Google Developer Console
$client_id = 'your client id';
$client_secret = 'your client secret';
$redirect_uri = base_url('login/gcallback');
//Create Client Request to access Google API
$client = new Google_Client();
$client->setApplicationName("Yourappname");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("email");
$client->addScope("profile");
//Send Client Request
$service = new Google_Service_Oauth2($client);
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
// User information retrieval starts..............................
$user = $service->userinfo->get(); //get user info
echo "</br> User ID :".$user->id;
echo "</br> User Name :".$user->name;
echo "</br> Gender :".$user->gender;
echo "</br> User Email :".$user->email;
echo "</br> User Link :".$user->link;
echo "</br><img src='$user->picture' height='200' width='200' > ";
}
The above function will fetch basic user information from the facebook and displays it.The sample output is as below.
Now for making your own google authentication system in your project by steps like storing the user information in the table if new and user session to store necessary data for your authentication and at last redirect to the appropriate page. I just skipped that steps because it mainly depends on your authentication system codes and I don't want to mess it. If you have any doubts or suggestion then please comment below.