Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 832
max13 Avatar asked May 05 '14 04:05

max13


People also ask

How do I integrate Facebook login to my website?

#3: Set Up Facebook Login for Your Website At this point, you'll see Facebook Login among your website app options. Click the Set Up button to get started. Next, you'll fill in the information about how and where you'll use the app. You can add the Facebook Login feature on any app across multiple devices.


2 Answers

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.'
    );
}
like image 164
Abhay Avatar answered Sep 25 '22 09:09

Abhay


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

like image 35
jonas Avatar answered Sep 25 '22 09:09

jonas