0

My asmx WEB service return this XML

   <?xml version="1.0" encoding="ISO-8859-1"?>
   <PRODUCT>
     <DESC>Vanilla ice cream</DESC>
     <CODEERR>0</CODEERR>
   </PRODUCT>

Calling WEB service from this PHP code

  $SoapCli = new SoapClient('http://www.foo.com/MyService.asmx?WSDL');
        $params = array(
          'PARAM1' => 'some_param_1',
          'PARAM2' => 'some_param_2',
        );
        $resp_WS = $SoapCli->__soapCall('MyFunction', array($params));
        var_dump($resp_WS);

result is

object(stdClass)#11946 (1) {
  ["MyFunctionResult"]=&gt;
  object(stdClass)#11947 (1) {
    ["any"]=&gt;
    string(88) "<product xmlns=""><desc>Vanilla ice cream</desc><codeerr>0</codeerr></product>"
  }
}

but, after googling a lot, I don't find PHP code for retreive values โ€‹โ€‹of two fields DESC and CODER

NicoBlu
  • 19
  • 4
  • 1
    Your XML is at `$resp_WS->MyFunctionResult->any`. From there you can parse it using one of the methods in the duplicate (SimpleXML is probably the easiest). If you have problems with the code you write to parse it, please ask another question. โ€“ Nick Apr 20 '19 at 05:30

1 Answers1

0

You can use json_encode,json_decode,simplexml_load_string to parse the XML response, try the following code snippet to read the XML response

$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
         <PRODUCT>
           <DESC>Vanilla ice cream</DESC>
           <CODEERR>0</CODEERR>
         </PRODUCT>';

 $res = json_decode(json_encode((array)simplexml_load_string($xml)),true);

Now you can use $res['DESC'] and $res['CODEERR'] to retrive the values.

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
  • 1
    I can almost understand doing this for complex XML, but after something like `$data = simplexml_load_string($xml)`, you can then use `$data->desc` - not exactly complicated. โ€“ Nigel Ren Apr 20 '19 at 06:38