Wes Wes - 5 months ago 28
PHP Question

facebook api " syntax error, unexpected 'use' "

Hello I am trying to use the facebook api to verify a access token and get the user data like email etc..

However when I test it I get this

Parse error: syntax error, unexpected 'use' (T_USE)
.

Here is the code I am using currently.

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


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


FacebookSession::setDefaultApplication('My App ID', 'My Secret');

// Use one of the helper classes to get a FacebookSession object.
// FacebookRedirectLoginHelper
// FacebookCanvasLoginHelper
// FacebookJavaScriptLoginHelper
// or create a FacebookSession with a valid access token:
$session = new FacebookSession('My Access Token');

try {
$me = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
echo $me->getName();
} catch (FacebookRequestException $e) {
// The Graph API returned an error
} catch (\Exception $e) {
// Some other error occurred
}


Any Ideas on how to fix this error? I never used "use" before and I have read the php documentation for it but i still can't figure it out.

Wes Wes
Answer

Well thanks everybody I finally got it working, turns out you have to make sure that the code below is not within a function like ceejayoz said. I just moved it outside my code igniter model class and now it works, thanks.

//We need to load the Facebook api library
require_once( getcwd().'/facebook-php-sdk-v4-4.0-dev/autoload.php' );
require_once( getcwd().'/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookSession.php' );
require_once( getcwd().'/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookRedirectLoginHelper.php' );
require_once( getcwd().'/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookRequest.php' );
require_once( getcwd().'/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookResponse.php' );
require_once( getcwd().'/Facebook/FacebookSDKException.php' );
require_once( getcwd().'/Facebook/FacebookRequestException.php' );
require_once( getcwd().'/Facebook/FacebookAuthorizationException.php' );
require_once( getcwd().'/Facebook/GraphObject.php' );

//Setup the facebook api for Facebook Validate
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
Comments