(1) Create a Facebook application here http://developers.facebook.com/apps and configure it in your domain. This step is very simple, put any namespace you want, it will be your application name, and then make sure that your application will be used as the application page and login (not the fan page or something related) and finally specify The URL (leave a blank canvas URL) where you will use the Facebook API (I think this URL should be under HTTPS, but I don’t know why Facebook still allows HTTP, so don’t worry).
Login Configuration:
Just set the url: http://yourdomain.com/
Application Configuration:
for example: http://www.mydomain.com/myfacebookapp/
So, when the user goes to:
http://apps.facebook.com/yourappName
Means that he really visits the first link and at the same time index.php you will need to do everything from below. To know, at the moment you can also set a logo for your application, manage the administrators of your application and get your application identifier and secret code, which you will need to use later in your PHP file.
(If you are confused at this step, you can perform a Google search, this configuration is also easy to find on YouTube)
(2) I always use these files to link my PHP environment with the Facebook API, you can download it here: <a4>
(3) Put these files in a folder called fb.
(4) I will show you the way to obtain data and images from the user, but first he / she should allow your application to receive information from him / her by entering your application. So, for this example, I will use a simple button to enter.
(Remember to replace your app id and secret with “xxx” and “yyy”)
<?php require 'fb/facebook.php'; <html xmlns:fb="http://www.facebook.com/2008/fbml"> <head> <title>Facebook PHP SDK</title> </head> <body> <h1>Facebook PHP SDK</h1> <?php if ($user): ?> <a href="<?php echo $logoutUrl; ?>">Logout</a> <?php else: ?> <div> Login using OAuth 2.0 handled by the PHP SDK: <a href="<?php echo $loginUrl; ?>">Login with Facebook</a> </div> <?php endif ?> <h3>PHP Session</h3> <pre><?php print_r($_SESSION); ?></pre> <?php if ($user): ?> <h3>You</h3> <img src="https://graph.facebook.com/<?php echo $user; ?>/picture"> <h3>Your User Object (/me)</h3> <pre><?php print_r($user_profile); ?></pre> <?php else: ?> <strong><em>You are not Connected.</em></strong> <?php endif ?> </html>
(5) The above example uses the PHP PHP SDK without javascript. So, if a user wants to log in and authorize your application to receive his / her information, the entire page will be redirected to the Facebook permissions page of your application, and then it will return to the main page of your Facebook application (indicated in the configuration when created your application).
(6) The following code will do the same as above, but using javascript and a custom Facebook login button that allows you to set special permissions, as you wrote in your question. Another difference is that instead of redirecting the entire page, a popup will appear.
(Remember to replace your app id and secret with “xxx” and “yyy”)
<?php require 'fb/facebook.php'; $facebook = new Facebook(array( 'appId' => 'xxx', 'secret' => 'yyy', )); <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Facebook PHP SDK</title> </head> <body> <fb:login-button size="small" onlogin="after_login_button()" scope="email, user_about_me, user_birthday, user_status, publish_stream, user_photos, read_stream, friends_likes">Login with facebook</fb:login-button> <div id="fb-root"></div> <script> window.fbAsyncInit = function() { FB.init({ appId: '<?php echo $facebook->getAppID() ?>', cookie: true, xfbml: true, oauth: true }); FB.Event.subscribe('auth.login', function(response) { if (response.authResponse) { </script> </body> </html>
(7) As you can see, the area attribute in the facebook login button determines what special permissions and information your application will require from the user, for example, his / her email (this is always private).
(8) Just to add something, you can only get publicly available information from someone who knows your identifier using the following code:
(Suppose your friend page is on facebook: http://www.facebook.com/foobar or like: <a6> )
//For example: your facebook friend page is http://www.facebook.com/foobar $myFriend = $facebook->api('/foobar'); //For example: your facebook friend page is http://www.facebook.com/users/1002020300010 $myFriend = $facebook->api('/1002020300010'); //Print information like your friend name: echo $myFriend['name']; //Print all information captured: print_r($myFriend);
And to get a picture of a friend:
<img src="https://graph.facebook.com/foobar/picture">
Or:
<img src="https://graph.facebook.com/1002020300010/picture">
Finally, suppose that you have all the user information recorded in variables, you can easily insert all the data into the database table.
Hope this helps :-)