Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
1,905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. <?php
  2.  
  3. class InstagramGraphLogin {
  4.  
  5. /*
  6. * Facebook Object
  7. */
  8. private $fb;
  9.  
  10. /*
  11. * Facebook helper
  12. */
  13. private $fbHelper;
  14.  
  15. /*
  16. * Facebook Permissions
  17. */
  18. private $permissions = ['instagram_basic', 'manage_pages'];
  19.  
  20. /**
  21. * Instantiates a new Facebook class object, Facebook Helper
  22. *
  23. */
  24. public function __construct() {
  25. $this->fb = new \Facebook\Facebook([
  26. 'app_id' => FACEBOOK_APP_ID,
  27. 'app_secret' => FACEBOOK_APP_SECRET,
  28. 'default_graph_version' => 'v3.3',
  29. ]);
  30. $this->fbHelper = $this->fb->getRedirectLoginHelper();
  31. }
  32.  
  33. /**
  34. * Get Login Url for your Application
  35. *
  36. */
  37. public function getLoginUrl() {
  38. return $this->fbHelper->getLoginUrl(INSTAGRAM_GRAPH_REDIRECT_URL, $this->permissions);
  39. }
  40.  
  41. /**
  42. * returns an AccessToken.
  43. *
  44. *
  45. * @return AccessToken|null|false
  46. *
  47. * @throws FacebookSDKException
  48. */
  49. public function getAccessToken() {
  50. try {
  51. $accessToken = $this->fbHelper->getAccessToken();
  52. } catch (Facebook\Exceptions\FacebookResponseException $e) {
  53. // When Graph returns an error
  54. error_log('Graph returned an error: ' . $e->getMessage());
  55. return false;
  56. } catch (Facebook\Exceptions\FacebookSDKException $e) {
  57. // When validation fails or other local issues
  58. error_log('Facebook SDK returned an error: ' . $e->getMessage());
  59. return false;
  60. }
  61.  
  62. if (!isset($accessToken)) {
  63. if ($this->fbHelper->getError()) {
  64. $error = "Error: " . $this->fbHelper->getError() . "\n";
  65. $error .= "Error Code: " . $this->fbHelper->getErrorCode() . "\n";
  66. $error .= "Error Reason: " . $this->fbHelper->getErrorReason() . "\n";
  67. $error .= "Error Description: " . $this->fbHelper->getErrorDescription() . "\n";
  68.  
  69. error_log($error);
  70. return false;
  71. } else {
  72. error_log('Bad request');
  73. return false;
  74. }
  75. return false;
  76. }
  77.  
  78. // Logged in
  79. $accessTokenValue = $accessToken->getValue();
  80.  
  81. // The OAuth 2.0 client handler helps us manage access tokens
  82. $oAuth2Client = $this->fb->getOAuth2Client();
  83.  
  84. // Get the access token metadata from /debug_token
  85. $tokenMetadata = $oAuth2Client->debugToken($accessToken);
  86.  
  87. // Validation (these will throw FacebookSDKException's when they fail)
  88. $tokenMetadata->validateAppId(FACEBOOK_APP_ID);
  89. // If you know the user ID this access token belongs to, you can validate it here
  90. //$tokenMetadata->validateUserId('123');
  91. $tokenMetadata->validateExpiration();
  92.  
  93. if (!$accessToken->isLongLived()) {
  94. // Exchanges a short-lived access token for a long-lived one
  95. try {
  96. $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
  97. } catch (Facebook\Exceptions\FacebookSDKException $e) {
  98. error_log("<p>Error getting long-lived access token: " . $e->getMessage() . "</p>\n\n");
  99. }
  100.  
  101. $accessTokenValue = ($accessToken->getValue());
  102. }
  103.  
  104. return $accessTokenValue;
  105. }
  106.  
  107. /**
  108. * returns Connected Instagram IDs
  109. *
  110. *
  111. * @return array|false
  112. *
  113. * @throws FacebookSDKException
  114. */
  115. public function getConnectedInstagramID($accessToken) {
  116. try {
  117. $response = $this->fb->get('/me/accounts', $accessToken);
  118. } catch (\Facebook\Exceptions\FacebookResponseException $e) {
  119. // When Graph returns an error
  120. error_log('Graph returned an error: ' . $e->getMessage());
  121. return false;
  122. } catch (\Facebook\Exceptions\FacebookSDKException $e) {
  123. // When validation fails or other local issues
  124. error_log('Facebook SDK returned an error: ' . $e->getMessage());
  125. return false;
  126. }
  127.  
  128. $decodedData = json_decode($response->getBody());
  129.  
  130. $connectedAccountIDs = [];
  131.  
  132. foreach ($decodedData->data as $key => $item) {
  133. $pageID = $item->id;
  134. try {
  135. $connectedAccountResponse = $this->fb->get('/' . $pageID . '?fields=connected_instagram_account,instagram_business_account', $accessToken);
  136. $decodedConnectedAccountData = json_decode($connectedAccountResponse->getBody());
  137.  
  138. if (!empty($decodedConnectedAccountData->instagram_business_account)) {
  139. $connectedAccountIDs[] = $decodedConnectedAccountData->instagram_business_account->id;
  140. } else if (!empty($decodedConnectedAccountData->connected_instagram_account)) {
  141. $connectedAccountIDs[] = $decodedConnectedAccountData->connected_instagram_account->id;
  142. }
  143. } catch (\Facebook\Exceptions\FacebookResponseException $e) {
  144. // When Graph returns an error
  145. error_log('Graph returned an error: ' . $e->getMessage());
  146. return false;
  147. } catch (\Facebook\Exceptions\FacebookSDKException $e) {
  148. // When validation fails or other local issues
  149. error_log('Facebook SDK returned an error: ' . $e->getMessage());
  150. return false;
  151. }
  152. }
  153.  
  154. return $connectedAccountIDs;
  155. }
  156.  
  157. /**
  158. * returns User Info for Connected Instagram IDs
  159. *
  160. *
  161. * @return array|false
  162. *
  163. * @throws FacebookSDKException
  164. */
  165. public function getUserInfo() {
  166. $accessToken = $this->getAccessToken();
  167.  
  168. $connectedInstagramIDs = $this->getConnectedInstagramID($accessToken);
  169.  
  170. $instagramuserInfo = [];
  171.  
  172. if (!empty($connectedInstagramIDs)) {
  173. foreach ($connectedInstagramIDs as $key => $value) {
  174. try {
  175. // Get the \Facebook\GraphNodes\GraphUser object for the current user.
  176. // If you provided a 'default_access_token', the '{access-token}' is optional.
  177. $response = $this->fb->get('/'.$value.'?fields=name,biography,username,ig_id,followers_count,follows_count,media_count,profile_picture_url,website', $accessToken);
  178.  
  179. $decodedData = json_decode($response->getBody());
  180.  
  181. $instagramuserInfo[] = $decodedData;
  182. } catch (\Facebook\Exceptions\FacebookResponseException $e) {
  183. // When Graph returns an error
  184. error_log('Graph returned an error: ' . $e->getMessage());
  185. return false;
  186. } catch (\Facebook\Exceptions\FacebookSDKException $e) {
  187. // When validation fails or other local issues
  188. error_log('Facebook SDK returned an error: ' . $e->getMessage());
  189. return false;
  190. }
  191. }
  192. }
  193.  
  194. return ["userInfo" => $instagramuserInfo, "accessToken" => $accessToken];
  195. }
  196.  
  197. }
  198.  
  199. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement