How do you get user information on facebook and paste it into the database?

I was not sure how to ask him, but I am trying to teach myself how to create a program that uses an api chart. Most of the manuals I have seen are older, and I don’t know how relevant they are now. Essentially, I try to get the “thing” when someone clicks on my application, it says that this application requires your username, etc., and then “Allow or not allow”.

I want him to take the information, and then I can insert it into the database. I use php and have a domain.

I can insert data without problems if I can get the data. I don’t understand how to do this.

I apologize for the vague question, and I searched. Without asking me to write my code for me, just point me in the right direction, maybe in a modern textbook that does what I ask. Thanks.

+8
authentication php facebook
source share
4 answers

(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'; // Create our Application instance (replace this with your appId and secret). $facebook = new Facebook(array( 'appId' => 'xxx', 'secret' => 'yyy', )); // Get User ID $user = $facebook->getUser(); if ($user) { try { // Proceed knowing you have a logged in user who authenticated. $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { error_log($e); $user = null; } } // Login or logout url will be needed depending on current user state. if ($user) { $logoutUrl = $facebook->getLogoutUrl(); } else { $loginUrl = $facebook->getLoginUrl(); } ?> <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', )); // See if there is a user from a cookie $user = $facebook->getUser(); if ($user) { try { // Proceed knowing you have a logged in user who authenticated. $user_profile = $facebook->api('/me'); $logoutUrl = $facebook->getLogoutUrl(); } catch (FacebookApiException $e) { $user = null; } } else { $loginUrl = $facebook->getLoginUrl(); } ?> <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 }); /* This is used with facebook button */ FB.Event.subscribe('auth.login', function(response) { if (response.authResponse) { // Specify the login page (the page in which your fb login button is situated) window.location = 'main.php'; } }); FB.Event.subscribe('auth.logout', function(response) { window.location = 'logout.php'; }); }; (function() { var e = document.createElement('script'); e.async = true; e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; document.getElementById('fb-root').appendChild(e); }()); function after_login_button(){ FB.getLoginStatus(function(response) { if (response.status=="connected") { // If user is connected, redirect to this page window.location = 'main.php'; } }, true); } </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 :-)

+15
source share

It seems that you are looking for creating authentication (login) using Facebook.

You can check Opauth , it processes all this for you and returns you information about the user in the array, which you can easily insert into the database.

See http://opauth.org for a quick demonstration and download of a library containing sample code.

+1
source share

Given that you are using the PHP SDK classes provided by Facebook here

You can do something like this

 $fb = new Facebook(array( 'appId' => 'YOUR_APP_ID', 'secret' => 'YOUR_APP_SECRET' )); $userData = $fb->api('/me','GET'); $userData = serialize($userData); //now userdata is a string, so, insert into the database. 

Remember that in the configuration of your application you must specify the type of information that you want from the user.

0
source share

You can use the php API and the query language that facebook provides (FQL). Here is a link containing a sample request and tutorials http://developers.facebook.com/docs/reference/fql/

0
source share

All Articles