0

I am trying to load the facebook SDK (drupal 7) but I am getting the error

Error: Class 'Facebook\Facebook' not found in 
FbPageSyncProcessSubmission::fbpagesync_facebook_sdk_initialize() (

My method

First download the facebook api Facebook API as recommended

cd web/sites/all/libraries
mkdir fbpagesync
composer require facebook/graph-sdk

Install xautoload

drush dl xautoload && drush en -y xautoload

In my custom module declare the library

function fbpagesync_libraries_info() {
  return [
    'fbpagesync' => [
      'name' => 'PHP SDK for Facebook APIs',
      'vendor url' => 'http://developers.facebook.com/',
      'download url' => 'https://github.com/facebook/php-graph-sdk/master.zip',
      'xautoload' => function ($adapter) {
       $adapter->composerJson('composer.json');
     },
      'path' => 'vendor/facebook/graph-sdk/src/Facebook',
      'version arguments' => [
      'file' => 'vendor/facebook/graph-sdk/src/Facebook/Facebook.php',
      'pattern' => "/const VERSION = '([\d.]*(?!-dev$))/",
      'lines' => 60,
    ],
  ],
  ];
}

It looks OK in libraries_load/libraries_detect

array (
 'name' => 'PHP SDK for Facebook APIs',
 'vendor url' => 'http://developers.facebook.com/',
 'download url' => 'https://github.com/facebook/php-graph-sdk/master.zip',
 'xautoload' => 
   Drupal\xautoload\Libraries\SerializableClosureWrapper::__set_state(array(
   'closure' => 
   Closure::__set_state(array(
  )),
 'moduleName' => 'fbpagesync',
 'libraryName' => 'fbpagesync',
)),
'path' => 'vendor/facebook/graph-sdk/src/Facebook',
'version arguments' => 
  array (
  'file' => 'vendor/facebook/graph-sdk/src/Facebook/Facebook.php',
  'pattern' => '/const VERSION = \'([\\d.]*(?!-dev$))/',
  'lines' => 60,
 ),
'info type' => 'module',
'module' => 'fbpagesync',
'machine name' => 'fbpagesync',
'download file url' => '',
'library path' => 'sites/all/libraries/fbpagesync',
'version callback' => 'libraries_get_version',
'files' => 
array (
),
'dependencies' => 
array (
),
'versions' => 
array (
),
'integration files' => 
array (
),
'callbacks' => 
array (
  'info' => 
array (
  0 => 'libraries_prepare_files',
),
'pre-detect' => 
array (
),
'post-detect' => 
array (
  0 => 'libraries_detect_dependencies',
),
'pre-dependencies-load' => 
array (
),
'pre-load' => 
array (
),
'post-load' => 
array (
),
),
'post-load integration files' => false,
'installed' => true,
'version' => '5.6.3',
'loaded' => 0,
)

The calling code is

use Facebook\Facebook;

...

public static function fbpagesync_facebook_sdk_initialize($reset = FALSE) {
 $settings = variable_get('fbpagesync_settings', []);      
 $facebookInit = new Facebook([
    'app_id' => $settings['app_id'],
    'app_secret' => $settings['app_secret'],
    'default_graph_version' => 'v2.2',
  ]);   
} 
0

It was calling the wrong composer.json. Facebook api has it's own.

The fix is

$adapter->composerJson('vendor/facebook/graph-sdk/composer.json');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.