0

As Facebook changed their API and deprecated the old one, I need to get data (likes count, share count, comment count) about single pages.

I figured out how to get data over Facebook graph (example link):

https://graph.facebook.com/?fields=og_object{likes.limit(0).summary(true)},share&ids=http://www.businessinsider.com/airlines-dont-disclose-carrier-fee-that-inflates-ticket-prices-2016-9

But now I don't know how to echo single data (likes count) in php. I tried with json, but had no sucsess:

$json = file_get_contents($xml);
$json_output = json_decode($json);

Any suggestions how to make this work?

Cristik
  • 30,989
  • 25
  • 91
  • 127
bigN
  • 3
  • 2

2 Answers2

1

The API Explorer adds the Access Token automatically, but you have to add it manually in your URL:

https://graph.facebook.com/?fields=og_object{likes.limit(0).summary(true)},share&ids=http://www.businessinsider.com/airlines-dont-disclose-carrier-fee-that-inflates-ticket-prices-2016-9&access_token=xxx

Result:

{
  "http://www.businessinsider.com/airlines-dont-disclose-carrier-fee-that-inflates-ticket-prices-2016-9": {
    "og_object": {
      "likes": {
        "data": [
        ],
        "summary": {
          "total_count": 0,
          "can_like": true,
          "has_liked": false
        }
      },
      "id": "949055545223224"
    },
    "share": {
      "comment_count": 0,
      "share_count": 346
    },
    "id": "http://www.businessinsider.com/airlines-dont-disclose-carrier-fee-that-inflates-ticket-prices-2016-9"
  }
}
andyrandy
  • 72,880
  • 8
  • 113
  • 130
0

The results of json_decode() are Objects. So you can easily browse through like this:

<?php    

$url = 'https://graph.facebook.com/?fields=og_object{likes.limit(0).summary(true)},share&ids=http://www.businessinsider.com/airlines-dont-disclose-carrier-fee-that-inflates-ticket-prices-2016-9';

$json = file_get_contents($url);
$json_output = json_decode($json);
foreach( $json_output as $site=>$data ){
    echo $site."\n";
    echo $data->og_object->likes->summary->total_count;
}

?>
millerf
  • 686
  • 1
  • 8
  • 16
  • This does not work for me. I tried your whole code, but it does not echoing enything. – bigN Sep 05 '16 at 13:49
  • If you prefer put the result of the URL into a variable $json and just do this bit: – millerf Sep 05 '16 at 14:05
  • $json_output = json_decode($json); foreach( $json_output as $site=>$data ){ echo $site."\n"; echo $data->og_object->likes->summary->total_count; } – millerf Sep 05 '16 at 14:05
  • unfortunatelly this still doesn't help me... If I just var_dumb $json_output, it doesn't return me enything. – bigN Sep 05 '16 at 14:22
  • I resolved the problem... I needed to add an access token at the end of $url So $url looks like: $url = "https://graph.facebook.com/v2.7/?fields=og_object%7Blikes.limit(0).summary(true)%7D%2Cshare&ids=http%3A%2F%2Frobinhud.si%2Fdozivetja%2Fkit-preseneti-surferko%2F&access_token="; – bigN Sep 05 '16 at 14:45