简体   繁体   中英

how to pull user information from facebook sdk into my site's database using php

I have enabled facebook login in my site using Facebook's java script sdk. Now i want to pull user's basic information from facebook, and store it in my site's MySQL database. I need this to be done via php as the alternative signup page is implemented in php. If any other better way is possible, please do mention.

edit: i follow that i need to use response object, but where mi supposed to place the object request? and php "post" codes? I am relatively new to php, where do the below code lines belong?

 FB.api('/me', function(response) {
console.log(JSON.stringify(response));

});

Will return an array of values:

{ "id":"101540562372987329832845483", "email":"example@example.com", "first_name":"Bob", [ ... ] }

edit 2:

FB.api does the api call & receives the response object, containing the user information, but do i have to use the exact "code1" below or can i use the other way as in "code2" that i am familiar with?

code1:

FB.login(function(response) {
 if (response.authResponse) {
 console.log('Welcome!  Fetching your information.... ');
 FB.api('/me', function(response) { 
  var fbname=response.name;
  var fbid=response.id;
 $.ajax({
 type: "POST",
 url: page_for_storing_information,
 data: "name="+fbname+"&uid="+fbid,
     success: function(msg){ 
      }
     });
 console.log('Good to see you, ' + response.name + '.');
  });
   } else {
   console.log('User cancelled login or did not fully authorize.');
   }
});

code 2:

$name = mysqli_real_escape_string($con, $_POST['response.name']);
$email = mysqli_real_escape_string($con, $_POST['response.email']);
$id = mysqli_real_escape_string($con, $_POST['response.id']);


$sql="INSERT INTO questbook (name, email, id)
VALUES ('$name', '$email', '$comment', )";

if (!mysqli_query($con,$sql)) {
   die('Error: ' . mysqli_error($con));
 } 

Facebook recommends using the new PHP SDK 4 for the API. It needs PHP 5.4+, make sure you got that on your server. Here are some links to get you started, the Facebook docs are not very detailed about the new PHP SDK (yet):

What you may want to use is the "JavaScript Login Helper".

Edit: I realize that you don´t even need to use the PHP SDK for this, as you even request the basic data via JavaScript. So all you need to do is to use AJAX to call your php file with the database storage code.

Example AJAX code:

var ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = function() {
    if(ajaxRequest.readyState === 4) {
        console.log(ajaxRequest.responseText);
    }
};
//send with POST
ajaxRequest.open('POST','yourscript.php',true);
ajaxRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded'); //like a form
ajaxRequest.send('name=xxxx&fbid=xxxxx');

Not sure if you know how to use MySQL, but you should google for "PDO" - the recommended way to use MySQL with PHP: http://php.net/manual/en/book.pdo.php

经历这个https://developers.facebook.com/docs/reference/php/4.0.0都在这里描述。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM