PHP code example of simplon / gplus

1. Go to this page and download the library: Download simplon/gplus library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simplon / gplus example snippets


$authVo = (new \Simplon\Gplus\Vo\GplusAuthVo())
    ->setClientId(CLIENT_ID)
    ->setClientSecret(CLIENT_SECRET)
    ->setUrlRedirect(AUTH_URL_REDIRECT); // URL to which google redirects after authentication

$authVo = (new \Simplon\Gplus\Vo\GplusAuthVo())
    ->setClientId(CLIENT_ID)
    ->setClientSecret(CLIENT_SECRET)
    ->setUrlRedirect(AUTH_URL_REDIRECT);
    ->forceApprovalPrompt();

// pass our prior created authVo
$gplus = new \Simplon\Gplus\Gplus($authVo);

// with class constants (highly recommended)
$scopes = [
    \Simplon\Gplus\GplusConstants::AUTH_SCOPE_EMAIL,
    \Simplon\Gplus\GplusConstants::AUTH_SCOPE_PROFILE,
];

// without class constants
$scopes = ['email', 'profile',];

// pass prior defined $scopes
$urlAuth = $gplus->getAuthUrl($scopes);

if(isset($_GET['code']))
{
    $gplusRequestAccessTokenVo = $gplus->requestAccessToken($_GET['code']);
    
    if($gplusRequestAccessTokenVo !== FALSE)
    {
        echo $gplusRequestAccessTokenVo->getAccessToken();
        echo $gplusRequestAccessTokenVo->getRefreshToken();
    }
}

$gplusPersonVo = $gplus->getUserDetails($accessToken, $refreshToken);

$gplusPersonVo->getAccessToken();
$gplusPersonVo->getDisplayName();
$gplusPersonVo->getEmailAccount();
$gplusPersonVo->getEmails();
$gplusPersonVo->getGender();
$gplusPersonVo->getId();
$gplusPersonVo->getLanguage();
$gplusPersonVo->getRefreshToken();
$gplusPersonVo->getUrlImage();
$gplusPersonVo->getUrlImageBySize($squareSizePixel = 50);
$gplusPersonVo->getUrlProfile();
$gplusPersonVo->getVerified();
$gplusPersonVo->isNewAccessToken();
$gplusPersonVo->isVerified();

$gplusRefreshAccessTokenVo = $gplus->refreshAccessToken($refreshToken);

// set credentials
$authVo = (new \Simplon\Gplus\Vo\GplusAuthVo())
    ->setClientId(CLIENT_ID)
    ->setClientSecret(CLIENT_SECRET)
    ->setUrlRedirect(AUTH_URL_REDIRECT);
    
// create instance
$gplus = new \Simplon\Gplus\Gplus($authVo);

// set permission scopes
$scopes = [
    \Simplon\Gplus\GplusConstants::AUTH_SCOPE_EMAIL,
    \Simplon\Gplus\GplusConstants::AUTH_SCOPE_PROFILE,
];

// get auth url
$urlAuth = $gplus->getAuthUrl($scopes);

// --> redirect user to Google's authentication page

// ##############################################

// <-- user comes back with code ...

if(isset($_GET['code']))
{
    // get accessToken + refreshToken
    $gplusRequestAccessTokenVo = $gplus->requestAccessToken($_GET['code']);
    
    if($gplusRequestAccessTokenVo !== FALSE)
    {
        // --> save accessToken + refreshToken for offline access to DB ...
        
        // fetch profile data
        $gplusPersonVo = $gplus->getUserDetails(
            $gplusRequestAccessTokenVo->getAccessToken(),
            $gplusRequestAccessTokenVo->getRefreshToken()
        );
        
        // print data
        var_dump($gplusPersonVo);
    }
}
FALSE