How to get Facebook Share Count and Comment Count

2019-08-04 05:20发布

What is my Issued?

Im not getting result/output, using my php code?(first issued)

How to input my Access Token on my php code? (Second Issued)

When i enter this URL from browser: https://graph.facebook.com/v2.1/?id=http://google.com

Im getting result JSON:

{
   "share": {
      "comment_count": 0,
      "share_count": 44100483
   },
   "og_object": {
      "id": "1485280808235339",
      "description": "Honoring our veterans #GoogleDoodle",
      "title": "Google",
      "type": "website",
      "updated_time": "2017-11-11T21:40:44+0000"
   },
   "id": "http://google.com"
}

However, When I used my php code I'm not getting any result.

My complete php code below:

        public function getFacebookData($domain)
        {
            try
            {
                $facebook_app_id = "666987483466439";
                $facebook_app_secret = "b3e1a0948513223b0bc51b32a735e2cf";
                $access_token = $facebook_app_id . '|' . $facebook_app_secret;
                $curl_response = $this->curl->get("http://graph.facebook.com/v2.7/?id=" . $domain . '&fields=share&access_token=' . $access_token);   //$curl_response = $this->curl->get("http://graph.facebook.com/v2.1/?id=" . $domain);

$curl_response = $this->curl->get($callback_url . http_build_query($data, '', '&')); if ($curl_response->headers['Status-Code'] == "200") { $parse_response = json_decode($curl_response, true); $fb_share_count = $parse_response['data'][0]['share_count']; $fb_like_count = $parse_response['data'][0]['like_count']; $fb_comment_count = $parse_response['data'][0]['comment_count']; } else { $fb_share_count = 0; $fb_like_count = 0; $fb_comment_count = 0; } $response = array( 'status' => 'success', 'data' => array( 'fb_share_count' => filter_var($fb_share_count, FILTER_SANITIZE_NUMBER_INT), 'fb_like_count' => filter_var($fb_like_count, FILTER_SANITIZE_NUMBER_INT), 'fb_comment_count' => filter_var($fb_comment_count, FILTER_SANITIZE_NUMBER_INT) ) ); } catch (Exception $e) { $response = array( 'status' => 'error', 'msg' => $e->getMessage() ); } return $response; }

Is there any problem with my code or something i missed?Thanks in advance for your help guys!

From Facebook Developer Site Doc: I need to input an Access Token on my code: Application ID|App Secret another Issue. The Application ID|App Secret i input here is working and tested.

How to input my token on my php code?

1条回答
闹够了就滚
2楼-- · 2019-08-04 05:37

I found a solution, but not in php ruther is js

Here is the working Code:

$(function() {
      var url = "http://facebook.com";
      var apiUrl = "https://graph.facebook.com/?ids=" + url;
    
  $.ajax({
      url: apiUrl,
      success: function(result) {
  $.each(result, function(key, val) {
        console.log(key + " - " + val["share"]["share_count"]);
        console.log(key + " - " + val["share"]["comment_count"]);

            var commentCount = val["share"]["comment_count"];
            var shareCount = val["share"]["share_count"];
            $("#fb-like-div").html(shareCount);
            $("#fb-comment-div").html(commentCount);
          });
        }
      });
    });
#fb-like-div,#fb-comment-div{color:red;}
<script type="text/javascript" src="//code.jquery.com/jquery-1.12.4.js"></script>
<div id="fb-like-div">0</div>
<div id="fb-comment-div">0</div>
 

查看更多
登录 后发表回答