Get facebook share, like, comment count for a url

From LemonWiki共筆
Jump to navigation Jump to search

查詢網址在 Facebook 被按讚、留言與分享的次數

方法[edit]

實際測試雖然網頁上寫按讚數 142 ,但是這是包含按讚與分享的次數。相等於下方結果的 engagement count。

  • 按讚數(估計值)[1]: 位於下方結果 og_object / likes / summary / total_count 所以是 1
  • 分享數: 下方結果 share / share_count 需要扣除按讚數,所以是 142 - 1 = 141
  • 留言數: 下方結果 share / comment_count 所以是 21 。不過跟 og_object / comments / summary / total_count 的 16 不一致 Icon_exclaim.gif

Graph API path: ?fields=id,share,og_object{engagement{count},likes.summary(true).limit(0),comments.limit(0).summary(true)}&id=http://udn.com/news/story/7320/1919727 (API explorer)[2]

{
  "id": "http://udn.com/news/story/7320/1919727",
  "share": {
    "comment_count": 21,
    "share_count": 142
  },
  "og_object": {
    "engagement": {
      "count": 142
    },
    "likes": {
      "data": [
      ],
      "summary": {
        "total_count": 1,
        "can_like": true,
        "has_liked": false
      }
    },
    "comments": {
      "data": [
      ],
      "summary": {
        "order": "ranked",
        "total_count": 16,
        "can_comment": true
      }
    },
    "id": "1117102125049204"
  }
}

其他方法 (log)[edit]

1. 取得 engagement count, comment count & share count

Graph API path: ?id=http://udn.com/news/story/7320/1919727&fields=og_object{engagement{count}},share (API explorer)[3]

{
  "og_object": {
    "engagement": {
      "count": 142
    },
    "id": "1117102125049204"
  },
  "share": {
    "comment_count": 21,
    "share_count": 142
  },
  "id": "http://udn.com/news/story/7320/1919727"
}


2. 只取得 engagement count

Graph API path: ?id=http://udn.com/news/story/7320/1919727&fields=og_object{engagement} (API explorer)[4]

{
  "og_object": {
    "engagement": {
      "count": 142,
      "social_sentence": "142 people like this."
    },
    "id": "1117102125049204"
  },
  "id": "http://udn.com/news/story/7320/1919727"
}

3. 只取得 comment count & share count

Graph API path: http://udn.com/news/story/7320/1919727 (api explorer)

{
  "og_object": {
    "id": "1117102125049204",
    "description": "新北市新店區1名李姓婦人搭乘台北捷運,嗆2名北一女學生未主動讓座,痛罵「這是誰家女兒該做雞吧」、「穿北一女的制服卻不給小...",
    "title": "藤井樹開砲:博愛座是智障設定! | 社會 | 聯合新聞網",
    "type": "article",
    "updated_time": "2016-08-26T00:35:41+0000"
  },
  "share": {
    "comment_count": 21,
    "share_count": 142
  },
  "id": "http://udn.com/news/story/7320/1919727"
}


4. REST API 方法已經無法使用 Icon_exclaim.gif 瀏覽器輸入網址: https://api.facebook.com/method/links.getStats?urls=http://udn.com/news/story/7320/1919727

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<error_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">
<error_code>12</error_code>
<error_msg>
REST API is deprecated for versions v2.1 and higher (12)
</error_msg>
<request_args list="true">
<arg>
<key>method</key>
<value>links.getStats</value>
</arg>
<arg>
<key>urls</key>
<value>http://udn.com/news/story/7320/1919727</value>
</arg>
</request_args>
</error_response>

備註

  • API v. 2.7, v. 2.6 可以使用,更舊版本不確定

references

  1. facebook 提供的 debug 訊息 "total_count represents the approximate number of nodes on the following edges: likes, comments. The actual number of results returned might be different depending on privacy settings."
  2. Facebook Graph API: engagement count breakdown - Stack Overflow
  3. Missing result field in GET requests of the Facebook GRAPH API - Stack Overflow
  4. How to get facebook share, like, comment count for a url with graph api only (in a non-deprecated way) - Stack Overflow

further reading