简短描述。我正在为 Facebook 开发Flash游戏。我需要在用户启动应用程序时首次显示注册表。它应检查用户是否已注册(存在于数据库中)。
目的。要实现此目的,我需要获取 Facebook用户的身份证并在 Facebook用户ID 已经存在的情况下检入数据库,在提供任何行动之前。
问题。如何获取 Facebook用户的身份并将其发送至registration.php?我应该有单独的文件来检查用户是否存在,打开应用程序,获取 Facebook用户的身份?
我尝试了什么:目前我已经尝试了类似的东西(代码和评论中的一些解释):
FacebookPermissions.php
include_once "php-sdk/src/facebook.php";
$app_id = 'xxxxx';
$app_url = 'http://www.facebook.com/myPage/app_xxxxx?ref=ts';
$facebook = new Facebook(array(
'appId' => 'your app id',
'secret' => 'your app secret'
));
$user = $facebook->getUser();
if ($user) {
// here should go code to check database with Facebook User's ID
// I don't have ideas how to achieve It correctly
$user_profile = $facebook->api('/me');
$id = $user_profile['id'];
session_start();
$_SESSION['id'] = $id;
// here should redirect to php file where is MySql query for checking?
// if yes, how to redirect It correctly?
}
else
{
// this part is to ask permissions for user
$loginUrl = "http://www.facebook.com/dialog/oauth?client_id=" .
$app_id . "&redirect_uri=" . urlencode($app_url) . "&scope=email";
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
这是用于从表单获取数据并将其插入数据库的php脚本,但它应该仅用于如果用户不存在于数据库中。并且只应在这种情况下显示所有形式(如果用户不存在)。
InsertData.php
<?php
session_start();
$id = $_SESSION['id'];
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$city = $_SESSION['city'];
$email = $_SESSION['email'];
$mysqli = new mysqli("localhost","xxx","xxx","xxx");
if ($stmt = $mysqli->prepare("INSERT INTO users (FB_Id, First_Name, Last_Name, City, Email) VALUE (?,?,?,?,?) "))
{
if (!$mysqli->set_charset("utf8"))
{
printf("Error loading character set utf8: %s\n", $mysqli->error);
}
$stmt->bind_param('sssss', $id, $first_name, $last_name, $city, $email);
$stmt->execute();
if ($stmt->error != '')
{
echo ' error:'.$stmt->error;
} else
{
echo 'success';
}
$stmt->close();
} else
{
echo 'error:'.$mysqli->error;
}
$mysqli->close();
答案 0 :(得分:3)
要获取用户的Facebook ID,您需要进行Graph API调用。要进行API调用,首先需要通过调用代码的这一部分来询问用户权限:
// this part is to ask permissions for user
$loginUrl = "http://www.facebook.com/dialog/oauth?client_id=" .
$app_id . "&redirect_uri=" . urlencode($app_url) . "&scope=email";
echo("<script> top.location.href='" . $loginUrl . "'</script>");
只有在用户授予权限后,您才可以通过调用代码的这一部分来获取用户的Facebook ID
$user_profile = $facebook->api('/me');