<?php
$token = "T6BuPKa33JP2oXtJUsTanOnl5lf3DeFl"; // Seu token
$ref = "teste_" . time();

$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= '<DPS xmlns="http://www.sped.fazenda.gov.br/nfse">';
$xml .= '<infDeclaracaoPrestacaoServico>';
$xml .= '<infRps>';
$xml .= '<serie>1</serie>';
$xml .= '<numero>1</numero>';
$xml .= '<dataEmissao>' . date('Y-m-d\TH:i:s') . '</dataEmissao>';
$xml .= '</infRps>';
$xml .= '<prestador>';
$xml .= '<cnpj>39555689000156</cnpj>';
$xml .= '<inscricaoMunicipal>123456</inscricaoMunicipal>';
$xml .= '</prestador>';
$xml .= '<tomador>';
$xml .= '<cnpj>92841279000235</cnpj>';
$xml .= '<razaoSocial>GREMIO NAUTICO UNIAO</razaoSocial>';
$xml .= '</tomador>';
$xml .= '<codigoMunicipio>4314902</codigoMunicipio>';
$xml .= '<codigoTributacao>070901</codigoTributacao>';
$xml .= '<discriminacao>Teste de emissao de nota fiscal</discriminacao>';
$xml .= '<valorServicos>100.00</valorServicos>';
$xml .= '<tribMun>';
$xml .= '<tpRetISSQN>2</tpRetISSQN>';
$xml .= '</tribMun>';
$xml .= '</infDeclaracaoPrestacaoServico>';
$xml .= '</DPS>';

echo "=== XML ENVIADO ===\n";
echo $xml . "\n\n";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://homologacao.focusnfe.com.br/v2/nfsen?ref=" . urlencode($ref));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/xml',
    'Authorization: Basic ' . base64_encode($token . ':')
]);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$resposta = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);

echo "=== RESPOSTA ===\n";
echo "HTTP Code: " . $httpCode . "\n";
echo "Resposta: " . $resposta . "\n";
if ($curlError) {
    echo "Erro CURL: " . $curlError . "\n";
}
?>