【发布时间】:2015-05-27 10:01:14
【问题描述】:
简短说明。我正在为 Facebook 开发 Flash 游戏。我需要在用户启动应用程序时首次显示注册表单。它应该检查用户是否已经注册(存在于数据库中)。
Purpose. 为此,我需要获取 Facebook 用户 ID 并在 Facebook 用户 ID 已经存在的情况下检查数据库,然后再提供任何操作。
问题。如何获取Facebook User's Id并将其发送到registration.php?我是否应该有单独的文件来检查用户的存在、打开应用程序、获取 Facebook 用户 ID?
我尝试过的:现在我已经尝试过类似的东西(代码的 cmets 中有一些解释):
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 脚本,但仅当用户不存在于数据库中时才应使用它。并且只有在这种情况下才应该显示表单(如果用户不存在)。
插入数据.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();
【问题讨论】:
标签: php mysql facebook facebook-graph-api redirect