PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 11, 2022

[FIXED] how to include php file contains 'use' command in another file with class

 February 11, 2022     alias, compiler-errors, facebook-php-sdk, php, require     No comments   

Issue

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!


Solution

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



Answered By - giorgio83
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing