Check for a particular Facebook user permission using PHP

为君一笑 提交于 2019-12-25 04:23:06

问题


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?


回答1:


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;
        }
    }



回答2:


Using facebook-php-sdk-v4-5.0.0, you can check in the following way:

$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.
    }
}


来源:https://stackoverflow.com/questions/28797381/check-for-a-particular-facebook-user-permission-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!