Facebook Connect Tutorial


/ Published in: PHP
Save to your folder(s)

I will show you how to crate an app for facebook to handle the facebook connect process on your web site.
Then I will show you the php code to perform the login on facebook and retrieve the informations you need for your site.
The last suggestion will point how to merge facebook users with your users.
Follow the link above to get the full tutoriall with text and images.


Copy this code and paste it in your HTML
  1. <?php
  2. //
  3. // uses the PHP SDK. Download from https://github.com/facebook/php-sdk
  4. include("facebook-php-sdk/src/facebook.php");
  5.  
  6. //
  7. // from the facebook app page
  8. define('YOUR_APP_ID', '32WEWWEWWEWEWEWEWEW24');
  9. define('YOUR_APP_SECRET', '49fd00ce6237WEEWEWEWEWEW2');
  10.  
  11. //
  12. // new facebook object to interact with facebook
  13. $facebook = new Facebook(array(
  14. 'appId' => YOUR_APP_ID,
  15. 'secret' => YOUR_APP_SECRET,
  16. ));
  17. //
  18. // if user is logged in on facebook and already gave permissions
  19. // to your app, get his data:
  20. $userId = $facebook->getUser();
  21.  
  22. ?>
  23. <html>
  24. <head>
  25. <style>body { text-align:center; font-size: 40px }</style>
  26. </head>
  27. <body>
  28. <?php
  29. if ($userId) {
  30. //
  31. // already logged? show some data
  32. $userInfo = $facebook->api('/' + $userId);
  33.  
  34. echo "<p>YOU ARE: <strong>". $userInfo['name'] ."</strong><br/>";
  35. echo "Your birth date is: ".$userInfo['birthday']."</p>";
  36.  
  37. &nbsp;
  38.  
  39. } else {
  40. //
  41. // use javaascript api to open dialogue and perform
  42. // the facebook connect process by inserting the fb:login-button
  43. ?>
  44. <div id="fb-root"></div>
  45. <fb:login-button scope='email,user_birthday'></fb:login-button>
  46. <?php
  47. }
  48. ?>
  49. <script>
  50. window.fbAsyncInit = function() {
  51. FB.init({
  52. appId : <?=YOUR_APP_ID?>,
  53. status : true,
  54. cookie : true,
  55. xfbml : true,
  56. oauth : true,
  57. });
  58.  
  59. FB.Event.subscribe('auth.login', function(response) {
  60. // ------------------------------------------------------
  61. // This is the callback if everything is ok
  62. window.location.reload();
  63. });
  64. };
  65.  
  66. (function(d){
  67. var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
  68. js = d.createElement('script'); js.id = id; js.async = true;
  69. js.src = "//connect.facebook.net/en_US/all.js";
  70. d.getElementsByTagName('head')[0].appendChild(js);
  71. }(document));
  72. </script>
  73. </body>
  74. </html>

URL: http://www.barattalo.it/facebook-connect-tutorial-new-version/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.