1. Go to this page and download the library: Download cilogon/oauth2-orcid 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/ */
cilogon / oauth2-orcid example snippets
$provider = new CILogon\OAuth2\Client\Provider\ORCID([
'clientId' => '{orcid-client-id}',
'clientSecret' => '{orcid-client-secret}',
'redirectUri' => 'https://example.com/callback-url',
]);
if (!empty($_GET['error'])) {
// Got an error, probably user denied access
exit('Got error: ' . $_GET['error'] .
'Description: ' . $GET['error_description']);
} elseif (empty($_GET['code'])) {
$authUrl = $provider->getAuthorizationUrl();
$_SESSION['oauth2state'] = $provider->getState();
header('Location: '.$authUrl);
exit;
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
// Check given state against previously stored one to mitigate CSRF attack
unset($_SESSION['oauth2state']);
exit('Invalid state');
} else {
try {
// Try to get an access token using the authorization code grant
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
// Print out the access token, which can be used in
// authenticated requests against the service provider's API.
echo '<xmp>' . "\n";
echo 'Token : ' . $token->getToken() . "\n";
$expires = $token->getExpires();
if (!is_null($expires)) {
echo 'Expires : ' . $token->getExpires();
echo ($token->hasExpired() ? ' (expired)' : ' (active)') . "\n";
}
echo '</xmp>' . "\n";
// Using the access token, get the user's details
$user = $provider->getResourceOwner($token);
echo '<xmp>' . "\n";
echo 'User ID : ' . $user->getId() . "\n";
echo 'First name : ' . $user->getGivenName() . "\n"; // or getFirstName()
echo 'Last name : ' . $user->getFamilyName() . "\n"; // or getLastName()
echo 'Published name : ' . $user->getName() . "\n";
echo 'Also Known As : ' . implode(',', $user->getOtherNames()) . "\n";
echo 'Email : ' . $user->getEmail() . "\n"; // 'Primary' preferred
echo 'Primary Email : ' . $user->getPrimaryEmail() . "\n";// 'Primary' ONLY
echo 'All Emails : ' . implode(',', $user->getEmails()) . "\n";
echo 'AuthnMethodRef : ' . $user->getAmr() . "\n"; // Only for Member API
echo '</xmp>';
} catch (Exception $e) {
// Failed to get access token or user details
exit('Something went wrong: ' . $e->getMessage());
}
}