Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Instagram Connect, get User Info
<?php
class InstagramGraphLogin {
/*
* Facebook Object
*/
private $fb;
/*
* Facebook helper
*/
private $fbHelper;
/*
* Facebook Permissions
*/
private $permissions = ['instagram_basic', 'manage_pages'];
/**
* Instantiates a new Facebook class object, Facebook Helper
*
*/
public function __construct() {
$this->fb = new \Facebook\Facebook([
'app_id' => FACEBOOK_APP_ID,
'app_secret' => FACEBOOK_APP_SECRET,
'default_graph_version' => 'v3.3',
]);
$this->fbHelper = $this->fb->getRedirectLoginHelper();
}
/**
* Get Login Url for your Application
*
*/
public function getLoginUrl() {
return $this->fbHelper->getLoginUrl(INSTAGRAM_GRAPH_REDIRECT_URL, $this->permissions);
}
/**
* returns an AccessToken.
*
*
* @return AccessToken|null|false
*
* @throws FacebookSDKException
*/
public function getAccessToken() {
try {
$accessToken = $this->fbHelper->getAccessToken();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
error_log('Graph returned an error: ' . $e->getMessage());
return false;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
error_log('Facebook SDK returned an error: ' . $e->getMessage());
return false;
}
if (!isset($accessToken)) {
if ($this->fbHelper->getError()) {
$error = "Error: " . $this->fbHelper->getError() . "\n";
$error .= "Error Code: " . $this->fbHelper->getErrorCode() . "\n";
$error .= "Error Reason: " . $this->fbHelper->getErrorReason() . "\n";
$error .= "Error Description: " . $this->fbHelper->getErrorDescription() . "\n";
error_log($error);
return false;
} else {
error_log('Bad request');
return false;
}
return false;
}
// Logged in
$accessTokenValue = $accessToken->getValue();
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $this->fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId(FACEBOOK_APP_ID);
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if (!$accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
error_log("<p>Error getting long-lived access token: " . $e->getMessage() . "</p>\n\n");
}
$accessTokenValue = ($accessToken->getValue());
}
return $accessTokenValue;
}
/**
* returns Connected Instagram IDs
*
*
* @return array|false
*
* @throws FacebookSDKException
*/
public function getConnectedInstagramID($accessToken) {
try {
$response = $this->fb->get('/me/accounts', $accessToken);
} catch (\Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
error_log('Graph returned an error: ' . $e->getMessage());
return false;
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
error_log('Facebook SDK returned an error: ' . $e->getMessage());
return false;
}
$decodedData = json_decode($response->getBody());
$connectedAccountIDs = [];
foreach ($decodedData->data as $key => $item) {
$pageID = $item->id;
try {
$connectedAccountResponse = $this->fb->get('/' . $pageID . '?fields=connected_instagram_account,instagram_business_account', $accessToken);
$decodedConnectedAccountData = json_decode($connectedAccountResponse->getBody());
if (!empty($decodedConnectedAccountData->instagram_business_account)) {
$connectedAccountIDs[] = $decodedConnectedAccountData->instagram_business_account->id;
} else if (!empty($decodedConnectedAccountData->connected_instagram_account)) {
$connectedAccountIDs[] = $decodedConnectedAccountData->connected_instagram_account->id;
}
} catch (\Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
error_log('Graph returned an error: ' . $e->getMessage());
return false;
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
error_log('Facebook SDK returned an error: ' . $e->getMessage());
return false;
}
}
return $connectedAccountIDs;
}
/**
* returns User Info for Connected Instagram IDs
*
*
* @return array|false
*
* @throws FacebookSDKException
*/
public function getUserInfo() {
$accessToken = $this->getAccessToken();
$connectedInstagramIDs = $this->getConnectedInstagramID($accessToken);
$instagramuserInfo = [];
if (!empty($connectedInstagramIDs)) {
foreach ($connectedInstagramIDs as $key => $value) {
try {
// Get the \Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$response = $this->fb->get('/'.$value.'?fields=name,biography,username,ig_id,followers_count,follows_count,media_count,profile_picture_url,website', $accessToken);
$decodedData = json_decode($response->getBody());
$instagramuserInfo[] = $decodedData;
} catch (\Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
error_log('Graph returned an error: ' . $e->getMessage());
return false;
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
error_log('Facebook SDK returned an error: ' . $e->getMessage());
return false;
}
}
}
return ["userInfo" => $instagramuserInfo, "accessToken" => $accessToken];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.