0
I receive as response the following XML
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<obterLoteCupomResponse xmlns="http://ws.pc.gif.com.br/">
<?xml version="1.0" encoding="UTF-8"?>
<resultadoLoteCupom versao="1.0">
<CNPJ>93109437000149</CNPJ>
<cLote>58735</cLote>
<dhRecbto>2019-09-17 18:07:28</dhRecbto>
<sit>100</sit>
<NFSe>
<chvAcessoNFSe>4393109437000149900CF000000007300402234</chvAcessoNFSe>
<sit>200</sit>
<motivos>
<mot>Error1159 Nota fiscal já foi emitida</mot>
</motivos>
</NFSe>
</resultadoLoteCupom>
</obterLoteCupomResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I need to convert to Objects to access the properties. I’m trying with the function below but unsuccessfully.
public static function xml2array($xmlString)
{
$xml = simplexml_load_string($xmlString);
$json = json_encode($xml);
$array = json_decode($json, TRUE);
return $array;
}
See if you can help https://answall.com/a/410393/137387, the second part of the answer, the difficult problem, if it fits the scenario you are facing.
– Augusto Vasques
@Augustovasques is still the same. Error "simplexml_load_string(): Entity: line 1: parser error : XML declaration allowed only at the start of the Document"
– Reação Sistemas
Your XML is incomplete put
<?xml version="1.0" encoding="utf-8"?>
in the first line.– Augusto Vasques
@Augustovasques I get it so from the server, I can’t add or remove anything.
– Reação Sistemas
$xmlString .= '<?xml version="1.0" encoding="utf-8"?>';
. Before you mess with any NFE data you have to validate, they provide XSD to do the validation.– Augusto Vasques
@Augustovasques same thing public Static Function xml2array($xmlString) ː $xmlString .= '<? xml version="1.0" encoding="utf-8"? >'; $xmlFormated = simplexml_load_string($xmlString); $json = json_encode($xmlFormated); $array = json_decode($json, TRUE); Return $array; }
– Reação Sistemas
@Augustovasques yes, the signed, transmitted and validated invoice is all XSD compliant. I just need to convert this answer into objects so that I can take internal attitudes according to the returned SOAP response. So much so that all the notes validate perfectly.
– Reação Sistemas
I found the error, your code working https://repl.it/repls/GenuineTrimOutlier. From a look at the fifth line of the XML question there is an XLM header statement out of context
– Augusto Vasques
@Augustovasques the problem is that I can’t modify to remove that line, I even noticed that it’s out of context when it should be at the beginning of XML.
– Reação Sistemas