<?php
header('Content-Type: application/json');

$cpf = preg_replace('/\D/', '', $_GET['cpf'] ?? '');

if (!$cpf || strlen($cpf) !== 11) {
    http_response_code(400);
    echo json_encode(['error' => 'CPF inválido']);
    exit;
}

$token = 'c5eebbc9-0469-4324-85f6-0c994b42d18a';
$url = 'https://api.amnesiatecnologia.rocks/?token=' . urlencode($token) . '&cpf=' . urlencode($cpf);

$ctx = stream_context_create(['http' => ['timeout' => 10]]);
$result = @file_get_contents($url, false, $ctx);

if ($result === false) {
    http_response_code(502);
    echo json_encode(['error' => 'Falha ao consultar API']);
    exit;
}

echo $result;
