I have an application in PHP, where I need to enter Facebook and count the number of likes in a particular post or page. I would like to know how to embed this in my application. I heard you should use the Graph Facebook API. Thanks for any help.
I have an application in PHP, where I need to enter Facebook and count the number of likes in a particular post or page. I would like to know how to embed this in my application. I heard you should use the Graph Facebook API. Thanks for any help.
Then friend, I waited a long time and saw that no one answered. I will help you, because coincidentally, I had to create an application similar to a few days.
Facebook Graph is a Facebook API, so developers can take advantage of the data / content that is published / stored on the social network. So, I need security, I say this first, for you see an example of a request: https://graph.facebook.com/{pagina}/posts?access_token={token de acesso}
Without this token, you can not do anything.
So, the way to generate a token is by going to the development documentation in the subtitle App Tokens, using the following code:
GET /oauth/access_token?
client_id={app-id}
&client_secret={app-secret}
&grant_type=client_credentials
Note: Whenever this code is written GET
at the beginning of the code, it means that it is the code for URL (possible browser view)
So you need a app-id
and app-secret
. To do this, you need to create a new APP for your page or whatever the need is, take this information and open it at the following URL: https://graph.facebook.com/oauth/access_token?client_id={app-id}&client_secret={app-secret}&grant_type=client_credentials
.
See a real example: Note:Ileftpartsofthecodevisibletobeawareofhowitisgenerated.
Fromthistoken,youcanscourallAPP'sofdevFacebook
.
Butcomeon,lookattheseexamples,withlargepagesformoregrace.
https://graph.facebook.com/9gag/feed
-9gag=page; feed=contenttoshow(therearelotsofoptions,see documentation )
But notice that the number of likes does not appear, so you need to put a filter with GET field
https://graph.facebook.com/9gag/feed?fields=likes.limit(1).summary(true)
- Now, notice that almost everything disappeared and a summary field appeared (number of likes).
If you need more information, just add the fields with commas in the links (compare with GET
of the unfiltered link), for example:
https://graph.facebook.com/9gag/feed?fields=likes.limit(1).summary(true),name,link
Having the GET, the rest is small.
How to get this information
In the documentation there is the details of how to use the SDK, in our case PHP. Unfortunately I had a problem with the hosting and I was not able to use the SDK, so I had to do a little gambiarra to work. Here is the gambiarra:
$JSONcontent = file_get_contents('https://graph.facebook.com/{todo o GET que eu ensinei}');
$graphObject = json_decode($JSONcontent);
foreach($graphObject->data as $post) {
echo 'Likes da postagem ' . $post->name . ': ' . $post->likes->summary["total_count"];
}
I do not remember if it's exactly that part of the likes, but I think it already gave you a basis for studies.