如何解析facebook图api响应

时间:2014-07-22 21:05:50

标签: php json facebook facebook-graph-api

我有一个facebook图表api请求,它会带回这个回复

Facebook\GraphObject Object
(
    [backingData:protected] => Array
        (
            [data] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => 111
                                [from] => stdClass Object
                                    (
                                        [id] => 111
                                        [name] => fo bar
                                    )

                                [name] => etc

                            )

我尝试$reponse->{'backingData:protected'},但它不起作用。

下一组结果也是图表api的链接,但结果是纯json。

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

            [next] => https://graph.facebook.com/v2.0/111/albums?access_token=xxxxv&limit=25&after=yyy
        )

我的代码

    $user_profile = (new FacebookRequest(
        $session, 'GET', '/me/albums'
    ))->execute()->getGraphObject();

    echo '<pre>'; print_r($user_profile); echo '</pre>';

3 个答案:

答案 0 :(得分:17)

这是方法:

        $user_profile = (new FacebookRequest(
        $session, 'GET', '/me/albums'
        ))->execute()->getGraphObject();
        $album =  $user_profile->getProperty('data');

        $album_data = $album->asArray();//this will do all job for you..
        foreach($album_data as $row){
            var_dump($row);
        }

答案 1 :(得分:6)

对于Facebook的新版Graph API v2.5读取读取数据如下:

$fb = new \Facebook\Facebook([
        'app_id' => 'KEY HERE',
        'app_secret' => 'SECRET HERE',
        'default_graph_version' => 'v2.5',
    ]);
 $asscee_t ="ACCESS TOKEN HERE";
    $response = $fb->get('/me/friends', $asscee_t);
        $get_data = $response->getDecodedBody(); // for Array resonse
        //$get_data = $response->getDecodedBody(); // For Json format result only
        echo $get_data['summary']['total_count']; die; // Get total number of Friends

答案 2 :(得分:4)

https://developers.facebook.com/docs/php/GraphObject/4.0.0

<强>的getProperty

的getProperty (string $name, string $type = 'Facebook\GraphObject') 获取此图形对象的命名键的值。如果值是标量(字符串,数字等),则返回。如果它是一个关联数组,它将作为GraphObject强制转换为适当的子类类型(如果提供)。

 $user_profile = (new FacebookRequest(
        $session, 'GET', '/me/albums'
    ))->execute()->getGraphObject();

$id = $user_profile->getProperty('id');

https://developers.facebook.com/docs/graph-api/reference/v2.0/album

中的完整字段列表