phpfacebookfacebook-graph-apiuser-permissions

Check for a particular Facebook user permission using PHP


I have the following code to get a users Facebook permissions:

$request = new FacebookRequest( $session, 'GET', '/me/permissions' );
$response = $request->execute();
$graphObject = $response->getGraphObject()->asArray();

I can type:

echo print_r( $graphObject, 1 );

to get a print out of all the permissions BUT I want to check whether or not a particular permission has been granted (in my case 'publish_actions') and act on it. How do I extract this element of the object to perform an 'if' test in PHP?


Solution

  • Get the raw response and use json_decode

      $publish_actions_perm = '';
        $array = json_decode($response->getRawResponse(), true);
        foreach($array['data'] as $perm){
            if($perm['permission']=='publish_actions'){
                $publish_actions_perm = $perm['status']; //granted
                break;
            }
        }