1. Go to this page and download the library: Download webiik/oauth2client 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/ */
webiik / oauth2client example snippets
// Facebook Example
// Prepare dependencies
$chc = new \Webiik\CurlHttpClient\CurlHttpClient();
// Instantiate OAuth2 client
$oAuth2Client = new \Webiik\OAuth2Client\OAuth2Client($chc);
// Your callback URL after authorization
// OAuth2 server redirects users to this URL, after user verification
$oAuth2Client->setRedirectUri('https://127.0.0.1/webiik/');
// API endpoints
$oAuth2Client->setAuthorizeUrl('https://www.facebook.com/v3.3/dialog/oauth');
$oAuth2Client->setAccessTokenUrl('https://graph.facebook.com/v3.3/oauth/access_token');
$oAuth2Client->setValidateTokenUrl('https://graph.facebook.com/debug_token');
// API credentials (create yours at https://developers.facebook.com/apps/)
$oAuth2Client->setClientId('your-client-id');
$oAuth2Client->setClientSecret('your-client-sectret');
// Make API calls...
// Define scope
$scope = [
'email',
];
if (!isset($_GET['code'])) {
// 1. Prepare Facebook user login link with specified scope and grand type
echo '<a href="' . $oAuth2Client->getAuthorizeUrl($scope) . '" target="_blank">Authorize with Facebook</a><br/>';
}
if (isset($_GET['code'])) {
// 2. Verify code to obtain user access_token
$user = $oAuth2Client->getAccessTokenByCode();
// 3. Verify clientId and clientSecret to obtain app access_token
$app = $oAuth2Client->getAccessTokenByCredentials();
}
if (isset($user, $user['access_token']) && isset($app, $app['access_token'])) {
// 4. User and app access_tokens are valid, user and app are authorized by Facebook
// Access protected resources...
// Get user id
$tokenInfo = $oAuth2Client->getTokenInfo($user['access_token'], $app['access_token'], true);
if (!isset($tokenInfo['data'], $tokenInfo['data']['user_id'])) {
// Err: Can't obtain user id
print_r($tokenInfo);
exit;
}
// Get additional user info
$fields = [
'name',
'first_name',
'middle_name',
'last_name',
'email',
];
$reg = $chc->prepareRequest('https://graph.facebook.com/v3.3/' . $tokenInfo['data']['user_id'] . '/?access_token=' . $user['access_token'] . '&fields=' . implode(',', $fields));
$res = $chc->send($reg);
if ($res->isOk()) {
header('Content-Type: application/json');
echo $res->body();
}
}