Advertisement
Guest User

Facebook login/connect help - PHP

a guest
May 27th, 2011
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.58 KB | None | 0 0
  1. <?php
  2.  
  3. require 'sdk/facebook.php';
  4.  
  5. // Create our Application instance (replace this with your appId and secret).
  6. $facebook = new Facebook(array(
  7.   'appId'  => 'YOUR-APP-ID',
  8.   'secret' => 'YOUR-SECRET-KEY',
  9.   'cookie' => true,
  10. ));
  11.  
  12. // We may or may not have this data based on a $_GET or $_COOKIE based session.
  13. //
  14. // If we get a session here, it means we found a correctly signed session using
  15. // the Application Secret only Facebook and the Application know. We dont know
  16. // if it is still valid until we make an API call using the session. A session
  17. // can become invalid if it has already expired (should not be getting the
  18. // session back in this case) or if the user logged out of Facebook.
  19. $session = $facebook->getSession();
  20.  
  21. $me = null;
  22. // Session based API call.
  23. if ($session) {
  24.   try {
  25.     $uid = $facebook->getUser();
  26.     $me = $facebook->api('/me');
  27.   } catch (FacebookApiException $e) {
  28.     error_log($e);
  29.   }
  30. }
  31.  
  32. // login or logout url will be needed depending on current user state.
  33. if ($me) {
  34.   $logoutUrl = $facebook->getLogoutUrl();
  35. } else {
  36.   $loginUrl = $facebook->getLoginUrl();
  37. }
  38.  
  39. // This call will always work since we are fetching public data.
  40. $naitik = $facebook->api('/naitik');
  41.  
  42. ?>
  43. <!doctype html>
  44. <html xmlns:fb="http://www.facebook.com/2008/fbml">
  45.   <head>
  46.     <title>php-sdk</title>
  47.     <style>
  48.       body {
  49.         font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
  50.       }
  51.       h1 a {
  52.         text-decoration: none;
  53.         color: #3b5998;
  54.       }
  55.       h1 a:hover {
  56.         text-decoration: underline;
  57.       }
  58.     </style>
  59.   </head>
  60.   <body>
  61.     <!--
  62.       We use the JS SDK to provide a richer user experience. For more info,
  63.       look here: http://github.com/facebook/connect-js
  64.     -->
  65.     <div id="fb-root"></div>
  66.     <script>
  67.       window.fbAsyncInit = function() {
  68.         FB.init({
  69.           appId   : '<?php echo $facebook->getAppId(); ?>',
  70.           session : <?php echo json_encode($session); ?>, // don't refetch the session when PHP already has it
  71.           status  : true, // check login status
  72.           cookie  : true, // enable cookies to allow the server to access the session
  73.           xfbml   : true // parse XFBML
  74.         });
  75.  
  76.         // whenever the user logs in, we refresh the page
  77.         FB.Event.subscribe('auth.login', function() {
  78.           window.location.reload();
  79.         });
  80.       };
  81.  
  82.       (function() {
  83.         var e = document.createElement('script');
  84.         e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
  85.         e.async = true;
  86.         document.getElementById('fb-root').appendChild(e);
  87.       }());
  88.     </script>
  89.  
  90.  
  91.     <h1><a href="example.php">php-sdk</a></h1>
  92.  
  93.     <?php if ($me): ?>
  94.     <a href="<?php echo $logoutUrl; ?>">
  95.       <img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">
  96.     </a>
  97.     <?php else: ?>
  98.     <div>
  99.       Using JavaScript &amp; XFBML: <fb:login-button></fb:login-button>
  100.     </div>
  101.     <div>
  102.       Without using JavaScript &amp; XFBML:
  103.       <a href="<?php echo $loginUrl; ?>">
  104.         <img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">
  105.       </a>
  106.     </div>
  107.     <?php endif ?>
  108.  
  109.     <h3>Session</h3>
  110.     <?php if ($me): ?>
  111.     <pre><?php print_r($session); ?></pre>
  112.  
  113.     <h3>You</h3>
  114.     <img src="https://graph.facebook.com/<?php echo $uid; ?>/picture">
  115.     <?php echo $me['name']; ?>
  116.  
  117.     <h3>Your User Object</h3>
  118.     <pre><?php print_r($me); ?></pre>
  119.     <?php else: ?>
  120.     <strong><em>You are not Connected.</em></strong>
  121.     <?php endif ?>
  122.  
  123.   </body>
  124. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement