How to use facebook API?

Asked

Viewed 10,368 times

11

I have a PHP application, in which I need to log into Facebook and count the number of Ikes in a given post or page. I would like to know how to incorporate this into my application. I’ve heard that I should use the Graph Facebook API. I appreciate any help.

  • https://github.com/facebook/facebook-php-sdk-v4 - RTFD

  • This link helped me a lot, teaches me to use via PHP and Javascript. http://tableless.com.br/facebook-api-sdk-php-na-pratica-e-preview-de-como-aprovar-seu-aplicativo/

  • Take a look here: https://developers.facebook.com/products/ and here: https://developers.facebook.com/docs/plugins?locale=pt_BR

  • For what you need, I believe you have it here: https://developers.facebook.com/docs/graph-api

1 answer

13

So, friend, I waited a long time and saw that no one answered. I’ll help you, because coincidentally, I had to create a similar app a few days ago.

Facebook Graph is a Facebook API, for developers to enjoy the data/content that is published/stored on the social network. Therefore, it is necessary security, I say this for first, because see an example of request: https://graph.facebook.com/{pagina}/posts?access_token={token de acesso} Without this token, you can’t do anything.

Thus, the means of generating a token is by accessing the development documentation in the App Tokens subtitle, using the following code:

GET /oauth/access_token?
     client_id={app-id}
    &client_secret={app-secret}
    &grant_type=client_credentials

Note: Whenever it is written GET at the beginning of the code, means it is the code for URL (possible view per browser)

So you need a app-id and app-secret. For this, you need to create a new APP, for your page or whatever the need, take this information and open in 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: inserir a descrição da imagem aqui Note: I left parts of the visible code to be aware of how it is generated

From this token, you can browse all the APP’s dev Facebook.

But come on, look at these examples, with big pages for more fun.

https://graph.facebook.com/9gag/feed - 9gag = page; feed = content to show (there are numerous options, see documentation)

But note that the number of Ikes 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 yes, notice that almost everything disappeared and appeared a field Summary (number of Likes).

If you need more information, just add the comma fields to the links (compare with GET link without filter), for example: https://graph.facebook.com/9gag/feed?fields=likes.limit(1).summary(true),name,link

Having the GET, the rest is thin.

How to get this information

In the documentation there are details of how to use the SDK, in our case, PHP. Unfortunately I had a problem in hosting and I could not use the SDK, so I had to make a small gambiarra to work. Follow 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 don’t remember if it’s exactly like this on the Ikes part, but I think it’s already given you a foundation for study.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.