Advertisement
ekaomk

Untitled

Jul 29th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.82 KB | None | 0 0
  1. <?php
  2.  
  3. session_start(); //Session should be active
  4.  
  5. $app_id             = '431555557031282';  //Facebook App ID
  6. $app_secret         = '1a547fd5677f1b0a1547ceed3cdef664'; //Facebook App Secret
  7. $required_scope     = 'public_profile, publish_actions, email'; //Permissions required
  8. $redirect_url       = 'http://localhost/facebook-connect/'; //FB redirects to this page with a code
  9.  
  10. //include autoload.php from SDK folder, just point to the file like this:
  11. require_once('facebook-php-sdk/src/facebook.php');
  12.  
  13. $config = array(
  14.   'appId' => '431555557031282',
  15.   'secret' => '1a547fd5677f1b0a1547ceed3cdef664',
  16.   'fileUpload' => false, // optional
  17.   'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
  18. );
  19.  
  20. $facebook = new Facebook($config);
  21.  
  22. // Get User ID
  23. $user = $facebook->getUser();
  24. // We may or may not have this data based on whether the user is logged in.
  25. //
  26. // If we have a $user id here, it means we know the user is logged into
  27. // Facebook, but we don't know if the access token is valid. An access
  28. // token is invalid if the user logged out of Facebook.
  29. if ($user) {
  30.   try {
  31.     // Proceed knowing you have a logged in user who's authenticated.
  32.     $user_profile = $facebook->api('/me');
  33.   } catch (FacebookApiException $e) {
  34.     error_log($e);
  35.     $user = null;
  36.   }
  37. }
  38. // Login or logout url will be needed depending on current user state.
  39. if ($user) {
  40.   $logoutUrl = $facebook->getLogoutUrl();
  41. } else {
  42.   $loginUrl = $facebook->getLoginUrl();
  43. }
  44. // This call will always work since we are fetching public data.
  45. //$naitik = $facebook->api('/naitik');
  46.  
  47. ?>
  48.  
  49. <!doctype html>
  50. <html xmlns:fb="http://www.facebook.com/2008/fbml">
  51.   <head>
  52.     <title>php-sdk</title>
  53.     <style>
  54.       body {
  55.         font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
  56.       }
  57.       h1 a {
  58.         text-decoration: none;
  59.         color: #3b5998;
  60.       }
  61.       h1 a:hover {
  62.         text-decoration: underline;
  63.       }
  64.     </style>
  65.   </head>
  66.   <body>
  67.     <h1>php-sdk</h1>
  68.  
  69.     <?php if ($user): ?>
  70.       <a href="<?php echo $logoutUrl; ?>">Logout</a>
  71.     <?php else: ?>
  72.       <div>
  73.         Login using OAuth 2.0 handled by the PHP SDK:
  74.         <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
  75.       </div>
  76.     <?php endif ?>
  77.  
  78.     <h3>PHP Session</h3>
  79.     <pre><?php print_r($_SESSION); ?></pre>
  80.  
  81.     <?php if ($user): ?>
  82.       <h3>You</h3>
  83.       <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
  84.  
  85.       <h3>Your User Object (/me)</h3>
  86.       <pre><?php print_r($user_profile); ?></pre>
  87.     <?php else: ?>
  88.       <strong><em>You are not Connected.</em></strong>
  89.     <?php endif ?>
  90.  
  91.     <!--<h3>Public profile of Naitik</h3>
  92.     <img src="https://graph.facebook.com/naitik/picture">
  93.      <?php //echo $naitik['name']; ?> -->
  94.   </body>
  95. </html>
  96.  
  97.  
  98. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement