phpjsonfacebookfacebook-graph-api

Having trouble parsing a response from Facebook graph


I am trying to find out if the permission for 'publish_action' has been granted for a user (using php).

I can query the graph ok, I am just stuck on parsing the response to be able to get publish_actions = 'granted'.

Query is:

$user_permissions = "https://graph.facebook.com/me/permissions?access_token=" .$access_token;

$permissions = json_decode(file_get_contents($user_permissions));

If I print the response

print_r($permissions);

I get...

stdClass Object ( [data] => Array ( [0] => stdClass Object ( [permission] => installed [status] => granted ) [1] => stdClass Object ( [permission] => public_profile [status] => granted ) [2] => stdClass Object ( [permission] => publish_actions [status] => granted ) ) )

Catchable fatal error: Object of class stdClass could not be converted to string

Basically I just want to be able to assign a variable in php if publish_actions[status] == 'granted'

Any help with this would be greatly appreciated.

Update

Trying what Sahil below suggested:

$user_permissions = "https://graph.facebook.com/me/permissions?access_token=" .$access_token;

$permissions = json_decode(file_get_contents($user_permissions, TRUE));

$hasPublishPerm = false;
print_r($permissions['data']);
print_r($permissions);
foreach($permissions['data'] as $permission){
   if($permission['permission']=="publish_actions" && 
      $permission['status']=="granted"){
        $hasPublishPerm = true;
        break;
   }
}

if($hasPublishPerm){
   echo "User has granted the publish permission";
}
else{
   echo "User has NOT granted the publish permission";
}

I get this error:

Fatal error: Cannot use object of type stdClass as array in...


Solution

  • Just do this-

    $hasPublishPerm = false;
    foreach($permissions['data'] as $permission){
       if($permission['permission']=="publish_actions" && 
          $permission['status']=="grantedd"){
            $hasPublishPerm = true;
            break;
       }
    }
    
    if($hasPublishPerm){
       echo "User has granted the publish permission";
    }
    else{
       echo "User has NOT granted the publish permission";
    }