Integrate the Google Login with API in PHP

In the previous post we have already see how to integrate the LinkedIn Login with API and now make for Google+ API in PHP. Compare with LinkedIn Google is the best & everyone have a account in google all of they maintain simply in one account in google. That’s why peoples are like google.

Now we see how to make the Google OAuth in PHP. Actually the code is same for compare with LinkedIn OAtuth so you have easily understand this article.

Google Developer Console

First, you need Client ID & Client Secret code to make the OAuth connection so here I have teach you how to get that just follow the steps here.

  1. Go to the Google Developer Console(
  2. Create new project & click create
  3. In the left sidebar Library tab will be visible, you have just click that and search Google+ API. Then click to Enable the Google+ API.
  4. Hereafter click Credentials.
  5. Click Create Credential tab button, Select Oauth Client ID
  6. Then list of Application type will be visible, you have just choose web application & create button.
  7. Then enter the Authorized Javascript Origins URL. The URL like
  8. Then fill the Authorized redirect URIs it like
  9. Finally click Save Button.

It’s like,

google developer console form

Database Configuration

Now move on the PHP code to integrate the Googel OAuth API. So first create one database the database name is googlelogin then just import the users.sql file in your database. Now the database side will be finished.

Integrate PHP code in Google+

So now open the gpConfig.php file and edit the following things like Client ID, Client Secret & redirect url. The code is


	//Include Google client library 
	include_once 'src/Google_Client.php';
	include_once 'src/contrib/Google_Oauth2Service.php';

	 * Configuration and setup Google API
	$clientId = ''; //Google client ID
	$clientSecret = 'PeQNr3CBhdZvaUXA0kQe5_ge'; //Google client secret
	$redirectURL = ''; //Callback URL

	//Call Google API
	$gClient = new Google_Client();
	$gClient->setApplicationName('Login to');

	$google_oauthV2 = new Google_Oauth2Service($gClient);

When you have change the Client ID & secret it will affect your code, so don’t change & delete. Suppose if you have delete just create another project and make the client ID’s to fetch the same credentials.

Homepage for Google+ OAuth

This is your own design, you have write any code to UI design to make the attractive look for your user. So just make the new things like attractive login page example. The sample code is,

    //Include GP config file && User class
    include_once 'gpConfig.php';
    include_once 'User.php';

    	$_SESSION['token'] = $gClient->getAccessToken();
    	header('Location: ' . filter_var($redirectURL, FILTER_SANITIZE_URL));

    if (isset($_SESSION['token'])) {

    if ($gClient->getAccessToken()) {
    	//Get user profile data from google
    	$gpUserProfile = $google_oauthV2->userinfo->get();
    	//Initialize User class
    	$user = new User();
    	//Insert or update user data to the database
        $gpUserData = array(
            'oauth_provider'=> 'google',
            'oauth_uid'     => $gpUserProfile['id'],
            'first_name'    => $gpUserProfile['given_name'],
            'last_name'     => $gpUserProfile['family_name'],
            'email'         => $gpUserProfile['email'],
            'gender'        => $gpUserProfile['gender'],
            'locale'        => $gpUserProfile['locale'],
            'picture'       => $gpUserProfile['picture'],
            'link'          => $gpUserProfile['link']
        $userData = $user->checkUser($gpUserData);
    	//Storing user data into session
    	$_SESSION['userData'] = $userData;
    	//Render facebook profile data
            $output = '<h1>Google+ Profile Details </h1>';
            $output .= '<img src="'.$userData['picture'].'" width="300" height="220">';
            $output .= '<br/>Google ID : ' . $userData['oauth_uid'];
            $output .= '<br/>Name : ' . $userData['first_name'].' '.$userData['last_name'];
            $output .= '<br/>Email : ' . $userData['email'];
            $output .= '<br/>Gender : ' . $userData['gender'];
            $output .= '<br/>Locale : ' . $userData['locale'];
            $output .= '<br/>Logged in with : Google';
            $output .= '<br/><a href="'.$userData['link'].'" target="_blank">Click to Visit Google+ Page</a>';
            $output .= '<br/>Logout from <a href="logout.php">Google</a>'; 
            $output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';
    } else {
    	$authUrl = $gClient->createAuthUrl();
    	$output = '<a href="'.filter_var($authUrl, FILTER_SANITIZE_URL).'"><img src="images/glogin.png" alt=""/></a>';
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Login with Google using PHP by CodexWorld</title>
        <style type="text/css">
            h1{font-family:Arial, Helvetica, sans-serif;color:#999999;}
        <div><?php echo $output; ?></div>

Execute the Program

The exact output look like,

google+ plus confirmation

Login with Google using PHP

Over. If you have any doubt regarding this feel free to comment below.

Download Demo


Google Matched Content...