5
<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.

Chinmay235
  • 3,236
  • 8
  • 62
  • 93
  • can i see data in `$xml->xpath('//returnvalues')` ? – Supun Praneeth Jun 29 '18 at 19:18
  • 2
    *btw* Soap is an XML format, but here are special libraries like [ext/soap](http://php.net/manual/de/book.soap.php) to handle it. – ThW Jun 29 '18 at 20:37
  • 2
    Cannot reproduce the problem. It prints `{"name":"status","value":"TEHNICAL_ERROR"}` on my machine. – Marco Jun 30 '18 at 08:13
  • @SupunPraneeth I have posted my SOAP response data. You could check there my `returnvalues` data – Chinmay235 Jun 30 '18 at 13:42
  • 1
    Why not use standard SoapClient instead of CURL? That way you can convert the answer directly to PHP objects (given you provide mappings for the types). Having an address of SOAP web service you can get all the necessary work done for you by libraries like https://github.com/wsdl2phpgenerator/wsdl2phpgenerator – Daniel Protopopov Jul 02 '18 at 09:12
  • thr is no error in code able to see result: https://3v4l.org/322K6 – Chetan Ameta Jul 05 '18 at 11:18
  • Oh!! Really sorry for that. It is my mistake. Above code is working fine. But in my SOAP response it's coming `ReturnValues` When I copied from `inspect element` its looking small letter. :( – Chinmay235 Jul 07 '18 at 13:23

2 Answers2

2

Your question edit is a big clue. You changed the XML from having lowercase to CamelCase elements. Change:

foreach ($xml->xpath('//returnvalues') as $item) {

to:

foreach ($xml->xpath('//ReturnValues') as $item) {

and it will work. Xpath queries are case sensitive.

jstur
  • 659
  • 6
  • 12
  • Fairly curious -- why the down vote? If the value of `$response` was what the OP expected, his code would generate a different result than it is. – jstur Jul 03 '18 at 22:42
  • Thanks for you answer. My code was right. I have edited my question. Could you please check and edit your answer – Chinmay235 Jul 07 '18 at 13:29
  • Sure thing. Edit complete. – jstur Jul 07 '18 at 16:16
0

Hope this helps

$response = '<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>';
$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);

$xml = new SimpleXMLElement($response);
$body = $xml->xpath('//returnvalues');
$json = json_encode((array)$body); 
print_r($json);
Vamsi
  • 423
  • 1
  • 5
  • 19