Facebook PHP SDK v5.0 - FacebookRequest not found Error

10,075

You should specifically define the namespace when creating the object:

$request = new Facebook\FacebookRequest(...

or keep with new FacebookRequest but add use Facebook\FacebookRequset at the beginning of your script.

Also pay attention that you're using the Facebook SDK v4 syntax instead of v5. See here for information about how to use FacebookRequest with SDK v5.

Share:
10,075
gurung
Author by

gurung

Always amazed at how little I know.

Updated on June 05, 2022

Comments

  • gurung
    gurung almost 2 years

    I am using PHP-SDK-v5.0 package downloaded from here. I am trying to get post-feeds from a public group following an example provided by facebook. When I try to run the script, it throws an error - Fatal error: Class 'FacebookRequest' not found. Here is the example that throws an error.

    require_once __DIR__. '/Facebook/autoload.php';    
    $request = new FacebookRequest(
          $session,
          'GET',
          '/{group-id}/feed',
          array(
            'fields' => 'full_picture,attachments.limit(3){media,title,type,url},icon,message',
            'limit' => '1'
          )
        );
    
        $response = $request->execute();
        $graphObject = $response->getGraphObject();
        var_dump($graphObject);
    

    I have another simple facebook login script based on the example below, which shares the same directory and works perfectly. So definitely not a path issue.

    session_start();
    require_once __DIR__. '/Facebook/autoload.php';
    $fb = new Facebook\Facebook([
        'app_id' => 'xxxxx',
        'app_secret' => 'xxxxxxxxxxx',
        'default_graph_version' => 'v2.2',
        ]);
    
    $helper = $fb->getRedirectLoginHelper();
    $permissions = ['email', 'public_profile', 'user_friends']; // Optional permissions
    $loginUrl = $helper->getLoginUrl('http://domain.com/fb-app/login-callback.php', $permissions);
    
    echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';
    

    I suspect it is an issue related to namespace of which I am not very familiar. I tried using a solution to this question question, which suggests to use use Facebook\FacebookRequest; but does not work, neither does $request = new Facebook\FacebookRequest. Also I feel that the script is incomplete and is supposed to incorporate an access token. I am still learning stuffs, please help me solve this.

  • gurung
    gurung over 8 years
    I had tried that earlier it throws this error - Fatal error: Call to undefined method Facebook\FacebookRequest::execute()
  • uri2x
    uri2x over 8 years
    That's a new subject :) That happens because you're probably reading a manual of an outdated SDK - v4 instead of v5. Look here for the latest manual on FacebookRequest.
  • gurung
    gurung over 8 years
    Sorry but as I said in my question I have already tried use Facebook\FacebookRequest;. it does not work and using new Facebook\FacebookRequest throws the above-mentioned additional error.
  • uri2x
    uri2x over 8 years
    @gurung, the undefined method is called because you're not using the SDK correctly. You're trying to use a method from Facebook SDK v4, while you've downloaded Facebook SDK v5. See here how to properly use this class (and don't forget to include autoload.php): developers.facebook.com/docs/php/FacebookRequest/5.0.0
  • gurung
    gurung over 8 years
    I have the updated code in the bottom of my question. Please take a look, still throwing an error.
  • uri2x
    uri2x over 8 years
    There's no ->execute() at all on v5. I suggest you take one of the v5 examples as-is and start from there.
  • Lee
    Lee almost 8 years
    actually I'm getting the same thing, and these are in fact as described on the 5.0 docs...
  • Lee
    Lee almost 8 years
    @uri2x developers.facebook.com/docs/graph-api/reference/page/rating‌​s - these are the 5 docs.. ->execute() is there