5

I am trying to have a login facebook option for my website using the latest php version (facebook-php-sdk-v4), but I just keep getting this error by the browser:

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

And as you should know that code (FacebookSession.php) is part of the PHP SDK V4 version I am trying to implement. (https://developers.facebook.com/docs/php/gettingstarted/4.0.0)

This is the part of the file that reports the error (I have even included it in my script but I get the same result):

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

This is the full code I have put in the index.php of my website:

<?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
}
?>

Any guidance will be greatly appreciated.

2 Answers 2

5

To Run the Facebook SDK 4.0 In (PHP < 5.4.0)

Please change chaining method from all facebook page such as FacebookSession.php Example are below

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

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

After changing the chaining method Please also changed.

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

the error “Parse error: syntax error, unexpected T_OBJECT_OPERATOR in…” is because the SDK v4 requires PHP version 5.4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.