How to make post/likes requests/share profiles on facebook

Asked

Viewed 1,137 times

0

I have to do a job where I have to create a code that captures friends/post/likes and shares a page on facebook and stores it in the database, but I have to do this with more than one page. I have an example that started working for a specific page. But now it’s not working out, even though I’m renewing the token, could anyone help? Code below:

<html>
    <head></head>
    <body>
        <?php

            $url = "146659712053598";
            $token="Aqui_Fica_o_Token"
            // Faz a requisição para API do Facebook
            $postagens = file("https://graph.facebook.com/".$url."/posts?access_token=".$token);
            $amigos = file("https://graph.facebook.com/".$url."/friends? access_token=".$token);
            $curtidas= file("https://graph.facebook.com/".$url."/likes?access_token=".$token);
            // Decodifica o retorno em JSON
            $json = json_decode($retorno, false);
            // Retorna o Número de Likes
            echo $postagens[0];
            echo"<br>";
            //echo $amigos[0];
            echo $curtidas[0];
        ?>
    </body>
</html>

1 answer

2


Omaths, come on:

  • Is missing ; semicolon at the end:

    $token="Aqui_Fica_o_Token"
    
  • In file("https://graph.facebook.com/".$url."/friends? access_token=, note that there is a gap between friends? access_token, should be friends?access_token

  • In $json = json_decode($retorno, false);, the variable $retorno was never declared

Consider using Curl instead of file():

<html>
<head></head>
<body>

    <?php

      /**
       * @param $url    URI to page to parse for Open Graph data
       * @return OpenGraph
       */
        function getPage($url) 
        {
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_FAILONERROR, true);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_TIMEOUT, 15);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

            $response = curl_exec($curl);
            $response = json_decode($response, true);

            curl_close($curl);
            return $response;
        }

    // Define valores
    $url    = "SuaPaginaAqui"; // ou ID numérico
    $token  ="ACCESS_TOKEN";

    // Faz a requisição para API do Facebook
    $postagens  = getPage("https://graph.facebook.com/".$url."/posts?access_token=".$token);
    $amigos     = getPage("https://graph.facebook.com/".$url."/friends?access_token=".$token);
    $curtidas   = getPage("https://graph.facebook.com/".$url."/likes?access_token=".$token);

    // Postagens
    echo "<h2>Postagens</h2>";
    if(is_array($postagens) && array_key_exists('data', $postagens))
    {
        foreach ($postagens['data'] as $key => $postagem) 
        {
            echo $postagem['message'] . "<br />";
        }
    }
    else { echo 'Nenhuma postagem'; }


    // Amigos
    echo "<h2>Amigos</h2>";
    if(is_array($amigos) && array_key_exists('data', $amigos))
    {
        foreach ($amigos['data'] as $key => $amigo) 
        {
            //echo $amigo['NAO_SEI_QUAL_CHAVE_USAR'] . "<br />";
        }
    }
    else { echo 'Nenhum amigo'; }


    // Curtidas
    echo "<h2>Curtidas</h2>";
    if(is_array($curtidas) && array_key_exists('data', $curtidas))
    {
        foreach ($curtidas['data'] as $key => $curtida) 
        {
            echo $curtida['name'] . "<br />";
        }
    }
    else { echo 'Nenhuma curtida'; }

    ?>
</body>
</html>

I tested here with my page and returned all right:

inserir a descrição da imagem aqui

  • Thank you Felipe, helped and very much, my code was very ruinzinho, kkk I will study better your code. Vlw even! ;)

  • Opa, hehe, imagine, there is no bad or good! It is that Curl allows much greater freedom! Vote on the answer if it was the most suitable, thank you!

  • Felipe, just a little question, if I want to access who liked or shared a certain post as I do?

  • Whom compartilhou has no way (the face does not provide), but who liked is only access getPage("https://graph.facebook.com/<ID_DO_POST>?access_token=".$token);, will see various keys and information about the post, one of them is the ['likes'] and ['comments']

Browser other questions tagged

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