Skip to content

Instantly share code, notes, and snippets.

@acras
Created October 15, 2015 14:11
Show Gist options
  • Save acras/1e9bb6edde3fdbaac081 to your computer and use it in GitHub Desktop.
Save acras/1e9bb6edde3fdbaac081 to your computer and use it in GitHub Desktop.
Exemplo de envio de NFe em PHP utilizando CURL e a API do Focus NFe (https://focusnfe.com.br/api)
<?php
// Você deve definir isso globalmente para sua aplicação
// Servidor de homologação
$SERVER = "http://homologacao.acrasnfe.acras.com.br";
// Servidor de produção
//$SERVER = "http://producao.acrasnfe.acras.com.br";
$TOKEN = "token_recebido_do_suporte";
$nfe = array(
"natureza_operacao" => 'Remessa de Produtos',
"forma_pagamento" => 0,
"data_emissao" => '2015-09-27T12:00:00-03:00',
"tipo_documento" => 1,
"finalidade_emissao" => 1,
"cnpj_emitente" => '99993153000143',
"nome_destinatario" => 'NF-E EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL',
"cnpj_destinatario" => '10812933000137',
"inscricao_estadual_destinatario" => 'ISENTO',
"telefone_destinatario" => '6132332933',
"logradouro_destinatario" => 'SMAS 6580 PARKSHOPPING',
"numero_destinatario" => '134',
"bairro_destinatario" => 'Zona Industrial (Guará)',
"municipio_destinatario" => 'Brasilia',
"uf_destinatario" => 'DF',
"pais_destinatario" => 'Brasil',
"cep_destinatario" => '71219900',
"icms_base_calculo" => '0',
"icms_valor_total" => '0',
"icms_base_calculo_st" => '0',
"icms_valor_total_st" => '0',
"icms_modalidade_base_calculo" => '0',
"icms_valor" => '0',
"valor_frete" => '0.0000',
"valor_seguro" => '0',
"valor_total" => '2241.66',
"valor_produtos" => '2241.66',
"valor_ipi" => '0',
"modalidade_frete" => '0',
"informacoes_adicionais_contribuinte" => 'Não Incidência ICMS conforme Decisão...',
"nome_transportador" => 'BRASPRESS TRANSPORTES URGENTES LTDA SP',
"cnpj_transportador" => '48740351000165',
"endereco_transportador" => 'RUA CORONEL MARQUES RIBEIRO, 225',
"municipio_transportador" => 'SÃO PAULO',
"uf_transportador" => 'SP',
"inscricao_estadual_transportador" => '116945108113',
"items" => array(
array(
"numero_item" => '1',
"codigo_produto" => '9999999',
"descricao" => 'Perfume Polo Black',
"cfop" => '6949',
"unidade_comercial" => 'un',
"quantidade_comercial" => '5000',
"valor_unitario_comercial" => '0.448332',
"valor_unitario_tributavel" => '0.448332',
"unidade_tributavel" => 'un',
"codigo_ncm" => '49111090',
"quantidade_tributavel" => '5000',
"valor_bruto" => '2241.66',
"icms_situacao_tributaria" => '41',
"icms_origem" => '0',
"pis_situacao_tributaria" => '07',
"cofins_situacao_tributaria" => '07',
"ipi_situacao_tributaria" => '53',
"ipi_codigo_enquadramento_legal" => '999'
)
),
"volumes" => array(
array(
"quantidade" => '2',
"especie" => 'Volumes',
"marca" => '',
"numero" => '',
"peso_bruto" => '36',
"peso_liquido" => '36'
)
),
"duplicatas" => array(
array(
"numero" => 'Pagamento a vista',
"valor" => '2241.66'
)
),
);
print("=> Teste de envio\n");
$ch = curl_init();
// Substituir pela sua identificação interna da nota
$ref = 1;
// caso queira enviar usando o formato YAML, use a linha abaixo
// curl_setopt($ch, CURLOPT_URL, $SERVER."/nfe2/autorizar?ref=" . $ref . "&token=" . $TOKEN);
// formato JSON
curl_setopt($ch, CURLOPT_URL, $SERVER."/nfe2/autorizar.json?ref=" . $ref . "&token=" . $TOKEN);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
// caso queira enviar usando o formato YAML, use a linha abaixo (necessário biblioteca PECL yaml)
// curl_setopt($ch, CURLOPT_POSTFIELDS, yaml_emit($nfe));
// formato JSON
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($nfe));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$body = curl_exec($ch);
$result = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//as três linhas abaixo imprimem as informações retornadas pela API, aqui o seu sistema deverá
//interpretar e lidar com o retorno
print("STATUS: ".$result."\n");
print("BODY: ".$body."\n\n");
print("");
curl_close($ch);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment