Home > Mobile >  how to properly get data from an XML SOAP response in PHP?
how to properly get data from an XML SOAP response in PHP?

Time:12-01

I'm able to access part of the XML record but the problem is the returned value is presented in this format below:

SimpleXMLElement Object
(
    [0] => 
1234567
)

And if I use inspect element this is what it shows:

<xx>
   <yy>
      <a>1</a>
      <b>2</b>
      <c>3</c>
      <d>4</d>
   </yy>
   <zz Type="type">
      <e>5</e>
      <f>6</f>
      <g>
         <h>7</h>
      </g>
   </zz>
</xx>

SOAP XML Response: (The actual SOAP response have < and > converted to &lt; and &gt; I just changed it here to make it more readable)

<s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:body>
<processrequestresponse xmlns="http://zzz.org/">
<processrequestresult>
<xx>
   <yy>
      <a>1</a>
      <b>2</b>
      <c>3</c>
      <d>4</d>
   </yy>
   <zz Type="type">
      <e>5</e>
      <f>6</f>
      <g>
         <h>7</h>
      </g>
   </zz>
</xx>
</processrequestresult>
</processrequestresponse>
</s:body>
</s:envelope>

Code:

$xml = simplexml_load_string($response);
$data    = $xml->xpath("//s:Body/*")[0];
        $details = $data->children("http://zzz.org/");
        $test    = $details->ProcessRequestResult[0];
        echo '<pre>';
        print_r($test);

So my question is how do I access the values for a,b,c,d,e,f, and g individually?

CodePudding user response:

Try it this way:

$data = $xml->xpath('//*//text()');

foreach ($data as $datum) {
    if (strlen(trim($datum))>0)
    echo trim($datum)."\n\r";
}

Output should be 1 through 7.

  • Related