PHP: Facebook SDK – Check Facebook User Permissions


Method 1: use method permissions
We use the code following:

//Required facebook permissions, separated with comma
$fbPermissions = 'read_stream,publish_stream,user_birthday,user_location,user_work_history,user_hometown,user_photos';  

//include sdk
include_once("./src/facebook.php");

//Facebook API
$facebook = new Facebook(array(
  'appId'  => 'xxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxx',
));

$fbuser = $facebook->getUser(); // get user
$loginUrl = $facebook->getLoginUrl(array('scope' => $fbPermissions)); //login url

if(!$fbuser)
{
$loginUrl = $facebook->getLoginUrl(array('scope' => $fbPermissions));
echo '<a href="'.$loginUrl.'">Login to</a>';
}
else
{
     try {
        $user_profile = $facebook->api('/me'); //user profile
        $fbPermissions = $facebook->api("/me/permissions"); //list of user permissions
      } catch (FacebookApiException $e) {
        $fbuser = null;
      }

      $permission_needed = 'publish_stream'; // permission required to proceed
      
      if(isset($fbPermissions["data"][0][$permission_needed]) && $fbPermissions["data"][0][$permission_needed]==1){
            //do anything, like post status
            $fbResult = $facebook->api( 
                '/' . $user . '/feed/', 
                'post', 
                array(
                    'access_token' => $_SESSION["fb_".$facebook->getAppId()."_access_token"], 
                    'message' => "Your message", 
                    'link'=>'http://'.$_SERVER['SERVER_NAME']) 
            );
        }
}

If we var_dump($fbPermissions), we’ll receive the result below:
array(2) {
["data"]=>
array(1) {
[0]=>
array(15) {
["installed"]=>
int(1)
["basic_info"]=>
int(1)
["read_stream"]=>
int(1)
["status_update"]=>
int(1)
["photo_upload"]=>
int(1)
["video_upload"]=>
int(1)
["create_note"]=>
int(1)
["share_item"]=>
int(1)
["publish_stream"]=>
int(1)
["publish_actions"]=>
int(1)
["user_birthday"]=>
int(1)
["user_hometown"]=>
int(1)
["user_location"]=>
int(1)
["user_work_history"]=>
int(1)
["user_photos"]=>
int(1)
}
}
["paging"]=>
array(1) {
["next"]=>
string(77) "https://graph.facebook.com/100003975546733/permissions?limit=5000&offset=5000"
}
}

Method 2: Use FQL

$fbPermissions = $facebook->api(array(
    "method" => "fql.query",
    "query" => "SELECT $permission_needed FROM permissions WHERE uid=me()"
        ));
 
if(isset($fbPermissions[0][$permission_needed]) && $fbPermissions[0][$permission_needed]==1){
            //do anything, like post status
            $fbResult = $facebook->api( 
                '/' . $user . '/feed/', 
                'post', 
                array(
                    'access_token' => $_SESSION["fb_".$facebook->getAppId()."_access_token"], 
                    'message' => "Your message", 
                    'link'=>'http://'.$_SERVER['SERVER_NAME']) 
            );
        }     
}

If we var_dump($fbPermissions), we’ll receive the result below:
array(1) {
[0]=>
array(1) {
["publish_stream"]=>
string(1) "1"
}
}

Method 3 Use method users.hasAppPermission

$fbPermissions = $facebook->api(array(
    "method"    => "users.hasAppPermission",
    "ext_perm"   => "publish_stream",
    "uid"       => $user
));

var_dump($fbPermissions);

Result:
string(1) "1"

We do like two methods above.

Leave a Reply