Senhores,
Estou tentando fazer a comunicação via PHP com o SEFAZ (NF-e) porém estou com certa dificuldade.
Vamos lá, segue o meu código:
<?php
//WS SCAN
//$wsdl = "https://www.scan.fazenda.gov.br/NfeStatusServico/NfeStatusServico.asmx?wsdl";
//$wsdl = "https://hom.nfe.fazenda.gov.br/SCAN/NfeStatusServico/NfeStatusServico.asmx?wsdl";
//WS SEFAZ SP
$wsdl = "https://nfe.fazenda.sp.gov.br/nfeweb/services/nfestatusservico.asmx?wsdl";
//$wsdl = "https://homologacao.nfe.fazenda.sp.gov.br/nfeweb/services/nfestatusservico.asmx?wsdl";
//$wsdl = "https://nfe.sefazvirtual.rs.gov.br/ws/nfestatusservico/NfeStatusServico.asmx?wsdl";
$xsi = "http://www.w3.org/2001/XMLSchema-instance";
$xsd = "http://www.w3.org/2001/XMLSchema";
$nfe = "http://www.portalfiscal.inf.br/nfe";
$client = new SoapClient($wsdl, array('local_cert' => "Desenv/chave.pem",
'passphrase' => "senha",
'encoding' => "UTF-8",
'verifypeer' => false,
'verifyhost' => false,
'trace' => 1
) );
$nfeCabecMsg="<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
"<cabecMsg versao=\"1.02\" xmlns=\"http://www.portalfiscal.inf.br/nfe\">".
"<versaoDados>1.07</versaoDados>".
"</cabecMsg>";
$nfeDadosMsg="<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
"<ConsStatServ xmlns:xsi=\"". $xsi ."\" xmlns:xsd=\"".$xsd."\" versao=\"1.07\" xmlns=\"".$nfe."\">".
"<tpAmb>1</tpAmb>".
"<cUF>35</cUF>".
"<xServ>STATUS</xServ>".
"</ConsStatServ>";
$client->nfeStatusServicoNF($nfeCabecMsg,$nfeDadosMsg);
// Exibe Dados:
echo '<b>Funções:</b><br>';
echo '<pre>' . var_dump($client->__getFunctions()) . '</pre>';
echo '<pre>' . var_dump($client->__getTypes()) . '</pre>';
echo '<b>Requisição:</b><br>';
echo '<pre>' . $client->__getLastRequestHeaders() .'<BR>'. $client->__getLastRequest() . '</pre>';
echo '<b>Resposta:</b><br>';
echo '<pre>' . $client->__getLastResponseHeaders() .'<BR>'. $client->__getLastResponse() . '</pre>';
?>
Para este caso eu sempre obtenho essa resposta do SEFAZ SP:
Funções:
array(2) {
[0]=>
string(77) "nfeStatusServicoNFResponse nfeStatusServicoNF(nfeStatusServicoNF $parameters)"
[1]=>
string(77) "nfeStatusServicoNFResponse nfeStatusServicoNF(nfeStatusServicoNF $parameters)"
}
array(2) {
[0]=>
string(71) "struct nfeStatusServicoNF {
string nfeCabecMsg;
string nfeDadosMsg;
}"
[1]=>
string(71) "struct nfeStatusServicoNFResponse {
string nfeStatusServicoNFResult;
}"
}
Requisição:
POST /nfeweb/services/nfestatusservico.asmx HTTP/1.1
Host: nfe.fazenda.sp.gov.br
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.2.6-1+lenny2
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico/nfeStatusServicoNF"
Content-Length: 607
<?xml version="1.0" encoding="UTF-8"?><ConsStatServ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" versao="1.07" xmlns="http://www.portalfiscal.inf.br/nfe"><tpAmb>1</tpAmb><cUF>35</cUF><xServ>STATUS</xServ></ConsStatServ>
Resposta:
HTTP/1.1 200 OK
Date: Thu, 05 Mar 2009 15:38:04 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 844
<?xml version="1.0" encoding="UTF-8"?><retConsStatServ xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.07"><tpAmb>1</tpAmb><verAplic>SP_NFE_PL_005c</verAplic><cStat>109</cStat><xMotivo>Serviço Paralisado sem Previsão</xMotivo><cUF>35</cUF><dhRecbto>2009-03-05T12:38:04</dhRecbto><tMed>9999</tMed></retConsStatServ>
Quanto deu tento fazer a consulta de status de serviço no SEFAZ do RS ou no SCAN (com o mesmo código) eu sempre recebo este resultado:
<?xml version="1.0" encoding="utf-8"?><retConsStatServ versao="1.07" xmlns="http://www.portalfiscal.inf.br/nfe"><tpAmb>1</tpAmb><verAplic>SVRS20080807095546</verAplic><cStat>243</cStat><xMotivo>Rejeicao: XML Mal Formado</xMotivo><cUF>0</cUF><dhRecbto>2009-03-05T12:45:23</dhRecbto><tMed>1</tMed></retConsStatServ>
Agora quanto eu tento consultar o uma Nota Fiscal com o mesmo código eu recebo o seguinte retorno:
Funções:
array(2) {
[0]=>
string(62) "nfeConsultaNFResponse nfeConsultaNF(nfeConsultaNF $parameters)"
[1]=>
string(62) "nfeConsultaNFResponse nfeConsultaNF(nfeConsultaNF $parameters)"
}
array(2) {
[0]=>
string(66) "struct nfeConsultaNF {
string nfeCabecMsg;
string nfeDadosMsg;
}"
[1]=>
string(61) "struct nfeConsultaNFResponse {
string nfeConsultaNFResult;
}"
}
Requisição:
POST /nfeweb/services/nfeconsulta.asmx HTTP/1.1
Host: nfe.fazenda.sp.gov.br
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.2.6-1+lenny2
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://www.portalfiscal.inf.br/nfe/wsdl/NfeConsulta/nfeConsultaNF"
Content-Length: 642
<?xml version="1.0" encoding="UTF-8"?><consSitNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.07" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><tpAmb>1</tpAmb><xServ>CONSULTAR</xServ><chNFe>35090351938520000180550010000050014069278001</chNFe></consSitNFe>
Resposta:
HTTP/1.1 200 OK
Date: Thu, 05 Mar 2009 15:49:39 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 838
<?xml version="1.0" encoding="UTF-8"?><retConsSitNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.07"><infProt><tpAmb>1</tpAmb><verAplic>SP_NFE_PL_005c</verAplic><cStat>999</cStat><xMotivo>Rejeição: Erro não catalogado - String reference not set to an instance of a String.
Parameter name: s</xMotivo><cUF>35</cUF></infProt></retConsSitNFe>
Eu tenho visto um código em nuSOAP. Existe algum motivo para se utilizar o nuSOAP ???
Obrigado,
Gustavo Zamboni