Facebook API - Check Likes and Comments of Post - php

Is there a way to check the number of likes and comments that a particular post has? I wanna display that number on my site so i need to reference the API somehow to get that result. I am looking for either a PHP or a javascript solution. This will also be done dynamically. I want to keep track of likes posted by my app to different users so I'm thinking i should store the post id when I make the post. Any ideas?

Each post in facebook has a specific Id
You can use graph api to grab this id and see all the comments, likes and shares of it.
You simply have to make a POST call and grab the produced JSON
More information can be found on the links below
http://graph.facebook.com/19292868552_10150189643478553
https://developers.facebook.com/tools/explorer/?method=GET&path=19292868552_10150189643478553
https://developers.facebook.com/docs/reference/api/post/

Related

Get Facebook post's comments using graph API by post URL

I like to get comments of a post by its URL but it seems its impossible by using graph API.
From my observations, I could find these types of posts URLs:
https://www.facebook.com/bbwbooks/photos/a.168788885097.122872.74209335097/10153736545150098/?type=3&theater
https://www.facebook.com/4thirteengroup/posts/1285218878169361
https://www.facebook.com/groups/semsari.malezi/permalink/1159959294084128/
or perhaps some other types.
Is there any idea, how its possible to get comments or at least the ID of the post from the url?
First use facebook developer section https://developers.facebook.com/tools/explorer/1021611514583622?method=GET&path=me%2Fposts%3Ffields%3Did%2Cobject_id%2Ccomments&version=v2.7
Select Application ,
Get access_token with user_posts access rights,
me/posts?fields=id,object_id,comments by calling this api ,you will get all posts with their comments

Facebook Graph API - Pulling photos from tagged posts

I'm using the Facebook Graph API in PHP to pull tagged and uploaded photos from a user. We are currently using the /me/photos... endpoint to grab both kinds of photos. On our side, the code roughly looks like...
$api_call = $this->facebook->api("me/photos?date_format=U&since=$since_time&until=$current_time&limit=".$photo_limit);
// Valid response was received
if(isset($api_call['data'])){
// Get the photos the user is tagged in
$user_tagged_photos = $api_call['data'];
}
Variables in the URL string are just vars we set. We then just do the same thing for uploaded photos.
However, what my team has realized is that users also sometimes are tagged in posts that have associated pictures with them. So, maybe they are tagged in a post with a picture of them, but they aren't physically tagged in the photo.
Is there a method available for grabbing photos from posts a user is tagged in? The graph api reference for /post/ lists "picture" as a field but describes it as:
The picture scraped from any link included with the post.
Which is not what I want. I want the attached picture, just can't seem to find a way to access...
Thanks in advance for any help.
In that case, object_id in the Post object that gets returned when making a request to /{user-id}/tagged should be the ID of any photo/video that is attached to the post the user is tagged in.
Check the docs for the /post endpoint.

like_count for a facebook post

i am working on a social media management system, one thing we do is that we get the number of likes each post has, that used to work but after the updates of the previous 2 years
we are starting to get into troubles,
can you please give me the proper way of getting the like_count of facebook page posts and displaying them on the graph explorer?
ps: i don't want the FQL query to get the lke_counts
You can use the likes connection of a post to get the number of total likes a post has received. This will return a list of all people that have liked a post. To make it even easier for you, you can pass in the parameter summary with its value set to 1 to get the total count of likes.
Take this as an example URL
https://developers.facebook.com/tools/explorer/?method=GET&path=19292868552_10150189643478553%2Flikes%3Fsummary%3D1

Get a list of all the shares of a facebook post

I can get a list of comments and likes just fine using the facebook sdk. But i can't
find a way to get a list of all the users that shared a post (i tried fql as well).
This is the code i used to get the likes:
$facebook = new Facebook($config);
$likes = $facebook->api('/THE_POST_ID/likes',
array('limit'=>9999999999));
At the time of writing this, Facebook Graph API allows you to get the count of shares of a post. Using your code, it would look like the following (please not that I'm not doing any exception handling here for keeping the example simple):
$facebook = new Facebook($config);
// fetch the post data.
$post = $facebook->api('/THE_POST_ID/');
// fetch the count of shares.
$sharesCount = $post["shares"]["count"];
Using the Graph API Explorer you can easily see that.
Unfortunately, the API does not provide a "shares" connection like it does for "likes" and "comments". See Post - Facebook Graph API Documentation for details.
On the other hand, there is a dirty hack for retrieving the list of users (only friends and people who shared publicly) who shared a specific post. The solution is explained on another thread: List of people who shared on facebook. But, I would never suggest using that solution.
Yes you can get list of all the shares of a facebook post.
by accessing:
https://graph.facebook.com/{POST_ID}/sharedposts?access_token={ACCESS_TOKEN}
See:
https://developers.facebook.com/docs/graph-api/reference/v2.5/post
I know you're looking to get at shares through the post_id, but can you settle for finding shares by page_id instead of post_id?
If you can, then you can use the following FQL to get the data you're looking for.
SELECT via_id, created_time, owner_comment
FROM link
WHERE owner = me()
You can then compare via_id against the posts author (page ID) to determine if this share came from the post author in question.
Unfortunately, there seems to be a bug with the data return where some of the via_ids come back as 0. There is a ticket in with Facebook which, at the time of this post, has been open for three weeks at medium priority. I have no idea if this issue is specific to my app or affects everyone, but that query might get you what you want.

Can't get Facebook comments from my post to the website

I am trying to show the comments from my facebook page post on my PHP website. I like the comments to be synced, so where the user enters comments on the facebook post of my page, it will automatically show up on my website. I know how to create the comment box, but it's doesn't pull the comments from the page.
for the comment box I have:
<div class='fb-comments' data-href='https://www.facebook.com/media/set/?set=a.182291248558753.39531.182290545225490&type=1&l=d607ba4dcc' data-num-posts='3' data-width='470'></div>
I am trying to show the comments that were left on my post, but it's not pulling it. I have looked at the graph API but can't figure out which on get the comments and how to get the so called "primary key" for the comments.
I have looked at the following fql:
Stream: but I don't know what the "post_id" is supposed to be. https://developers.facebook.com/docs/reference/fql/stream/
Comment: but I don't know what the "post_id","object_id", or "xid" would be for a post in a page of the an album. I even tried to replace the URL in the example post they had, but it seems like FB FQL can't parse that kinda url. https://developers.facebook.com/docs/reference/fql/comment/
Even when we get a list of comments, I am not sure how the fb:comments plugin can use the stream to populate my page, don't know if it's possible!
Any help would be greatly appreciated.
You could use Facebook PHP SDK for this which you can get from github. You can then use it to retrieve the comments from the post and store them in a database, updating every now and then to stay current. This will mean less requests to Facebook. When a user posts on your site using your form you can then send the post to Facebook using the SDK.
The problem with this method is that you will need to create an app and users will have to authorise the app for it to be able to post as them. So maybe it's not such a good idea, but I've typed all this in now so I'm not going to delete it.

Categories