512 * 1024) { fwrite(STDERR, "XML vazio, ilegível ou maior que 512 KiB.\n"); exit(1); } $input = ['xml' => $xml]; // Modelo, CRT, data e local vêm do XML. if (($argv[2] ?? '') !== '') { if (!is_file($argv[2])) { fwrite(STDERR, "Arquivo de alíquotas não encontrado.\n"); exit(1); } $rates = json_decode(file_get_contents($argv[2])); if (!is_object($rates) || json_last_error() !== JSON_ERROR_NONE) { fwrite(STDERR, "Informe as alíquotas como objeto JSON.\n"); exit(1); } $input['aliquotasNominais'] = $rates; } $body = json_encode($input, JSON_UNESCAPED_UNICODE); if ($body === false) { fwrite(STDERR, "O conteúdo deve estar em UTF-8.\n"); exit(1); } $ch = curl_init('https://calculadora-rtc.hafiderp.com.br/api/v1/xml/comparar/'); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $body, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => false, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 120, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $key, 'Content-Type: application/json', 'Accept: application/json'], ]); $response = curl_exec($ch); $status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); $failed = $response === false; if (PHP_VERSION_ID < 80000) { curl_close($ch); } if ($failed) { fwrite(STDERR, "Comunicação não concluída; não marque a nota como inválida.\n"); exit(2); } $report = json_decode($response, true); if ($status !== 200 || json_last_error() !== JSON_ERROR_NONE || !is_array($report)) { fwrite(STDERR, 'HTTP ' . $status . "\n" . $response . "\n"); exit(2); } // O relatório contém dados fiscais. Guardar somente no local autorizado do ERP. echo json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n"; // HTTP 200 também pode conter divergências ou verificações parciais. exit(($report['estado'] ?? '') === 'confere' && ($report['completo'] ?? false) === true ? 0 : 2);