skip to Main Content

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?

2

Answers


  1. 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;
    }
    
    ?>
    
    Login or Signup to reply.
  2. 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"
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search