Implement Google Auth in PHP using cURL Http Request

Implement Google Auth in PHP using cURL Http Request

In this Tutorial We will be building Login with Google Functionality from scratch. All the Source code of the Application is given below with application screenshots.

 

 

 

 

First of all go to your google developers console and create a new project and enter the redirect url and then generate the client id and client secret of the application as shown in the figure.

 

 

Make a new file settings.php to store all the credentials such as client id client secret and redirect url

 

<?php

/* Google App Client Id */
define('CLIENT_ID', 'xxxxxxxxxxxxxxxxxxxx');

/* Google App Client Secret */
define('CLIENT_SECRET', 'xxxxxxxxxxxxxxxxxxxx');

/* Google App Redirect Url */
define('CLIENT_REDIRECT_URL', 'xxxxxxxxxxxxxxxxxxxx');

?>

 

READ  Login with Google Account using PHP

Make a index.php file which will be the starting point of the application and include the settings.php file also in it

 

<?php

require_once('settings.php');

$login_url = 'https://accounts.google.com/o/oauth2/v2/auth?scope=' . urlencode('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email') . '&redirect_uri=' . urlencode(CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . CLIENT_ID . '&access_type=online';

?>
<html>
<head></head>

<body>
	
	
	<a href="<?= $login_url ?>">Login with Google</a>

	
</body>
</html>

 

Make a last but very important file of this application the redirect url file i.e. in my case login.php this will take the code which is sent by google and generate the access token and make a request to google for the information of the user all the source code is given below

READ  Create Dynamic PDF in MPDF & Send as Attachment with Email in PHP using PHPMailer

 

<?php
// Holds the Google application Client Id, Client Secret and Redirect Url
require_once('settings.php');

// Holds the various APIs functions
function GetAccessToken($client_id, $redirect_uri, $client_secret, $code) {	
	$url = 'https://www.googleapis.com/oauth2/v4/token';			

	$curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';
	$ch = curl_init();		
	curl_setopt($ch, CURLOPT_URL, $url);		
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);		
	curl_setopt($ch, CURLOPT_POST, 1);		
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);	
	$data = json_decode(curl_exec($ch), true);
	$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);		
	if($http_code != 200) 
		throw new Exception('Error : Failed to receieve access token');
	
	return $data;
}

function GetUserProfileInfo($access_token) {	
	$url = 'https://www.googleapis.com/oauth2/v2/userinfo?fields=name,email,gender,id,picture,verified_email';	
	
	$ch = curl_init();		
	curl_setopt($ch, CURLOPT_URL, $url);		
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
	$data = json_decode(curl_exec($ch), true);
	$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);		
	if($http_code != 200) 
		throw new Exception('Error : Failed to get user information');
		
	return $data;
}

// Google passes a parameter 'code' in the Redirect Url
if(isset($_GET['code'])) {
	try {
		// Get the access token 
		$data = GetAccessToken(CLIENT_ID, CLIENT_REDIRECT_URL, CLIENT_SECRET, $_GET['code']);

		// Access Token
		$access_token = $data['access_token'];
		
		// Get user information
        $user_info = GetUserProfileInfo($access_token);
        
        var_dump($user_info);

        echo $user_info['id'] . "<br>";

        echo $user_info['email'] . "<br>";
        
        echo $user_info['name'] . "<br>";
		
		echo "<img src=".$user_info['picture'].">";
        


	}
	catch(Exception $e) {
		echo $e->getMessage();
		exit();
	}
}

?>

 

READ  How to Check Username Availability in PHP AJAX MySQL

Congratulations we are done making a complete application in which we have implemented Google Auth in PHP with the help of cURL Http Request from scratch. Thanks for reading this post and if you like reading this and wants to read more of this please subscribe the blog below to get all the notications.

 

Leave a Reply

Close Menu