我使用Facebook的Java Script SDK在我的网站中启用了Facebook登录。现在我想从Facebook中提取用户的基本信息,并将其存储在我的网站的MySQL数据库中。由于在PHP中实现了备用标志页面,我需要通过PHP完成。如果有任何其他更好的方法,请提及。

编辑: 我遵循我需要使用响应对象,但我应该在哪里放置对象请求?和PHP“帖子”代码? 我对PHP相对较新,下面的代码线属于哪里?

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

});

将返回一个值数组:

{ “ID”:“101540562372987329832845483”, “电子邮件”:“example@example.com”, “first_name”:“bob”, [...] }

编辑2:

fb.api api调用并收到包含用户信息的响应对象,但我必须使用下面的确切“Code1”,或者我可以使用我熟悉的“Code2”中的另一种方式?

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.');
   }
});
.

代码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建议使用新的PHP SDK 4为API。它需要PHP 5.4+,确保您在您的服务器上达到了。这里有一些链接可以让你入门,Facebook文件不是关于新的PHP SDK(尚):

您可能想要使用的是“JavaScript登录助手”。

编辑:我意识到您甚至不需要使用PHP SDK,因为您甚至通过JavaScript请求基本数据。所以你需要做的就是使用AJAX使用数据库存储代码调用您的PHP文件。

示例ajax代码:

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');
.

不确定您是否知道如何使用MySQL,但您应该谷歌“PDO” - 使用PHP使用MySQL的推荐方式: http://php.net/manual/en/book.pdo.php

其他提示

通过此 - https://developers.facebook.com/docs/reference/php/4.0.0 所有这些都在这里描述。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top