gpt4 book ai didi

php - 为什么 Facebook PHP SDK getUser 总是返回 0?

转载 作者:IT王子 更新时间:2023-10-29 00:57:13 26 4
gpt4 key购买 nike

我正在尝试使用需要 Facebook 用户的一些信息的网站,我正在使用 PHP 和 JS SDK。

我有一个 PHP 函数:

public function isLoggedOnFacebook() {
$user = $this->_facebook->getUser();
if ($user) {
return $this->_facebook->api("/$user");
}
return false;
}

$this->_facebook 中持有来自 SDK 的 facebook 对象的类上。

然后在一个 block 上我这样做:

<?php if (!$this->isLoggedOnFacebook()): ?>
<div>
<fb:login-button show-faces="true" perms="email" width="500" />
</div>
<?php endif ?>

而且 FB JS 环境设置正确(我认为)所以它可以工作。因此,用户会收到弹出窗口并授权网站。

问题是在app被用户授权后$user始终为0,也就是说$facebook->getUser()总是返回0,然后列出用户的人脸,包括已登录的用户,但如果我让它调用 $facebook->api('/me') 或其他任何方式,那么它会抛出无效 token 异常。

我看到过这个问题,但是没看到解决方案,我不知道问题出在哪里,我的想法用完了。

在应用程序部分的开发者 Facebook 页面上有一个网站选项卡,您可以在其中设置您的网站 URL 和您的网站域,我认为这是我的问题的原因,但我不知道正是这些字段应该包含的内容。

最佳答案

我遇到了同样的问题,我发现这是因为 SDK 使用变量 $_REQUEST 并且在我的环境中与 $_GET 合并的情况不正确, $_POST$_COOKIE 变量。

我认为这取决于 PHP 版本,这就是为什么有人通过启用 cookie 使其工作的原因。

我在 base_facebook.php 中找到了这段代码:

protected function getCode() {
if (isset($_REQUEST['code'])) {
if ($this->state !== null &&
isset($_REQUEST['state']) &&
$this->state === $_REQUEST['state']) {

// CSRF state has done its job, so clear it
$this->state = null;
$this->clearPersistentData('state');
return $_REQUEST['code'];
} else {
self::errorLog('CSRF state token does not match one provided.');
return false;
}
}

return false;
}

我通过创建 $server_info 变量对其进行了修改,如下所示。

protected function getCode() {
$server_info = array_merge($_GET, $_POST, $_COOKIE);

if (isset($server_info['code'])) {
if ($this->state !== null &&
isset($server_info['state']) &&
$this->state === $server_info['state']) {

// CSRF state has done its job, so clear it
$this->state = null;
$this->clearPersistentData('state');
return $server_info['code'];
} else {
self::errorLog('CSRF state token does not match one provided.');
return false;
}
}

return false;
}

关于php - 为什么 Facebook PHP SDK getUser 总是返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6790272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com