Advertisement
Piksel

Untitled

Nov 11th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. <?php
  2. session_start();
  3. require_once __DIR__ . '/src/Facebook/autoload.php';
  4. $fb = new Facebook\Facebook([
  5. 'app_id' => 'APP_ID',
  6. 'app_secret' => 'APP_SECRET',
  7. 'default_graph_version' => 'v2.5',
  8. ]);
  9. $helper = $fb->getRedirectLoginHelper();
  10. $permissions = ['user_birthday', 'user_location', 'user_website']; // optional
  11.  
  12. try {
  13. if (isset($_SESSION['localhost_app_token'])) {
  14. $accessToken = $_SESSION['localhost_app_token'];
  15. } else {
  16. $accessToken = $helper->getAccessToken();
  17. }
  18. } catch(Facebook\Exceptions\FacebookResponseException $e) {
  19. // When Graph returns an error
  20. echo 'Graph returned an error: ' . $e->getMessage();
  21. exit;
  22. } catch(Facebook\Exceptions\FacebookSDKException $e) {
  23. // When validation fails or other local issues
  24. echo 'Facebook SDK returned an error: ' . $e->getMessage();
  25. exit;
  26. }
  27. if (isset($accessToken)) {
  28. if (isset($_SESSION['localhost_app_token'])) {
  29. $fb->setDefaultAccessToken($_SESSION['localhost_app_token']);
  30. } else {
  31. // getting short-lived access token
  32. $_SESSION['localhost_app_token'] = (string) $accessToken;
  33. // OAuth 2.0 client handler
  34. $oAuth2Client = $fb->getOAuth2Client();
  35. // Exchanges a short-lived access token for a long-lived one
  36. $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['localhost_app_token']);
  37. $_SESSION['localhost_app_token'] = (string) $longLivedAccessToken;
  38. // setting default access token to be used in script
  39. $fb->setDefaultAccessToken($_SESSION['localhost_app_token']);
  40. }
  41. // redirect the user back to the same page if it has "code" GET variable
  42. if (isset($_GET['code'])) {
  43. header('Location: ./');
  44. }
  45. // getting basic info about user
  46. try {
  47. $profile_request = $fb->get('/me?fields=name,first_name,last_name,birthday,website,location');
  48. $profile = $profile_request->getGraphNode()->asArray();
  49. } catch(Facebook\Exceptions\FacebookResponseException $e) {
  50. // When Graph returns an error
  51. echo 'Graph returned an error: ' . $e->getMessage();
  52. session_destroy();
  53. // redirecting user back to app login page
  54. header("Location: ./");
  55. exit;
  56. } catch(Facebook\Exceptions\FacebookSDKException $e) {
  57. // When validation fails or other local issues
  58. echo 'Facebook SDK returned an error: ' . $e->getMessage();
  59. exit;
  60. }
  61.  
  62. // printing $profile array on the screen which holds the basic info about user
  63. echo $profile['birthday']->format('d-m-Y');
  64. echo $profile['website'];
  65. echo $profile['location']['name'];
  66. // Now you can redirect to another page and use the access token from $_SESSION['localhost_app_token']
  67. } else {
  68. // replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here
  69. $loginUrl = $helper->getLoginUrl('http://sohaibilyas.com/fbapp/', $permissions);
  70. echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement