Php – How to support login using a Facebook account (facebook-php-sdk-v4)

facebook, facebook-login, php

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:

<?phprequire_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 sessionsession_start();// init app with app id and secretFacebookSession::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.

Best Solution

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 thisif(session_id() === "") {    throw new FacebookSDKException(        'Session not active, could not load state.'    );}