简体   繁体   中英

Facebook PHP SDK v5.0 - FacebookRequest not found Error

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM