Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Facebook Developers - Summary of likes requested but API keeps returning just a list of likes - any idea why?? Works fine in the Explorer?

At present I'm trying to write a php plugin that will tell me the number of likes a particular feed has.

I want to use the likes[total_count] field which needs the summary flag set.

I have the following code

$fb = new Facebook\Facebook ( /* app id/secret, etc*/ ) ;

/* the access token is appid|appsecret and it's a public page*/

$fb->setDefaultAccessToken ( $w->accessToken () ) ;

$response = $fb->get ('/xxxxx_xxxxx?fields=likes.limit(1).summary(true),id' ) ;

$graphObject = $response->getGraphObject();

print_r ( $graphObject->asArray () ) ;

The result is

Array (

[likes] => Array (

[0] => Array ( [id] => xxxxxx )

[id] => xxxxx_xxxxx )

I've also tried a number of other things like getGraphNode() (same result), getGraphEdge () (error 620 - response is not GraphEdge - which I was expecting anyway!), I've also tried it with and without the .limit(1) the only difference being I just get more like id's - so the limit seems to be working it's just summary isn't.

When I try the same thing in Graph API Explorer on the facebook developer page I get the following result

"likes": {

"data": [

{

"id": "xxxx"

}

],

"paging": {

"cursors": {

"after": "MTAxNTU4NjU4ODY0NDA0MjI=",

"before": "MTAxNTI0MTQ2NjY2ODIxODc="

}

},

"summary": {

"total_count": 6,

"can_like": true,

"has_liked": false

}

In the API Explorer there is an additional array called summary which I can't see in my array.

Update:

Yahoo answers truncated the important line of code to ... which should read

$response = $fb->get ('/390458134468686_541172056063959?fields=likes

.summary(true),id' ) ;

Anyone any idea why on earth I can't access the summary array - do I have to do something else? maybe make a call to another function to get a summary or something?

Update 2:

ok just found out that this code works to get the total count

$graphObject = $response->getGraphNode();

$edge = $graphObject->getField ('likes' ) ;

$total_count = $edge->getMetaData()['summary']['total_count'] ;

omg seriously is this the only way to get the total_count??? Seems very long winded just to get a bit of data that should be quite easy to obtain!!

Update 3:

Thanks Chris - I was originally using json_decode but getting exactly the same thing - seems the only way to get a summary is to get the edge and then the metadata!

2 Answers

Relevance
  • 5 years ago

    This will do the trick:

    $likesCount = $response->getDecodedBody()[ likes ][ summary ][ total_count ];

  • ?
    Lv 7
    5 years ago

    You can use json_decode.

    $data = json_decode( $json );

    $likes = $data[ "likes" ][ "totalcount" ];

Still have questions? Get your answers by asking now.