<?php

function enviarMidiaPorLink($numero, $mediaType, $mimeType, $caption, $linkArquivo, $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" => $linkArquivo,
        "fileName" => $fileName,
        "delay" => 123,
        "linkPreview" => false, 
    ];

    $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);
}

if (isset($_GET['number']) && isset($_GET['link']) && isset($_GET['fileName']) && isset($_GET['caption'])) {
  $linkArquivo = $_GET['link'];
  $fileName = $_GET['fileName'];
  $numero = $_GET['number'];
  $mediaType = "document"; 
  $mimeType = "application/pdf";
  $caption = $_GET['caption'];
  $nome_instancia = "laurodefreitas";
  $chave_api = "zYzP7ocstxh3Sscefew4FZTCu4ehnM8v436";

  enviarMidiaPorLink($numero, $mediaType, $mimeType, $caption, $linkArquivo, $fileName, $nome_instancia, $chave_api);
}
