3

i'm trying the new Facebook API (4.0) and I have serious problem with this.

The code of my page loginUser.php is:

<?php
require_once("autoload_fb.php");


// init app with app id and secret
FacebookSession::setDefaultApplication( '647345538685342','6ea4dc94874b31a637b6fe368bcfba76' );

// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://www.radiobrunoestate.mumbleserver.it/api/loginUser' );

// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
  // create new session from saved access_token
  $session = new FacebookSession( $_SESSION['fb_token'] );

  // validate the access_token to make sure it's still valid
  try {
    if ( !$session->validate() ) {
      $session = null;
    }
  } catch ( Exception $e ) {
    // catch any exceptions
    $session = null;
  }

} else {
  // no session exists

  try {
    $session = $helper->getSessionFromRedirect();
  } catch( FacebookRequestException $ex ) {
    // When Facebook returns an error
  } catch( Exception $ex ) {
    // When validation fails or other local issues
    echo $ex->message;
  }

}

// see if we have a session
if ( isset( $session ) ) {

  // save the session
  $_SESSION['fb_token'] = $session->getToken();
  // create a session using saved token or the new one we generated at login
  $session = new FacebookSession( $session->getToken() );

  // graph api request for user data
  $request = new FacebookRequest( $session, 'GET', '/me' );
  $response = $request->execute();
  // get response
  $graphObject = $response->getGraphObject()->asArray();

  // print profile data
  echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';

  // print logout url using session and redirect_uri (logout.php page should destroy the session)
  echo '<a href="' . $helper->getLogoutUrl( $session, 'http://yourwebsite.com/app/logout.php' ) . '">Logout</a>';

} else {
  // show login url
  echo '<a href="' . $helper->getLoginUrl( array( 'email', 'user_friends' ) ) . '">Login</a>';
  }
?>

and the autoload_fb.php is simply:

<?php
    session_start();

    require_once( 'Facebook/FacebookSession.php' );
    require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
    require_once( 'Facebook/FacebookRequest.php' );
    require_once( 'Facebook/FacebookResponse.php' );
    require_once( 'Facebook/FacebookSDKException.php' );
    require_once( 'Facebook/FacebookRequestException.php' );
    require_once( 'Facebook/FacebookAuthorizationException.php' );
    require_once( '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;
?>

I get this error on first server:

Fatal error: Class 'FacebookSession' not found in /home/jack/provaprova/api/loginUser.php on line 6

Testing the same code on another Server i have this error:

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/eyikmdnu/public_html/jack/provaprova/api/Facebook/FacebookSession.php on line 140

Where could be the problem? could be a problem of the server configuration?

Thanks

Giacomo Torricelli
  • 764
  • 1
  • 6
  • 21

3 Answers3

2

Facebook PHP SDK v4 requires PHP 5.4. You are most likely using an older version of PHP.

WizKid
  • 4,888
  • 2
  • 23
  • 23
1

I think you have to put the "use" statements also in the top of the "loginUser.php" file.

Suppose you've already made it work, but I think that was the problem.

Maybe this can help someone else.

Alejandro
  • 21
  • 2
0

Your use statements from the include file won't import them for the main file. So you'll need to move the use statements to your main file right after your include of the autoload_fb.php file.

But then you'll have another issue. Your autoloader isn't a real autoloader. Here's more info on that.

Community
  • 1
  • 1
SammyK
  • 983
  • 11
  • 20