Graph facebook with comment counter does not work anymore

2

I created a function in PHP that added the number of comments made on facebook and wordpress to show in each post, but I think because of facebook plugin updates, the code no longer works.

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

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

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 here's 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 looked for more information about my error and did some tests again and found that it is still working, just not like before. By directly printing the variable "$ 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, I gave the $ json error, so I removed it and now I have this result. The question now is that I do not know how to get into the part I want, which is the

Could you give me some strength, please?

    
asked by anonymous 25.10.2016 / 05:03

1 answer

1

You can not remove the json_decode function.

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

See this line in your code:

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

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

{
   "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, just access the correct property:

$json = json_decode($filecontent);
$fb_count = $json->$url->share->comment_count;
    
26.10.2016 / 12:17