<?php
function enviarWhatsapp($telefono, $mensaje) {
    $instancia = "instance135272";
    $token = "8n0k4s0smxae0ssv";

    $url = "https://api.ultramsg.com/$instancia/messages/chat";

    $params = array(
        'token' => $token,
        'to' => "+51" . $telefono,
        'body' => $mensaje
    );

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => http_build_query($params),
        CURLOPT_HTTPHEADER => array("Content-Type: application/x-www-form-urlencoded"),
    ));

    $response = curl_exec($curl);
    if ($err = curl_error($curl)) {
        return ["success" => false, "error" => $err];
    }
    curl_close($curl);

    return ["success" => true, "response" => $response];
}
?>
