How to make post requests / tanned / profile sharing on facebook

0

I have to do a job where I have to create a code that captures friends / post / tanned and sharing a page on facebook and save it to the database, but I need to do it with more than one page. I have an example that started working for a specific page. But now this is not working, even if I renew 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>
    
asked by anonymous 02.07.2015 / 18:10

1 answer

2

OMaths , come on:

  • Comma is missing% comma at the end:

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

  • In% with%, the variable friends? access_token was never declared

Consider using cURL instead of friends?access_token :

<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 it all right:

    
02.07.2015 / 18:56