Friday, 29 June 2018

How to convert SOAP response to XML or JSON format?

<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:body>
        <ns1:mbillcommandresponse xmlns:ns1="http://www.mysoap.com/SoapService/">
            <returnvalues>
                <name>status</name>
                <value>TEHNICAL_ERROR</value>
            </returnvalues>
            <returnvalues>
                <name>description</name>
                <value>Please contact your administrator</value>
            </returnvalues>
        </ns1:mbillcommandresponse>
    </soapenv:body>
</soapenv:envelope>

Above I got response in my CURL response. Here is my PHP code:

    $response = curl_exec($ch);
    $xml = simplexml_load_string($response);
    //$result = $xml->xpath('//name'); //echo "<pre>"; print_r($result); exit;
    $xml->registerXPathNamespace('ns1', 'http://www.mysoap.com/SoapService/');
    foreach ($xml->xpath('//returnvalues') as $item) {
        $json = json_encode($item);
        $convrt_arr = json_decode($json, true);
        break;
    }
    print_r($json); exit;

On my above code I got empty json. Could you please help me.



from How to convert SOAP response to XML or JSON format?

No comments:

Post a Comment