使用 PHP 检查特定的 Facebook 用户权限

Check for a particular Facebook user permission using PHP

我有以下代码来获取用户 Facebook 权限:

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

我可以打字:

echo print_r( $graphObject, 1 );

打印所有权限,但我想检查是否已授予特定权限(在我的情况下 'publish_actions')并对其采取行动。如何提取对象的此元素以在 PHP 中执行 'if' 测试?

获取原始响应并使用 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;
        }
    }

使用facebook-php-sdk-v4-5.0.0,可以通过以下方式查看:

$fb = new Facebook\Facebook([
  'app_id'                  =>      $app_id,
  'app_secret'              =>      $app_secret,
  'default_graph_version'   =>      'v2.2',
  ]);

// Getting declined and granted permissions. 
$permissions            =   $fb->get('/me/permissions',$access_token);
$permissions            =   $permissions->getGraphEdge()->asArray();

// Permissions array sample format (For me, only asked for publish_actions
[0] => Array([permission] => public_profile [status] => granted )
[1] => Array([permission] => publish_actions[status] => declined)

foreach($permissions as $key)
{
    if($key['status'] == 'declined')
    {
        // One permission is not granted.
        // Do something. Redirect with an error message.
    }
}