1

I have two files.

in the first file (Facebook.php) i take user data via Facebook (Facebook access token):

<?php

    require_once '../include/Config.php';
    require_once ( '../libs/facebook/autoload.php' );

    use Facebook\FacebookSession;
    use Facebook\FacebookRequest;
    use Facebook\FacebookRequestException;

    function getUserData($token){

      // init app with app id and secret
      FacebookSession::setDefaultApplication( FB_APP_ID,FB_APP_SECRET ); //

      // If you already have a valid access token:
      $session = new FacebookSession($token); // 'access-token'

      // To validate the session:

      try {

           $session->validate();

      } catch (FacebookRequestException $ex) {

           // Session not valid, Graph API returned an exception with the reason.
           echo $ex->getMessage();

      } catch (\Exception $ex) {

            // Graph API returned info, but it may mismatch the current app or have expired.
            echo $ex->getMessage();
      }

     if($session) {

             try {

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


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

                  return $user_profile;

              } catch(FacebookRequestException $e) {

                   echo "Exception occured, code: " . $e->getCode();
                   echo " with message: " . $e->getMessage();
              }
      }

}

?>

and this is the second file for manage user data:

<?php

   require_once '../../../include/Facebook.php';

   class userManager {

       function __construct() {

    }

   /**
   * Get user data from facebook
   * @param user key
   * @return user data if exist else false
   */
   public function facebookSignIn($token){

        $fbUserData = getUserData($token);

        print_r($fbUserData);
   }
}
?>

in the second file i want to manage user data with a class but i think that my script don't run because the code lines in Facebook.php

use Facebook\FacebookSession; 
use Facebook\FacebookRequest; 
use Facebook\FacebookRequestException;

causing an error in compiling time.

if I comment out these lines my script compiles but naturally doesn't retrieve facebook user data.

what is the right way to include 'facebook.php' in my class? where am I wrong?

I apologize but I do not understand 'use' command and how I can use it.

If i include Facebook.php in a test file without class:

<?php

     require_once '../include/Facebook.php';

     $token = $_GET["token"];

     getUserData($token);
?> 

the script run fine!

giorgio83
  • 288
  • 2
  • 14
  • "*causing an error in compiling time*" - what error message do you get? Are we supposed to magically guess what it is? What version of PHP are you running on? `use` was introduced in 5.3. – DCoder Oct 16 '14 at 19:17
  • up to 5.4 i know.. and the script run fine if i include and use it in a test file without class – giorgio83 Oct 16 '14 at 19:20

2 Answers2

2

i found a solution for my problem. The problem is in the include lines in the first file because test.php is in different hierarchy path of second file.

I found the error using these lines of code:

error_reporting(E_ALL);
ini_set('display_errors', 1);

I apologize, but sometimes the most obvious things that are beyond our control.

thanks to everyone for the support

giorgio83
  • 288
  • 2
  • 14
0

I ran into this problem myself early on because I was not accustomed to using these commands. Use commands have to be used within the same file that the functions relative to them are called. So if File A contains the SDK initialization and you try the use commands then your API requests should work. If File B is required or included within File A (or vice-versa), you must add the use commands within File B to initiate any Graph functions but they should still work. It seems use commands only work within the file for which they are located and no others.

In your second file, add the use commands that you need and I believe you'll find it will work. In your specific case, you may also need it within your function.

Phillip
  • 1,558
  • 3
  • 15
  • 25