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

时间:2014-05-05 04:35:00

标签: 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
}
?>

非常感谢任何指导。

2 个答案:

答案 0 :(得分:5)

运行Facebook SDK 4.0 In(PHP <5.4.0)

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

// 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.'
    );
}

答案 1 :(得分:3)

错误“解析错误:语法错误,意外的T_OBJECT_OPERATOR in ...”是因为SDK v4需要PHP版本5.4