Graph facebook with comments counter no longer works

{Facebook facebook [3]} I created a function in PHP that summed up the number of comments made on Facebook and wordpress to show in each post, but I think because of the updates of the Facebook plugins, the code no longer works.

I have tried to create a new app on facebook to use the most current version, but I have not been successful and I have no idea if I did any wrong part or I did everything wrong.

The code is currently like this, only the facebook counter does not work, but the from wordpress Yes:

function total_number_comment() {

      global $post;
      $url = get_permalink($post->ID);

      $filecontent = file_get_contents('https://graph.facebook.com/?ids='.$url);
      $json = json_decode($filecontent);

      $fb_count = $json->$url->comments;
      if ($fb_count == 0 || !isset($fb_count)) {
          $fb_count = 0;
      }
      $wp_count = (int)get_comments_number();

      echo $fb_count + $wp_count;
  }

And this is the script:

<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'ID',
      xfbml      : true,
      version    : 'v2.4'
    });
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>

Update

I searched for more information about my error and did some tests again and found that it is still working, just not as before. By directly printing the variable"fil filecontent", I get the following result:

{
       "URL da postagem": {
          "og_object": {
             "id": "id",
             "description": "Descrição da postagem",
             "title": "Título da Postagem",
             "type": "Tipo da da Postagem",
             "updated_time": "Data da atualização da Postagem"
          },
          "share": {
             "comment_count": 6,
             "share_count": 37
          },
          "id": "URL da Postagem"
       }
}

Well, it gave error in theson json variable, so I removed it and now I have this result. The question now is I don't know how to get to the part I want, that is the "comment_count".

Could you give me some strength, please?

Author: Filipe Moraes, 2016-10-25

1 answers

You cannot remove the function json_decode.

This function is required to convert the string contained in $filecontent into an object. After that, you can access the property comment_count.

See this line in your code:

$fb_count = $json->$url->comments;

The property comments no longer exists, just look at the contents of the variable filecontent. Where is the property comments?

{
   "URL da postagem": {
      "og_object": {
         "id": "id",
         "description": "Descrição da postagem",
         "title": "Título da Postagem",
         "type": "Tipo da da Postagem",
         "updated_time": "Data da atualização da Postagem"
      },
      "share": {
         "comment_count": 6,
         "share_count": 37
      },
      "id": "URL da Postagem"
   }
}

To get the number of comments, simply access the correct property:

$json = json_decode($filecontent);
$fb_count = $json->$url->share->comment_count;
 1
Author: Filipe Moraes, 2016-10-26 10:17:08