这很容易成为我在使用 FB api 时遇到的最奇怪的问题。也许我对他们的 API 太过分了,但是就这样吧。
我正在为我的网站使用登录按钮。点击后,用户将看到来自 FB 的弹出窗口,告诉他们有关我的应用程序的一些基本信息(基本详细信息、活跃用户数量和它请求的权限)
这一切都很好,花花公子。如果用户接受应用程序,页面将被重新加载,并且从应用程序请求的他们的信息将被插入到数据库中,并且将创建一个会话。
session_start();
require_once("model/functions.php");
require_once("controller.php");
require_once("model/facebook_api/src/facebook.php");
$facebook = new Facebook(array(
'appId' => '123456789',
'secret' => '123456789',
'cookie' => true
));
$access_token = $facebook->getAccessToken();
$facebook->setAccessToken($access_token);
if($access_token != "")
{
$user = $facebook->getUser();
if($user != 0)
{
$user_profile = $facebook->api("/".$user);
$fb_id = $user;
$fb_first_name = $user_profile['first_name'];
$fb_last_name = $user_profile['last_name'];
$fb_email = $user_profile['email'];
// The FB user_id and all of their other info is correct. Seriously!!
echo $fb_id."<br />";
print_r($user_profile);
// Query the DB to see if this person's info from FB is in there
$query = "SELECT *
FROM users
WHERE fb_id = :fb_id";
$stmt = $db->prepare($query);
$stmt->execute(array(':fb_id' => $fb_id));
$count = $stmt->rowCount();
if($count == 1)
{
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$_SESSION['id'] = $row['user_id'];
$_SESSION['fb_id'] = $row['fb_id'];
$_SESSION['first'] = $row['first_name'];
$_SESSION['last'] = $row['last_name'];
$_SESSION['email'] = $row['email'];
$_SESSION['photo'] = $row['photo'];
$_SESSION['accuracy'] = $row['accuracy_rate'];
}
} else
{
$img_data = file_get_contents('https://graph.facebook.com/'.$fb_id.'/picture?type=large');
$save_path = 'img/profile_pics/large/';
file_put_contents($save_path.''.$fb_id.'.jpg', $img_data);
$insert = "INSERT INTO
users
(first_name,
last_name,
email,
photo,
fb_id,
accuracy_rate,
date_joined,
date_joined_int)
VALUES
(:first_name,
:last_name,
:email,
:photo,
:fb_id,
:points,
:date_joined,
:date_int)";
$params = array(':first_name' => $fb_first_name,
':last_name' => $fb_last_name,
':email' => $fb_email,
':photo' => $fb_id.'.jpg',
':fb_id' => $fb_id,
':points' => '100',
':date_joined' => date("M j, Y"),
':date_int' => idate('z'));
$stmt = $db->prepare($insert);
$stmt->execute($params)or die('error');
print_r($params);
// Query the DB to see if this person's info from FB is in there
$query = "SELECT *
FROM users
WHERE fb_id = :fb_id";
$stmt = $db->prepare($query);
$stmt->execute(array(':fb_id' => $fb_id));
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$_SESSION['id'] = $row['user_id'];
$_SESSION['fb_id'] = $row['fb_id'];
$_SESSION['first'] = $row['first_name'];
$_SESSION['last'] = $row['last_name'];
$_SESSION['email'] = $row['email'];
$_SESSION['photo'] = $row['photo'];
$_SESSION['accuracy'] = $row['accuracy_rate'];
}
}
} else
{
}
} else
{
}
每次刷新页面时都会在数据库中插入一行。从未创建任何会话。更奇怪的是,插入到数据库中 fb_id 列中的 facebook id 属于属于测试帐户的帐户,FB 使用该帐户来测试提交以供批准的打开图形操作。我知道是因为在深入研究了数据库之后,我在数据库中看到了属于 FB 开放图形操作测试人员的一行。ID 是“2147483647”。这显然不是我打印出 $user_profile 数组时打印出的 ID。此外,在第一次重新加载页面后,甚至不应该发生插入查询,因为返回的 rowCount 设置为 1。插入查询仅在用户是第一次使用时才应该执行。沙盒模式未开启。我想可能与它有关。但是,它没有。
在不相关的说明中,FB 的 api 是否存在错误
$facebook->api("/me");
这似乎行不通。
$facebook->api("/".$user);
似乎工作得很好。
现场示例可以在这里找到