Check Facebook User Permissions (PHP)
Written by Saran on
March 3, 2013,
Updated
March 4, 2013I assume you are using
Facebook PHP SDK in your project, if you need to check multiple permissions granted by the user using PHP, try following method. We will break string into an array, and then check for the permissions already granted by the user.
[cc lang="php"] $fbPermissions = 'publish_stream,user_hometown,user_subscriptions'; //permissions $permissions_needed = explode(',',$fbPermissions); //create array by splitting string foreach($permissions_needed as $per) //loop through each permission { if (!array_key_exists($per, $user_permissions['data'][0])) { //check whether more permission needed die('We need additional '.$per.' permission to continue!'); } } [/cc]Here's how I've applied above method in real-time: [cc lang="php"] //Required facebook permissions, separated with comma $fbPermissions = 'publish_stream,user_hometown,user_subscriptions';//include sdk include_once("inc/facebook.php");//Facebook API $facebook = new Facebook(array( 'appId' => 'app id', 'secret' => 'app secret', ));$fbuser = $facebook->getUser(); // get user $loginUrl = $facebook->getLoginUrl(array('scope' => $fbPermissions)); //login urlif(!$fbuser) { $loginUrl = $facebook->getLoginUrl(array('scope' => $fbPermissions)); echo '
Login to Facebook'; } else { try { $user_profile = $facebook->api('/me'); //user profile $user_permissions = $facebook->api("/me/permissions"); //list of user permissions } catch (FacebookApiException $e) { $fbuser = null; }$permissions_needed = explode(',',$fbPermissions); // permission required to proceed foreach($permissions_needed as $per) //loop thrugh each permission { if (!array_key_exists($per, $user_permissions['data'][0])) { //if more permission needed show login link die('We need additional '.$per.' permission to continue,
click here!'); } } } [/cc]