<?php

function enviarMidia($numero, $mediaType, $mimeType, $caption, $mediaBase64, $fileName, $nome_instancia, $chave_api) {
    $url = "https://storage.essencialapi.app.br/message/sendMedia/{$nome_instancia}";

    $dados = [
        "number" => $numero,
        "mediatype" => $mediaType,
        "mimetype" => $mimeType,
        "caption" => $caption,
        "media" => $mediaBase64,
        "fileName" => $fileName,
        "delay" => 123,
        "linkPreview" => true,
    ];

    $payload = json_encode($dados);

    $headers = [
        "Content-Type: application/json",
        "apikey: {$chave_api}"
    ];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $resposta = curl_exec($ch);

    if ($resposta === false) {
        echo "Erro no curl: " . curl_error($ch);
    } else {
        echo "Resposta da API: " . $resposta;
    }

    curl_close($ch);
}


$urlImagem = "https://pbs.twimg.com/media/EMF95opWsAAG3ZY.jpg";


$conteudo = file_get_contents($urlImagem);

if ($conteudo === false) {
    die("Erro ao baixar a imagem da URL.");
}


$mediaBase64 = base64_encode($conteudo);


$fileName = basename($urlImagem);


$numero = "5574998181834";
$mediaType = "image";
$mimeType = "image/jpeg"; 
$caption = "Foto enviada via API";
$nome_instancia = "haps";
$chave_api = "zYzP7ocstxh3Sscefew4FZTCu4ehnM8v436";

enviarMidia($numero, $mediaType, $mimeType, $caption, $mediaBase64, $fileName, $nome_instancia, $chave_api);
