PHP code example of weble / zohoclient

1. Go to this page and download the library: Download weble/zohoclient 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/ */

    

weble / zohoclient example snippets




$client = new \Weble\ZohoClient\OAuthClient('{CLIENT_ID}', '{CLIENT_SECRET}', '{REGION}', '{REDIRECTURL}');
$client->offlineMode(); // this needs to be set if you want to be able to refresh the token
$client->promptForConsent(); // Optional setting: Prompts for user consent each time your app tries to access user credentials.

// Get the url
$client->setScopes([]); // Set the zoho scopes you need, see https://www.zoho.com/crm/developer/docs/api/v2/scopes.html
$url = $client->getAuthorizationUrl();
$state = $client->getState(); // Get the state for security, and save it (usually in session)

redirect($url); // Do your redirection as you prefer

// Wait for the user to redirect...

// In the redirection page, check for the state you got before and that you should've stored
if ($state !== $_GET['state']) {
    throw new \Exception('Someone is tampering with the oauth2 request');
}

// Try to get an access token (using the authorization code grant)
try {
    $client->setGrantCode($_GET['code']);
    
    // if you set the offline mode, you can also get the refresh token here (and store it)
    $refreshToken = $client->getRefreshToken();
    
    // get the access token (and store it probably)
    $token = $client->getAccessToken();
    
} catch (\Exception $e) {
    // handle your exceptions
}



$client = new \Weble\ZohoClient\OAuthClient('{CLIENT_ID}', '{CLIENT_SECRET}');
$client->setRegion(\Weble\ZohoClient\Enums\Region::us());
$client->setRedirectUri('{REDIRECT_URI_OF_YOUR_APP}');
$client->onlineMode();

// When you get redirected back to your REDIRECT_URI_OF_YOUR_APP
$code = $_GET['code'];
$client->setGrantCode($code);

// Done!
$accessToken = $client->getAccessToken();

// Check if it's expired
$isExpired = $client->accessTokenExpired();