Simple php function to login with google plus using api, simple tutorial and example with full php code to sign in with google plus.

How To Make Login With Google Plus

Using Google Plus API and PHP cURL we will make login with google plus easily.

Live Demo

Open live demo.

Create A Project

Go to Google Developer Console and create a project:

create-a-project

Now open your project, and in left menu, choose “API Manager”:

api-manager

Now click on “Google+ API”:

google-plus-api

Now on “Enable API” button:

enable-api

Now in left menu, click on “Credentials” link, after that click on “New credentials” button and choose “OAuth client ID”:

google-api-credentials

Now choose “Web Application” and fill all fields:

create-client-id

In “Name” field, enter your app name, and in “Authorized JavaScript origins” field, enter your domain or enter “http://localhost”, in “Authorized redirect URIs” field, enter your redirect uri.

Now we need Client ID and Client secret and Redirect URI, open your project, and you will find it:

oauth-client

Login With Google Plus Example

Now we need to make authorization, to get user access token, we will use Qassim_HTTP() Function, create Qassim_HTTP.php file and add this code inside it:

<?php

/* By Qassim Hassan, wp-time.com */

function Qassim_HTTP($method, $url, $header, $data){
    if( $method == 1 ){
        $method_type = 1; // 1 = POST
    }else{
        $method_type = 0; // 0 = GET
    }

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_HEADER, 0);

    if( $header !== 0 ){
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    }

    curl_setopt($curl, CURLOPT_POST, $method_type);

    if( $data !== 0 ){
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    $response = curl_exec($curl);
   $json = json_decode($response, true);
    curl_close($curl);

    return $json;
}

?>

Now create config.php file and add this code inside it (read the comments next to the variables):

<?php

/* By Qassim Hassan, wp-time.com */

$scope = "https://www.googleapis.com/auth/userinfo.email"; // Do not change it!

$redirect_uri = "xxxx"; // Enter your redirect_uri

$client_id = "xxxx"; // Enter your client_id

$client_secret = "xxxx"; // Enter your client_secret

$login_url = "https://accounts.google.com/o/oauth2/v2/auth?scope=$scope&response_type=code&redirect_uri=$redirect_uri&client_id=$client_id"; // Do not change it!

$image_size = 100; // Change user profile image size: 100 = 100x100

?>

Now create login.php file and add this code inside it:

<?php

session_start();

/* By Qassim Hassan, wp-time.com */

if( !isset($_GET['code']) or isset($_SESSION["gp_access_token"]) or isset($_SESSION["gp_result"]) ){
    header("location: index.php");
    exit();
}

include 'Qassim_HTTP.php';
include 'config.php';

$header = array( "Content-Type: application/x-www-form-urlencoded" );

$data = http_build_query(
        array(
           'code' => str_replace("#", null, $_GET['code']),
            'client_id' => $client_id,
            'client_secret' => $client_secret,
            'redirect_uri' => $redirect_uri,
            'grant_type' => 'authorization_code'
        )
    );

$url = "https://www.googleapis.com/oauth2/v4/token"; 

$result = Qassim_HTTP(1, $url, $header, $data);

if( !empty($result['error']) ){ // If error login
    header("location: index.php");
    exit();
}else{
    $_SESSION["gp_access_token"] = $result['access_token']; // Access Token
    header("location: index.php");
}

?>

Now create index.php file and add this code inside it:

<?php
session_start();

/* By Qassim Hassan, wp-time.com */

include 'config.php';
if( isset($_SESSION["gp_access_token"]) ){ // If logged in
 include 'Qassim_HTTP.php';
 $access_token = $_SESSION["gp_access_token"]; // User access token
 $api_url = "https://www.googleapis.com/plus/v1/people/me?fields=aboutMe%2Cemails%2Cimage%2Cname&access_token=$access_token"; // Do not change it!

 if( !isset($_SESSION["gp_result"]) ){
 $result = Qassim_HTTP(0, $api_url, 0, 0);
 $_SESSION["gp_result"] = $result;
 $user_info = $_SESSION["gp_result"];
 }else{
 $user_info = $_SESSION["gp_result"];
 }

 $first_name = $user_info['name']['givenName']; // User first name
 $last_name = $user_info['name']['familyName']; // User last name
 $email = $user_info['emails'][0]['value']; // User email
 $get_profile_image = $user_info['image']['url'];
 $change_image_size = str_replace("?sz=50", "?sz=$image_size", $get_profile_image);
 $profile_image_link = $change_image_size; // User profile image link
 $page_title = "Hello $first_name $last_name!"; // Page title if user is logged in
}
else{
 $page_title = "Login With Google Plus Using Qassim_HTTP() Function"; // Page title if user is no logged in
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $page_title; ?></title>
</head>
<body>

<?php

if( isset($_SESSION["gp_access_token"]) ){ // If logged in
 ?>
 <img src="<?php echo $profile_image_link; ?>">
 <h1>Hello <?php echo $first_name.' '.$last_name; ?>!</h1>
 <p>Your Email: <?php echo $email; ?></p>
 <p><a href="logout.php">Logout</a></p>
 <?php
}

else{ // If is not logged in
 ?>
 <h1>Welcome</h1>
 <p><a href="<?php echo $login_url; ?>">Login with Google plus</a></p>
 <?php
}

?>

</body>
</html>

Now create logout.php file and add this code inside it:

<?php

session_start();

/* By Qassim Hassan, wp-time.com */

if ( isset($_SESSION["gp_access_token"]) or isset($_SESSION["gp_result"]) ){
    unset($_SESSION["gp_access_token"]);
    unset($_SESSION["gp_result"]);
    header("location: index.php");
}

else{
    header("location: index.php");
}

?>

Now open your website and login with google plus:

login with google plus demo

You are logged in:

login with google plus

Download login with google PHP script.

Note

It’s same login with “Google Account”.

More

Check more Tutorials of Login with API.