I'm trying to integrate the new Facebook SDK4 for PHP in my Drupal 7.34 website, where I have successfully installed it using the libraries API but after that instead of the code execution I'm getting this error:

Fatal error: Class 'Facebook\FacebookSession' not found in mymodule.module on line 122

Where on line no 122, I've used this:

FacebookSession::setDefaultApplication('my_app_id', 'secret'); Line - 122
$session = FacebookSession::newAppSession(); Line - 123
$request = new FacebookRequest($session, 'GET', '/' . 'my_fb_page_id' . '/feed'); Line - 124

This use to get work perfectly when I was intergrating it in this way:

require (drupal_get_path('module', 'social_feeds') . '/facebook-php-sdk-v4-master/src/Facebook/autoload.php');

Below are the steps that I've used for integrating the Facebook SDK with libraries:

In mymodule.module

/**
 * The minimum version of Facebook SDK required.
 */
define('MYMODULE_MIN_PLUGIN_VERSION', '4.0');

/**
 * Implementation of hook_libraries_info().
 */
function mymodule_libraries_info() {
  return array(
    'mymodule' => array(
      'name' => 'PHP SDK for Facebook APIs',
      'vendor url' => 'http://developers.facebook.com/',
      'download url' => 'https://github.com/facebook/facebook-php-sdk-v4/archive/master.zip',
      'xautoload' => function($adapter) {
        $adapter->add('Facebook', 'src');
      },
      'version arguments' => array(
        'file' => 'facebook-php-sdk-v4-master/src/Facebook/Facebook.php',
        'pattern' => "/const VERSION = '([\d.]*(?!-dev$))/",
        'lines' => 60,
      ),
      'files' => array(
        'php' => array(
          'facebook-php-sdk-v4-master/src/Facebook/autoload.php',
        ),
      ),
    ),
  );
}

and in mymodule.install file I've mentioned this:

/**
 * Implements hook_requirements().
 */
function mymodule_requirements($phase) {
  $requirements = array();

  // Ensure translations don't break at install time
  $t = get_t();

  $library = libraries_detect('mymodule');
  $error_type = isset($library['error']) ? drupal_ucfirst($library['error']) : '';
  $error_message = isset($library['error message']) ? $library['error message'] : '';

  if (empty($library['installed'])) {
      $requirements['mymodule'] = array(
        'title' => $t('MyModule'),
        'value' => $t('@e: At least @a', array('@e' => $error_type, '@a' => MYMODULE_MIN_PLUGIN_VERSION)),
        'severity' => REQUIREMENT_ERROR,
        'description' => $t('!error You need to download the !mymodule, 
        place the mymodule directory in the %path directory on your server 
        and extract the archive
        .', array(
          '!error' => $error_message,
          '!mymodule' => l($t('MyModule'),
          $library['download url']
          ),
          '%path' => 'sites/all/libraries')),
      );
    }
    elseif (version_compare($library['version'], MYMODULE_MIN_PLUGIN_VERSION, '>=')) {
      $requirements['mymodule'] = array(
        'title' => $t('MyModule'),
        'severity' => REQUIREMENT_OK,
        'value' => 'v' . $library['version'] . ' installed',
      );
    }
    else {
      $requirements['mymodule'] = array(
        'title' => $t('MyModule'),
        'value' => $t('At least @a', array('@a' => MYMODULE_MIN_PLUGIN_VERSION)),
        'severity' => REQUIREMENT_ERROR,
        'description' => $t('You need to download a later version of the 
        !mymodule and replace the old version located in the %path directory 
        on your server.', array(
          '!mymodule' => l($t('MyModule'),
          $library['download url']),
          '%path' => $library['library path'])),
      );
    }

  return $requirements;
}

I have also got this in my admin/reports/status

MyModule v4.1.0 installed

Which means I have integrated the Facebook SDK correctly.

The only issue is with this error "Fatal error: Class 'Facebook\FacebookSession' not found in" where I think my xautoload implementation is incorrect, but I your need suggestion over this.

Thanks.

Comments

leewoodman’s picture

Same error here but i am implementing it slightly differently:

$fb_lib_path = function_exists('libraries_get_path') ? libraries_get_path('facebook-php-sdk') : 'sites/all/libraries/facebook-php-sdk';
$fb_platform = $fb_lib_path . '/autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;

FacebookSession::setDefaultApplication('101010101010101','1010101010101010101');

// Use one of the helper classes to get a FacebookSession object.
//   FacebookRedirectLoginHelper
//   FacebookCanvasLoginHelper
//   FacebookJavaScriptLoginHelper
// or create a FacebookSession with a valid access token:
$session = new FacebookSession('B72sN89gIh1heWhiIts30fDgBIg5agxDRa7DZBP3No5XR98sQZDZD');

// Get the GraphUser object for the current user:

try {
  $me = (new FacebookRequest(
    $session, 'GET', '/me'
    ))->execute()->getGraphObject(GraphUser::className());
  echo $me->getName();
} catch (FacebookRequestException $e) {
  // The Graph API returned an error
} catch (\Exception $e) {
  // Some other error occurred
}
donquixote’s picture

https://www.drupal.org/node/1976234
"Always call libraries_load($library_name)"
"Libraries + xautoload works without libraries_load() for BC reasons, but don't count on that!"

Are you calling libraries_load()?
I'm not sure this is the reason for your problem, there could be something else going wrong. Just mentioning it as a first step.

donquixote’s picture

function mymodule_libraries_info() {
  return array(
    'mymodule' => array(

Why is your library named "mymodule" ? This seems wrong.

      'version arguments' => array(
        'file' => 'facebook-php-sdk-v4-master/src/Facebook/Facebook.php',

This suggests that the root folder of the library, as seen by Libraries API, is one level up from where it should be.
So, you probably have
sites/all/libraries/mymodule/facebook-php-sdk-v4-master/src/Facebook/Facebook.php
and sites/all/libraries/mymodule is the root folder for this library, as seen by Libraries API.

This means in the xautoload setup you would need

      'xautoload' => function($adapter) {
        $adapter->add('Facebook', 'facebook-php-sdk-v4-master/src');
      },

But the better solution would be to fix the folder setup for the library.

donquixote’s picture

@leewoodman:
You don't say how you integrate your module / library with xautoload. So I can't really help you.

Rishi Kulshreshtha’s picture

Status: Active » Fixed

Always call libraries_load($library_name)

This worked like a charm, thanks for your help @donquixote.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.