NFePHP\NFe\ToolsNFe::sefazCadastro PHP Метод

sefazCadastro() публичный Метод

sefazCadastro Busca os dados cadastrais de um emitente de NFe NOTA: Nem todas as Sefaz disponibilizam esse serviço
public sefazCadastro ( string $siglaUF = '', string $tpAmb = '2', string $cnpj = '', string $iest = '', string $cpf = '', array &$aRetorno = [] ) : string
$siglaUF string sigla da UF da empresa que queremos consultar
$tpAmb string
$cnpj string numero do CNPJ da empresa a ser consultada
$iest string numero da Insc. Est. da empresa a ser consultada
$cpf string CPF da pessoa física a ser consultada
$aRetorno array aRetorno retorno da resposta da SEFAZ em array
Результат string XML de retorno do SEFAZ
    public function sefazCadastro($siglaUF = '', $tpAmb = '2', $cnpj = '', $iest = '', $cpf = '', &$aRetorno = array())
    {
        $cnpj = preg_replace('/[^0-9]/', '', $cnpj);
        $cpf = preg_replace('/[^0-9]/', '', $cpf);
        $iest = trim($iest);
        //se nenhum critério é satisfeito
        if ($cnpj == '' && $iest == '' && $cpf == '') {
            //erro nao foi passado parametro de filtragem
            $msg = "Na consulta de cadastro, pelo menos um desses dados deve ser" . " fornecido CNPJ, CPF ou IE !!!";
            throw new Exception\InvalidArgumentException($msg);
        }
        if ($tpAmb == '') {
            $tpAmb = $this->aConfig['tpAmb'];
        }
        //selecionar o criterio de filtragem CNPJ ou IE ou CPF
        if ($cnpj != '') {
            $filtro = "<CNPJ>{$cnpj}</CNPJ>";
            $txtFile = "CNPJ_{$cnpj}";
        } elseif ($iest != '') {
            $filtro = "<IE>{$iest}</IE>";
            $txtFile = "IE_{$iest}";
        } else {
            $filtro = "<CPF>{$cpf}</CPF>";
            $txtFile = "CPF_{$cpf}";
        }
        //carrega serviço
        $servico = 'NfeConsultaCadastro';
        $this->zLoadServico('nfe', $servico, $siglaUF, $tpAmb);
        if ($this->urlService == '') {
            $msg = "A consulta de Cadastros não está disponível na SEFAZ {$siglaUF}!!!";
            throw new Exception\RuntimeException($msg);
        }
        $cons = "<ConsCad xmlns=\"{$this->urlPortal}\" versao=\"{$this->urlVersion}\">" . "<infCons>" . "<xServ>CONS-CAD</xServ>" . "<UF>{$siglaUF}</UF>" . "{$filtro}</infCons></ConsCad>";
        //validar mensagem com xsd
        //if (! $this->validarXml($cons)) {
        //    $msg = 'Falha na validação. '.$this->error;
        //    throw new Exception\RuntimeException($msg);
        //}
        //montagem dos dados da mensagem SOAP
        $body = "<nfeDadosMsg xmlns=\"{$this->urlNamespace}\">{$cons}</nfeDadosMsg>";
        //envia a solicitação via SOAP
        $retorno = $this->oSoap->send($this->urlService, $this->urlNamespace, $this->urlHeader, $body, $this->urlMethod);
        $lastMsg = $this->oSoap->lastMsg;
        $this->soapDebug = $this->oSoap->soapDebug;
        //salva mensagens
        $filename = "{$txtFile}-consCad.xml";
        $this->zGravaFile('nfe', $tpAmb, $filename, $lastMsg);
        $filename = "{$txtFile}-retConsCad.xml";
        $this->zGravaFile('nfe', $tpAmb, $filename, $retorno);
        //tratar dados de retorno
        $aRetorno = ReturnNFe::readReturnSefaz($servico, $retorno);
        return (string) $retorno;
    }

Usage Example

Пример #1
0
<?php

error_reporting(E_ALL);
ini_set('display_errors', 'On');
require_once '../../bootstrap.php';
use NFePHP\NFe\ToolsNFe;
$nfe = new ToolsNFe('../../config/config.json');
$nfe->setModelo('55');
$aResposta = array();
$siglaUF = $nfe->aConfig['siglaUF'];
$tpAmb = '2';
$cnpj = $nfe->aConfig['cnpj'];
// Consulta por CNPJ ou IE ou CPF
$iest = '';
$cpf = '';
$retorno = $nfe->sefazCadastro($siglaUF, $tpAmb, $cnpj, $iest, $cpf, $aResposta);
echo '<br><br><pre>';
echo htmlspecialchars($nfe->soapDebug);
echo '</pre><br><pre>';
print_r($aResposta);
echo "</pre><br>";