How to parse a facebook graph api response

空扰寡人 提交于 2020-01-21 09:24:51

问题


I have a facebook graph api request which brings back this response

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

                                [name] => etc

                            )

I've tried to do $reponse->{'backingData:protected'} but it doesn't work.

Also the next set of results is a link to the graph api but the results from this is pure 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
        )

My code

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

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

回答1:


Here is the way:

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



回答2:


For the New version Graph API v2.5 of Facebook Read read data as below :

$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



回答3:


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

getProperty

getProperty(string $name, string $type = 'Facebook\GraphObject') Gets the value of a named key for this graph object. If the value is a scalar (string, number, etc.) it will be returned. If it's an associative array, it will be returned as a GraphObject cast to the appropriate subclass type if provided.

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

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

full list of field in https://developers.facebook.com/docs/graph-api/reference/v2.0/album



来源:https://stackoverflow.com/questions/24897838/how-to-parse-a-facebook-graph-api-response

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