1

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',
      '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"

Does this mean I have to use xautoload

  • Does this mean I have to use xautoload Not necessarily (although it's a great module), but you'll definitely need an autoloader. PHP doesn't natively know what classes are in what files, or how the namespace hierarchy is structured – Clive Feb 16 '15 at 18:03
  • Ohhh I see what you're doing now...perhaps the library php is simply included too late; the autoloader needs to be registered by the time the file your use statements are in is included – Clive Feb 16 '15 at 18:08
  • Hi @Clive, "but you'll definitely need an autoloader" what's the solution for this? "the autoloader needs to be registered by the time the file your use statements are in is included" Where should I add the use statement then, any suggestions? – Rishi Kulshreshtha Feb 16 '15 at 19:25
  • @Clive, I've updated the question, I was getting error due to this FacebookSession::setDefaultApplication any suggestion why? – Rishi Kulshreshtha Feb 17 '15 at 6:55

Your Answer

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

Browse other questions tagged or ask your own question.