Convert a SOAP response to JSON

Asked

Viewed 312 times

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.

  • @Augustovasques is still the same. Error "simplexml_load_string(): Entity: line 1: parser error : XML declaration allowed only at the start of the Document"

  • Your XML is incomplete put <?xml version="1.0" encoding="utf-8"?> in the first line.

  • @Augustovasques I get it so from the server, I can’t add or remove anything.

  • $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.

  • @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; }

  • @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.

  • 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

  • @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.

Show 4 more comments

1 answer

1

Try to extract the contents of the message to convert, I believe your function is not working because I do not understand xml as valid, if you put the contents here https://codebeautify.org/xmlvalidator it accuses the error, try with the body value:

<?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>

hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.