Php-graph-sdk: Only "data" key is cast to GraphNode

Created on 14 Nov 2016  路  10Comments  路  Source: facebookarchive/php-graph-sdk

I'm retrieving a page's ratings including their id by requesting: /v2.8/*PAGE_ID*/ratings?fields=open_graph_story. The response has the following format:

{
  "data": [
    {
      "open_graph_story": {
        "message": "*REVIEW_TEXT*",
        "start_time": "2016-11-08T14:09:42+0000",
        "type": "places.rates",
        "data": {
          "rating": {
            "value": 5,
            "scale": 5
          },
          "review_text": "*REVIEW_TEXT*",
          "language": "en",
          "generic_place": {
            "id": "*PAGE_ID*",
            "title": "*PAGE_NAME*",
            "type": "place",
            "url": "*PAGE_URL*"
          }
        },
        "id": "*REVIEW_ID*"
      }
    },
  ]
}

Each object in the data array has a key id that contains the review's story id. However, it is dropped when the reviews are casted into GraphNode objects:

object(Facebook\GraphNodes\GraphEdge)#1878 (5) {
  ["items":protected]=>
  array(25) {
    [0]=>
    object(Facebook\GraphNodes\GraphNode)#1761 (1) {
      ["items":protected]=>
      array(1) {
        ["open_graph_story"]=>
        object(Facebook\GraphNodes\GraphNode)#1762 (1) {
          ["items":protected]=>
          array(4) {
            ["rating"]=>
            object(Facebook\GraphNodes\GraphNode)#1774 (1) {
              ["items":protected]=>
              array(2) {
                ["value"]=>
                int(5)
                ["scale"]=>
                int(5)
              }
            }
            ["review_text"]=>
            string(928) "*REVIEW_TEXT*"
            ["language"]=>
            string(2) "en"
            ["generic_place"]=>
            object(Facebook\GraphNodes\GraphNode)#1760 (1) {
              ["items":protected]=>
              array(4) {
                ["id"]=>
                string(15) "*PAGE_ID*"
                ["title"]=>
                string(10) "*PAGE_NAME*"
                ["type"]=>
                string(5) "place"
                ["url"]=>
                string(35) "*PAGE_URL*"
              }
            }
          }
        }
      }
    }
  }
}

Responsible is this:

// Sometimes Graph is a weirdo and returns a GraphNode under the "data" key
$data = $data['data'];

Graph is being a weirdo and doesn't include all information under the data key.

bug

Most helpful comment

This will be fixed soon, finally! :)

All 10 comments

The missing id field bug might get solved in 6.0.0 via https://github.com/facebook/php-graph-sdk/issues/437 anyway, but please keep in mind.

Hey @foaly-nr1! Yes - you've stumbled upon a bug in the overly-complex Graph-node casting. :/ We should do some digging to find out what's causing this, but until then, a workaround is to bypass the GraphEdges & GraphNodes and just access the array directly with getDecodedBody():

$res = $fb->get('/{pageId}/ratings?fields=open_graph_story', $accessToken);
$ratings = $res->getDecodedBody();
foreach ($ratings['data'] as $rating) {
    var_dump($rating['open_graph_story']['id']);
}

I hope that helps! :)

Thanks @SammyK that's super helpful.

What's causing this is that when you descent through the nodes to cast them, you either cast the full node or when there is a "data" key, you cast the "data" key instead. So any structure that has information on the same level as a data key will lead to this information being lost. Example:

{
    aRandomNode: {
        thisWillBeLost: 123,
        data: {
            thisWillBeUnderRandomNode: 456
        }
}

Hey @foaly-nr1! Good call. I think this is the problem right here. :)

@SammyK @yguedidi
Do you have an example when "Graph is a weirdo" ?

It is not possible to just check like this ?

if (\count($data) === 1)
{
    // Sometimes Graph is a weirdo and returns a GraphNode under the "data" key
    $data = $data['data'];
}

@yguedidi Yes, when grabbing a user's profile picture for example: /me?fields=id,picture would result in something like:

{
  "id": "123456",
  "picture": {
    "data": {
      "is_silhouette": false,
      "url": "https://scontent.xx.fbcdn.net/p50x50/foo.jpg"
    }
  }
}

Ok I test tomorrow but my "patch" seems to be ok

@tolbon can you create a PR please?

@foaly-nr1 Yep tomorrow after my test :)

This will be fixed soon, finally! :)

Was this page helpful?
0 / 5 - 0 ratings