Posted By

saturngod on 03/05/10


Tagged

api facebook


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

BrianCoyDesign
dantreacy
eval
joethermal


Facebook Connect


 / Published in: PHP
 

URL: http://www.saturngod.net/knowledgebase/facebook-connect

using facebook connect snippet

  1. <?php
  2. //include_once 'fbconnect/config.php'; //has $api_key and $secret defined.
  3. include_once 'facebook/facebook.php';
  4. //global $api_key,$secret;
  5. $appapikey = '';
  6. $appsecret = '';
  7. //Start Facebook
  8. $fb=new Facebook($appapikey,$appsecret);
  9. $fb_user=$fb->get_loggedin_user();
  10.  
  11. ?>
  12. <html>
  13. <head>
  14. <!-- Call Javascript For FBML -->
  15.  
  16. <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script><script type="text/javascript">FB.init("<?= $appapikey ?>");</script>
  17.  
  18. <title>Facebook Connect</title>
  19. </head>
  20. <body>
  21. <?php
  22. if(!$fb_user)
  23. {
  24. ?>
  25. <!-- Login Button -->
  26. <fb:login-button v="2" size="medium" onlogin="window.location.reload(true);">Connect with Facebook</fb:login-button>
  27.  
  28. <?php
  29. }
  30. else
  31. {
  32. echo "Facebook";
  33.  
  34. //User
  35. //http://wiki.developers.facebook.com/index.php/Users.getInfo
  36. //can retrive many user information except user's email , check wiki
  37. $user_details=$fb->api_client->users_getInfo($fb_user, array('uid','last_name','first_name','proxied_email','email_hashes'));
  38. $uid=$user_details[0]['uid'];
  39. $firstName=$user_details[0]['first_name'];
  40. $lastName=$user_details[0]['last_name'];
  41. $proxemail=$user_details[0]['proxied_email'];
  42. $email=$user_details[0]['email_hashes'];
  43. echo $uid;
  44. echo "<br>";
  45. echo $email;
  46. echo "<br>";
  47. echo $proxemail;
  48. echo "<br>";
  49. echo $firstName;
  50. echo "<br>";
  51. echo $lastName;
  52.  
  53.  
  54. //Friend
  55. //http://wiki.developers.facebook.com/index.php/Friends.get
  56. echo "<p>Friends:";
  57. $friends = $fb->api_client->friends_get();
  58. //slice the friend list
  59. $friends = array_slice($friends, 0, 5);
  60. foreach ($friends as $friend) {
  61. echo "<br>$friend";
  62. }
  63. echo "</p>";
  64. ?>
  65.  
  66. <!--
  67. http://developers.facebook.com/tools.php?connect_wizard
  68. -->
  69.  
  70. <!-- Profile Picture -->
  71. <fb:profile-pic uid="<?= $uid ?>" size="square" facebook-logo="true"></fb:profile-pic>
  72. <br/>
  73. <!-- Friend Pic -->
  74. <div id="profile_pics"></div>
  75. <script type="text/javascript">
  76. var widget_div = document.getElementById("profile_pics");
  77. FB.ensureInit(function () {
  78. FB.Facebook.get_sessionState().waitUntilReady(function() {
  79. FB.Facebook.apiClient.friends_get(null, function(result) {
  80. var markup = "";
  81. var num_friends = result ? Math.min(5, result.length) : 0;
  82. if (num_friends > 0) {
  83. for (var i=0; i<num_friends; i++) {
  84. markup +=
  85. '<fb:profile-pic size="square" uid="'+result[i]+'" facebook-logo="true"></fb:profile-pic>';
  86. }
  87. }
  88. widget_div.innerHTML = markup;
  89. FB.XFBML.Host.parseDomElement(widget_div);
  90. });
  91. });
  92. });
  93. </script>
  94.  
  95. <?
  96.  
  97. //status
  98.  
  99. $status=$fb->api_client->call_method("facebook.status.get", array('uid'=>$uid, 'limit'=>'5'));
  100.  
  101. for($i=0;$i<5;$i++)
  102. {
  103. echo $status[$i]['message'];
  104. echo "<hr>";
  105. }
  106. //you can try
  107. //print_r($status);
  108.  
  109. echo "<br>";
  110.  
  111.  
  112. //Check Permission
  113. $permission = $fb->api_client->Users_hasAppPermission('status_update');
  114. if($permission)
  115. {
  116. //If permission have status update
  117. $msg=$fb->api_client->call_method("facebook.status.set",array('status'=>'I can update stataus from fb connect'));
  118. }
  119. else
  120. {
  121. ?>
  122. <script>
  123. /*
  124. extended permission are
  125. status_update, photo_upload, video_upload, create_note, and share_item
  126. */
  127.  
  128. //Call facebook and return to ornagai.com
  129. //if not get permission , next for Allow, next_cancel for not Allow
  130. location.href="http://www.facebook.com/authorize.php?api_key=<?= $appapikey ?>&v=1.0&ext_perm=status_update&next=http://www.yoursite.com/fbconnect.php&next_cancel=http://www.yoursite.com"
  131.  
  132.  
  133. //You can also use dialog messagebox
  134. /*
  135. FB.Connect.showPermissionDialog("status_update", function(perms) {
  136. if (!perms) {
  137. continue_without_permission();
  138. } else {
  139. save_session();
  140. }
  141. });
  142. */
  143. </script>
  144. <?
  145. }
  146. ?>
  147. <!--
  148. with PHP
  149. $fb->logout( "http://site.com/returnAfterLogout.php" )
  150. -->
  151.  
  152. <a href="javascript:FB.Connect.logout()" >Logout</a>
  153. <?
  154. }//End Facebook user
  155. ?>
  156. </body>
  157. </html>

Report this snippet  

You need to login to post a comment.