douzi5214 2014-07-04 07:21
浏览 37
已采纳

难以从Facebook图表中解析响应

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

  • 写回答

1条回答 默认 最新

  • douwei1174 2014-07-04 07:48
    关注

    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";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程