php - 如何支持使用Facebook账号登录(facebook-php-sdk-v4)

标签 php facebook facebook-login

我正在尝试使用最新的 php 版本 (facebook-php-sdk-v4) 为我的网站设置一个登录 facebook 选项,但浏览器一直出现此错误:

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in
/home/.../src/Facebook/FacebookSession.php on line 89

正如您应该知道的那样,代码 (FacebookSession.php) 是我尝试实现的 PHP SDK V4 版本的一部分。 ( https://developers.facebook.com/docs/php/gettingstarted/4.0.0 )

这是报告错误的文件部分(我什至将其包含在我的脚本中,但我得到了相同的结果):

$me = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());

这是我放在我网站的 index.php 中的完整代码:

<?php

require_once( 'src/Facebook/FacebookSession.php' );
require_once( 'src/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'src/Facebook/FacebookRequest.php' );
require_once( 'src/Facebook/FacebookResponse.php' );
require_once( 'src/Facebook/FacebookSDKException.php' );
require_once( 'src/Facebook/FacebookRequestException.php' );
require_once( 'src/Facebook/FacebookAuthorizationException.php' );
require_once( 'src/Facebook/GraphObject.php' );

use src\Facebook\FacebookSession;
use src\Facebook\FacebookRedirectLoginHelper;
use src\Facebook\FacebookRequest;
use src\Facebook\FacebookResponse;
use src\Facebook\FacebookSDKException;
use src\Facebook\FacebookRequestException;
use src\Facebook\FacebookAuthorizationException;
use src\Facebook\GraphObject;

// start session
session_start();

// init app with app id and secret
FacebookSession::setDefaultApplication( 'xxxxxxxxxxxx','xxxxxxxxxxxxxxxxxxxxxxxxxxx' );

$helper = new FacebookRedirectLoginHelper('your redirect URL here');
$loginUrl = $helper->getLoginUrl();
// Use the login url on a link or button to redirect to Facebook for authentication

$helper = new FacebookRedirectLoginHelper();
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch(\Exception $ex) {
// When validation fails or other local issues 
}
if ($session) {
// Logged in
}
?>

任何指导将不胜感激。

最佳答案

在 (PHP < 5.4.0) 中运行 Facebook SDK 4.0

请从所有 facebook 页面更改链接方法,例如 FacebookSession.php 示例如下

// change this
$graphObject = (new FacebookRequest( $session, 'GET', '/me' ))->execute()->getGraphObject();

// into this
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
$graphObject = $response->getGraphObject(); 

更改链接方法后请也更改。

// change this  
 if (session_status() !== PHP_SESSION_ACTIVE) {
      throw new FacebookSDKException(
        'Session not active, could not load state.'
      );
    }

// into this
if(session_id() === "") {
    throw new FacebookSDKException(
        'Session not active, could not load state.'
    );
}

关于php - 如何支持使用Facebook账号登录(facebook-php-sdk-v4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23464856/

相关文章:

php - 通过 PHP mysql PDO 根据下拉列表选择的值更新表

Facebook Graph API - 对象洞察不返回任何数据

python - 将深层嵌套的 json 从 facebook 转换为 python 中的数据框

css - 更改 Facebook 评论框的 CSS

php - 使用 MySQL DB 和 iOS 进行 Facebook 登录的安全性

php - 在 Symfony 中包含用于传播 PDF 生成器包的字体

php - 使用 Mockery 在 PHP 中模拟接口(interface)时出错

phpUnderControl 和 CodeBrowser

java - SERVER_ERROR : [code] 1675030 [message]: Error performing query

android - Google Play 应用签名和 Facebook 登录