How to parse a facebook graph api response

15,737

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

Solution 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

Solution 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

Share:
15,737
user9418
Author by

user9418

Updated on June 04, 2022

Comments

  • user9418
    user9418 almost 2 years

    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>';
    
    • Lejdi Prifti
      Lejdi Prifti almost 10 years
      $response->data[0]->id I think
    • bhushya
      bhushya almost 10 years
      can you post php code here?
    • user9418
      user9418 almost 10 years
      No I get 'Cannot use object of type Facebook\GraphObject as array'
    • bhushya
      bhushya almost 10 years
      try this $data = $response->getData() then print_r($data);
    • user9418
      user9418 almost 10 years
      Added code example and @bhushya no that doesn't work either
    • bhushya
      bhushya almost 10 years
      I mean you tried $user_profile->getData() r8? or print_r($user_profile->getProperty('data'));
    • user9418
      user9418 almost 10 years
      print_r($user_profile->getProperty('data')) produce the same output except for [data] => Array, but when I do $user_profile->getProperty('data')[0] I get errors
    • bhushya
      bhushya almost 10 years
    • bhushya
      bhushya almost 10 years
      @user9418 can you explain what exact error u got?
  • user9418
    user9418 almost 10 years
    Thanks, but this doesn't provide any output in the foreach, I've added the array output in your post above. But when you try and iterate over it nothing is generated.
  • bhushya
    bhushya almost 10 years
    what error u got ehrn you printed $user_profile->getProperty('data')[0]
  • user9418
    user9418 almost 10 years
    [2014-07-22 22:33:41] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Cannot use object of type Facebook\GraphObject as array' in /var/www/app/controllers/FacebookController.php:52 Stack trace: #0 [internal function]: Illuminate\Exception\Handler->handleShutdown() #1 {main} [] [] The output without the [0] is gist.github.com/anonymous/8e345f124f7efcd076b5
  • bhushya
    bhushya almost 10 years
    @user9418 Can you try this print_r($album_data->asArray())?? im sure this will work :)
  • RisingSun
    RisingSun over 9 years
    The getProperty function does not capture all use cases. For example, the /me/permission returns an extremely ugly data (no surprise there) that cannot be parsed with getProperty or at least I couldn't find a solution on the internet that used it. However, Bhushya's answer is the way to go. Facebook is so stupid that it returned me a graphobject containing an array containing a graphobject for a single data entry. And hardcoding anything like data[0] is completely unacceptable to me as a programmer. Plus, facebook changes things a lot and hides it in their changelog hidden in the dev site. dtf
  • Tim Marshall
    Tim Marshall over 9 years
    @bhushya +1 I agree, answer should be accepted. It has somewhat helped with my Stackoverflow Question as it is showing data but the outcome is a mess and I don't know how to get value X