user9418
user9418

Reputation: 395

How to parse a facebook graph api response

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

Upvotes: 2

Views: 15980

Answers (3)

Vishal J
Vishal J

Reputation: 342

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

Upvotes: 6

bhushya
bhushya

Reputation: 1317

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

Upvotes: 17

weivall
weivall

Reputation: 987

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

Upvotes: 4

Related Questions