/examples/with_js_sdk.php

http://github.com/facebook/php-sdk · PHP · 59 lines · 52 code · 5 blank · 2 comment · 3 complexity · 555e738cb8c6b42834a5c5a2083bd3e6 MD5 · raw file

  1. <?php
  2. require '../src/facebook.php';
  3. $facebook = new Facebook(array(
  4. 'appId' => '344617158898614',
  5. 'secret' => '6dc8ac871858b34798bc2488200e503d',
  6. ));
  7. // See if there is a user from a cookie
  8. $user = $facebook->getUser();
  9. if ($user) {
  10. try {
  11. // Proceed knowing you have a logged in user who's authenticated.
  12. $user_profile = $facebook->api('/me');
  13. } catch (FacebookApiException $e) {
  14. echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
  15. $user = null;
  16. }
  17. }
  18. ?>
  19. <!DOCTYPE html>
  20. <html xmlns:fb="http://www.facebook.com/2008/fbml">
  21. <body>
  22. <?php if ($user) { ?>
  23. Your user profile is
  24. <pre>
  25. <?php print htmlspecialchars(print_r($user_profile, true)) ?>
  26. </pre>
  27. <?php } else { ?>
  28. <fb:login-button></fb:login-button>
  29. <?php } ?>
  30. <div id="fb-root"></div>
  31. <script>
  32. window.fbAsyncInit = function() {
  33. FB.init({
  34. appId: '<?php echo $facebook->getAppID() ?>',
  35. cookie: true,
  36. xfbml: true,
  37. oauth: true
  38. });
  39. FB.Event.subscribe('auth.login', function(response) {
  40. window.location.reload();
  41. });
  42. FB.Event.subscribe('auth.logout', function(response) {
  43. window.location.reload();
  44. });
  45. };
  46. (function() {
  47. var e = document.createElement('script'); e.async = true;
  48. e.src = document.location.protocol +
  49. '//connect.facebook.net/en_US/all.js';
  50. document.getElementById('fb-root').appendChild(e);
  51. }());
  52. </script>
  53. </body>
  54. </html>