facebook.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright 2011 Facebook, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License. You may obtain
  7. * a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations
  15. * under the License.
  16. */
  17. use Facebook\Exceptions\FacebookSDKException;
  18. /**
  19. * Facebook gives cross-site-request-forgery-validation-failed without
  20. * initializing session data and without having the
  21. * 'persistent_data_handler' => 'session' property below
  22. */
  23. session_start();
  24. /**
  25. * you should update these values when debugging,
  26. * NOTE website URL for the app must be be set to http://localhost:8000/
  27. */
  28. $fb = new Facebook\Facebook(array(
  29. 'app_id' => '460287924057084',
  30. 'app_secret' => 'e27a5a07f9f07f52682d61dd69b716b5',
  31. 'default_graph_version' => 'v2.5',
  32. 'persistent_data_handler' => 'session'
  33. ));
  34. $helper = $fb->getRedirectLoginHelper();
  35. $permissions = [];
  36. //after logging in facebook will redirect us to this callback page
  37. $callback = 'http://localhost:8000/facebook';
  38. try {
  39. $accessToken = $helper->getAccessToken();
  40. if ($accessToken) {
  41. //if everything is ok we have accessToken from the callback
  42. $response = $fb->get('/me', $accessToken);
  43. $user = $response->getGraphUser()->asArray();
  44. $logoutUrl = $helper->getLogoutUrl($accessToken, $callback);
  45. $errorCode = 0;
  46. } else {
  47. //the first time we come to this page access token will be null
  48. $loginUrl = $helper->getLoginUrl($callback);
  49. $errorCode = 1;
  50. $user = null;
  51. }
  52. } catch (FacebookSDKException $e) {
  53. //the second time we come to this we might get this if something is wrong with login
  54. $errorCode = " 3 " . $e->getMessage();
  55. $user = null;
  56. }
  57. ?>
  58. <!doctype html>
  59. <html xmlns:fb="http://www.facebook.com/2008/fbml">
  60. <head>
  61. <title>php-sdk</title>
  62. <style>
  63. body {
  64. font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
  65. }
  66. h1 a {
  67. text-decoration: none;
  68. color: #3b5998;
  69. }
  70. h1 a:hover {
  71. text-decoration: underline;
  72. }
  73. </style>
  74. </head>
  75. <body>
  76. <h1>php-sdk</h1>
  77. <pre><?php print_r("\n errorCode: $errorCode\n"); ?></pre>
  78. <?php if ($user): ?>
  79. <a href="<?php echo $logoutUrl; ?>">Logout</a>
  80. <?php else: ?>
  81. <div>
  82. Login using OAuth 2.0 handled by the PHP SDK:
  83. <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
  84. </div>
  85. <?php endif ?>
  86. <h3>PHP Session</h3>
  87. <pre><?php print_r($_SESSION); ?></pre>
  88. <?php if ($user): ?>
  89. <h3>You</h3>
  90. <img src="https://graph.facebook.com/<?php echo $user['id']; ?>/picture">
  91. <h3>Your User Object (/me)</h3>
  92. <pre><?php print_r($user); ?></pre>
  93. <?php else: ?>
  94. <strong><em>You are not Connected.</em></strong>
  95. <?php endif ?>
  96. </body>
  97. </html>