Halter
Halter

Reputation: 48

Fatal error: Class 'FacebookRequest' not found

I am certain I'm doing everything right and i know my server settings and facebook settings are right because this worked when i was using zend with an autoloader, however now im not and frankly i dont understand custom autoloaders and the manual wasnt super clear, so that may be my issue. the problem im having is when i declare new FacebookRequest php throws an error saying Fatal error: Class 'FacebookRequest' not found however the file is included is is being used. there are no other errors that the sdk is throwing.

my config.php

require_once('facebook/FacebookSession.php');
require_once('facebook/FacebookRedirectLoginHelper.php');
require_once('facebook/FacebookHttpable.php');
require_once('facebook/FacebookCurl.php');
require_once('facebook/FacebookCurlHttpClient.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;

//start session if its not started already
if(session_id() == '') {
session_start();
}

// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('APPID','SECRET');
$FBHelper = new FacebookRedirectLoginHelper('http://website.org/registration.php', $appId = $FBAppID, $appSecret = $FBAppSecret);

my "website.org/registration.php"

//if were redirecting back from FB auth
try {
    $session = $FBHelper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
    echo $ex;
    die("<br><br>Facebook made an error");
} catch(\Exception $ex) {
    echo $ex;
    die("<br><br>It appears I messed up");
}

//check if were logged in or if we need to further our registration process
if (isset($session)) {
    //try to grab the user data to send to our database
    try {
        $user_profile = (new FacebookRequest(//heres the error
            $session, 'GET', '/me'
        ))->execute()->getGraphObject(GraphUser::className());          
    } catch(FacebookRequestException $e) {
        //if we're here then we failed getting the users data
        echo "Exception occured, code: " . $e->getCode();
        echo " with message: " . $e->getMessage();
    }   

Upvotes: 0

Views: 9571

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173572

The thing to understand about use statements is that they only work inside the file that defines them.

From the documentation:

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.

This works the other way around as well; the parent will not inherit the child file's importing rules either.

You can resolve this in two ways:

  1. Add the necessary use statements inside the registration script,
  2. Always use the canonical name of \Facebook\FacebookRequest instead.

Upvotes: 2

zerkms
zerkms

Reputation: 254934

It's defined in a Facebook namespace.

It means that in every file you must add

use Facebook\FacebookRequest;

so that php knew about this class.

Or you could use a fully qualified name instead: new use \Facebook\FacebookRequest(...)

Upvotes: 4

Related Questions