I am maintaining the WordPress Social Metrics Tracker plugin. The plugin makes GET requests to various social network APIs, including Facebook, to retrieve the number of tim
The endpoint you're looking for is /?id={url}
. The Graph API v2.3 requires at least an App Access Token for this, which would mean that your plugins' users must create an Facebook App first before being able to use this endpoint. This can be done via https://developers.facebook.com/apps/
A sample call would be
https://graph.facebook.com/v2.3/?access_token={app_access_token}&id=http://www.wikipedia.org
which results in
{
"og_object": {
"id": "382267859091",
"title": "Wikipedia",
"type": "website",
"updated_time": "2015-04-14T23:28:48+0000",
"url": "http://www.wikipedia.org/"
},
"share": {
"comment_count": 0,
"share_count": 195105
},
"id": "http://www.wikipedia.org"
}
As you noticed, the likes are missing.
You can get these by issueing a second call by using the returned og_object.id
like this:
https://graph.facebook.com/v2.3/382267859091?fields=likes.summary(true).limit(0)&access_token={app_access_token}
which results in
{
"likes": {
"data": [
],
"summary": {
"total_count": 1298
}
},
"id": "382267859091"
}
You can also do this in one Batch request:
POST https://graph.facebook.com/v2.3
with the field batch
set to
[
{
"method": "GET",
"name": "get-url-stats",
"relative_url": "v2.3/?id=http://www.wikipedia.org",
"omit_response_on_success": false
},
{
"method": "GET",
"name": "likes",
"relative_url": "v2.3/{result=get-url-stats:$.og_object.id}?fields=likes.summary(true).limit(0)"
}
]
and the fields access_token
with your App Access Token. This returns a lengthy response like this (headers omitted for brevity):
[
{
"code": 200,
"headers": [
...
],
"body": "{\n \"og_object\": {\n \"id\": \"382267859091\",\n \"title\": \"Wikipedia\",\n \"type\": \"website\",\n \"updated_time\": \"2015-04-14T23:28:48+0000\",\n \"url\": \"http://www.wikipedia.org/\"\n },\n \"share\": {\n \"comment_count\": 0,\n \"share_count\": 195105\n },\n \"id\": \"http://www.wikipedia.org\"\n}"
},
{
"code": 200,
"headers": [
...
],
"body": "{\n \"likes\": {\n \"data\": [\n \n ],\n \"summary\": {\n \"total_count\": 1298\n }\n },\n \"id\": \"382267859091\"\n}"
}
]
You have to parse each body
property as JSON, and then use the data to create/show your stats.
See