Graph facebook with comment counter no longer works

Asked

Viewed 173 times

2

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

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

The code is currently like this, only facebook counter does not work, but 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>

Updating

I searched for more information about my mistake and did some tests again and found that it is still working, just not as before. By directly printing the "$filecontent" variable, 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, there was error in the $json variable, so I removed it and now I have this result. The point now is I don’t know how to get to the part I want, which is the "comment_count".

Could you give me a hand, please?

  • Is there an error? What is the value of the "filecontent" variable? Edit your question and add relevant information.

  • @Filipemoraes Sorry for the lack of information, I will improve now.

  • But you need to use the function json_decode to convert the string contained in $filecontent an object. After that, you can access the property comment_count.

1 answer

1


You cannot remove the function json_decode.

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

See this line in your code:

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

The estate comments no longer exists, just observe 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, just access the correct property:

$json = json_decode($filecontent);
$fb_count = $json->$url->share->comment_count;
  • My goodness, complete lack of attention on my part. Thank you very much, I will have more attention next time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.