Example module for custom Facebook integrations - 7.x-2.x
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
Note that these instructions are only applicable for the 7.x-2.x version of the Simple FB Connect module.
If you want to write your own site specific custom module for Facebook integrations, you can use the FacebookSession provided by Simple FB Connect.
Below is a simple module which checks if the currently logged in user has granted us "public_profile" permission. You can use this as an example and modify this to your needs. For example, you can post photos to user's Facebook profile.
Essential parts of the example:
- You need to declare that you are going to use the Facebook PHP SDK classes
- You load the classes via Libraries API (and xautoload so that the classes are loaded only when they are needed)
- You initialize the FB App and get the currently logged in user's FB session like this
simple_fb_connect_initialize(); $session = simple_fb_connect_get_session();
Here's the whole example module. You also need to write the .info file for your module but that is left as an exercise. :)
<?php
/**
* @file
* This is an example module on how to use Facebook PHP SDK v4 together with
* Simple FB Connect. This module don't do much on its own but it demonstrates
* how to make an API call to FB Open Graph.
*/
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequest;
use Facebook\FacebookRequestException;
/**
* Implements hook_libraries_info().
*/
function yourmodule_libraries_info() {
$libraries['facebook-php-sdk-v4'] = array(
'name' => 'Facebook PHP SDK v4',
'vendor url' => 'https://github.com/facebook/facebook-php-sdk-v4',
'download url' => 'https://github.com/facebook/facebook-php-sdk-v4/releases',
'version arguments' => array(
'file' => 'README.md',
// pattern to search for: Stable-4.0.23
'pattern' => '@Stable-(\d*\.\d*\.\d*)@'
),
'xautoload' => function($adapter) {
$adapter->add('Facebook', 'src');
},
);
return $libraries;
}
/**
* Implementation of hook_menu
*/
function yourmodule_menu() {
$items = array();
// go to example/check_permission to see the example
$items['example/check'] = array(
'title' => 'Check FB permissions',
'page callback' => 'yourmodule_check',
'access callback' => TRUE, // update access callback or access arguments!
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Menu callback for example/check
*/
function yourmodule_check() {
// check if user has granted us 'public_profile' permission
if (yourmodule_check_permissions('public_profile')) {
return "permission OK!";
}
else {
return "permission NOT ok";
}
}
/**
* Check if the current user has granted the app given permission
*/
function yourmodule_check_permissions($permission_to_check) {
// initialize FB app and check if we have an active FB session
simple_fb_connect_initialize();
$session = simple_fb_connect_get_session();
if (!$session) {
drupal_set_message('User is not logged in via Simple FB Connect', 'warning');
return FALSE;
}
try {
// make the API call to Facebook
$request = new FacebookRequest($session, 'GET', '/me/permissions');
$permissions = $request->execute()->getGraphObject()->asArray();
// loop through the permissions
foreach ($permissions as $permission) {
if ($permission->permission == $permission_to_check && $permission->status == 'granted') {
return TRUE;
}
}
} catch (FacebookRequestException $ex) {
watchdog(
'YOURMODULE',
'Error checking permission: FacebookRequestException. Error details: @message',
array('@message' => json_encode($ex->getResponse())),
WATCHDOG_WARNING
);
} catch (\Exception $ex) {
watchdog(
'YOURMODULE',
'Error checking permission: Unhandled exception. Error details: @message',
array('@message' => $ex->getMessage()),
WATCHDOG_WARNING
);
}
// We don't have permission or we got an exception during the API call
return FALSE;
}
?>
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion