PHP code example of digitalgdn / oauth2-sheepcrm

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

    

digitalgdn / oauth2-sheepcrm example snippets


$sheepProvider = new \DigitalGdn\OAuth2\Client\Provider\Sheep([
    'clientId'     => 'yourId',          // The client ID assigned to you by sheepCRM
    'clientSecret' => 'yourSecret',      // The client password assigned to you by sheepCRM
    'redirectUri'  => 'yourRedirectUri'  // The return URL you specified for your app on sheepCRM
]);

// Get authorization code
if (!isset($_GET['code'])) {
    // Options are optional, defaults shown below.
    // Add 'write' or 'introspection' only if your app needs them.
    $options = ['scope' => ['read', 'read_membership']];
    // Get authorization URL
    $authorizationUrl = $sheepProvider->getAuthorizationUrl($options);

    // Get state and store it to the session
    $_SESSION['oauth2state'] = $sheepProvider->getState();

    // Redirect user to authorization URL
    header('Location: ' . $authorizationUrl);
    exit;
// Check for errors
} elseif (empty($_GET['state']) || empty($_SESSION['oauth2state']) || !hash_equals($_SESSION['oauth2state'], $_GET['state'])) {
    if (isset($_SESSION['oauth2state'])) {
        unset($_SESSION['oauth2state']);
    }
    exit('Invalid state');
} else {
    // The state is single use
    unset($_SESSION['oauth2state']);

    // Get access token
    try {
        $accessToken = $sheepProvider->getAccessToken(
            'authorization_code',
            [
                'code' => $_GET['code']
            ]
        );
    } catch (\Exception $e) {
        exit(htmlspecialchars($e->getMessage()));
    }

    // Get resource owner
    try {
        $resourceOwner = $sheepProvider->getResourceOwner($accessToken);
    } catch (\Exception $e) {
        exit(htmlspecialchars($e->getMessage()));
    }

    // Now you can store the results to session etc.
    $_SESSION['accessToken'] = $accessToken;
    $_SESSION['resourceOwner'] = $resourceOwner;

    var_dump(
        $resourceOwner->getId(),
        $resourceOwner->getUsername(),
        $resourceOwner->getFirstName(),
        $resourceOwner->getLastName(),
        $resourceOwner->getName(),
        $resourceOwner->getEmail(),
        $resourceOwner->getActiveMember(),
        $resourceOwner->toArray()
    );
}