如何在PHP中循环访问Facebook \ GraphUser对象

时间:2014-09-20 01:21:10

标签: php facebook-graph-api

我想获得facebook组中每个墙贴的喜欢总数(如果有的话),但是正常的foreach循环似乎在Facebook \ GraphUser对象中不起作用。

if($session) {

  try {

    $user_profile = (new FacebookRequest(
      $session, 'GET', '/{group_id}/feed'
    ))->execute()->getGraphObject(GraphUser::className());


    foreach ($user_profile as $data) {
      echo '<img src="'.$data['picture'].'"><br>';
      if($data['likes']){
        echo 'total likes for this photo is '.count($data['likes']['data']).'<br>';
      }else{
         echo 'no likes at the moment.<br>';
      }
    }

  } catch(FacebookRequestException $e) {

    echo "Exception occured, code: " . $e->getCode();
    echo " with message: " . $e->getMessage();

  }   

}

以下是GraphUser对象供参考。任何想法如何循环这个?

Facebook\GraphUser Object
(
    [backingData:protected] => Array
        (
            [data] => Array
                (
                   [0] => stdClass Object
                        (
                            [id] => xxxxxxxxx
                            [from] => stdClass Object
                                (
                                    [id] => xxxxxxxxx
                                    [name] => xxxxxxxxx
                                )

                            [to] => stdClass Object
                                (
                                    [data] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [name] => xxxxxxxxx
                                                    [id] => xxxxxxxxx
                                                )

                                        )

                                )

                            [message] => xxxxxxxxx
                            [picture] => xxxxxxxxx
                            [link] => xxxxxxxxx
                            [icon] => xxxxxxxxx
                            [actions] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [name] => Comment
                                            [link] => xxxxxxxxx
                                        )

                                    [1] => stdClass Object
                                        (
                                            [name] => Like
                                            [link] => xxxxxxxxx
                                        )

                                )

                            [privacy] => stdClass Object
                                (
                                    [value] => 
                                )

                            [type] => photo
                            [object_id] => xxxxxxxxx
                            [created_time] => xxxxxxxxx
                            [updated_time] => xxxxxxxxx
                            [likes] => stdClass Object
                                (
                                    [data] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [id] => xxxxxxxxx
                                                    [name] => xxxxxxxxx
                                                )

                                            [1] => stdClass Object
                                                (
                                                    [id] => xxxxxxxxx
                                                    [name] => xxxxxxxxx
                                                )

                                            [2] => stdClass Object
                                                (
                                                    [id] => xxxxxxxxx
                                                    [name] => xxxxxxxxx
                                                )

                                            [3] => stdClass Object
                                                (
                                                    [id] => xxxxxxxxx
                                                    [name] => xxxxxxxxx
                                                )

                                        )

                                    [paging] => stdClass Object
                                        (
                                            [cursors] => stdClass Object
                                                (
                                                    [after] => xxxxxxxxx
                                                    [before] => xxxxxxxxx=
                                                )

                                        )

                                )

                        )

1 个答案:

答案 0 :(得分:1)

进一步挖掘后,我终于得到了一个可行的答案来回答我自己的问题; - )

$x = $user_profile->getProperty('data');    
$y = $x->asArray();