Facebook SDK with PHP - Link Image

Asked

Viewed 60 times

0

I’m using the code below for an application for facebook, I’m having difficulty posting the image with link in the image, follow the code.

// Cria a instancia da aplicacao, informando o appid e o secret
$facebook = new Facebook(array(
'appId'  => APP_ID,
 'secret' => APP_SECRET,
));
// habilita suporte para upload de arquivos
$facebook->setFileUploadSupport(true);
// obtem o id do usuario
$user = $facebook->getUser();
if ($user) { // usuario logado
    try {

        // verificar permissao
        $permissions = $facebook->api("/me/permissions");

        $perms = [];
        foreach($permissions['data'] as $perm) {
            if($perm['status'] == 'granted') {
                $perms[] = $perm['permission'];
            }
        }

        // solicita a permissao ate que o usuario aceite para poder continuar
        if(!in_array('publish_actions', $perms)) {
            $loginUrl = $facebook->getLoginUrl(array('scope' => 'publish_actions, user_photos'));
            header("Location: " . $loginUrl);
            exit;
        }

        // publicar foto

        // dados para envio da publicacao da foto
        $post_data = array(
                "message" => "teste_" . time(),
                "image" => '@' . realpath("ok.jpg"), // localizacao da foto
        );

        // publica foto na timeline
        $data['photo'] = $facebook->api("/me/photos", "post", $post_data);
        echo "Foto publicada com sucesso!";

    } catch (FacebookApiException $e) {
            // tratamento de excecao
            echo($e);
            $user = null; }
} else {
    // usuario nao logado, solicitar autenticacao e permissão publish_actions
    $loginUrl = $facebook->getLoginUrl(array('scope' => 'publish_actions'));
    echo "<a href=\"$loginUrl\">Facebook Login</a><br />";
    echo "<strong><em>Voc&ecirc; n&atilde;o esta conectado..</em></strong>";
}

1 answer

1

Browser other questions tagged

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