Chris Rogers
Chris Rogers

Reputation: 1863

PHP SoapClient is not interpreting XML response into array

I am struggling to get PHP's inbuilt SoapClient to interpret the response coming back from the web service I'm trying to call.

SoapUI is able to interrogate this soap method and return good results. I am also able to get nusoap_client to return correct results (but am not able to use nusoap for other reasons and think I'm stuck with SoapClient).

Using SoapClient, I can see that seemingly good data is being returned, but instead of the results being parsed and broken into easily consumed arrays of values, the XML response string is being stuffed into a single field in an object (labelled 'any').

My code and results are shown below :

$client = new SoapClient($url);
$results = $client->GetPropertiesByProjectAndContractStatus($params);
var_dump($results);

The output from the above code is below :

object(stdClass)[3]
  public 'GetListingsByGUIDResult' => 
    object(stdClass)[4]
      public 'any' => string '<xs:schema xmlns="" ........ (long xml here) ....

Now, perhaps it's possible that the service I am using is returning some xml which has something wrong with it (although it seems fine to my eye). nusoap and SoapUI both have no problems using it either.

So I'm wondering what is it with SoapClient that is different.

Upvotes: 4

Views: 5325

Answers (3)

Snor
Snor

Reputation: 1133

This happens when the data returned is not specified in the WSDL that you are using. Anything not in the WSDL will get lumped into this "any" element at the end of parsing the XML.

If this is happening then you should ensure that your script is using the correct WSDL for the SOAP service you're using.

For example, if you're using an old WSDL and new elements are now in use in the service, they will end up inside this "any" element!

Upvotes: 1

Neil
Neil

Reputation: 11

I have a function that grabs that result and turns it into a dom object so you can use the dom functions to extract data.

protected function getElementsFromResult($elementName, $simpleresult) {
  $dom = new DOMDocument ();
  $dom->preserveWhiteSpace = FALSE;
  if ($simpleresult == null) {
     echo 'null';
     return null;
  } else {
    $dom->loadXML ( $simpleresult->any );
    return $dom->getElementsByTagName ( $elementName );
  }

$elementName is the name of the elements you want from the result and $simpleresult is the object containing the 'any' string.

Upvotes: 1

Peter
Peter

Reputation: 31751

Did you try using the SOAP_SINGLE_ELEMENT_ARRAYS feature?

<?php
$client = new SoapClient($url, array('features' => SOAP_SINGLE_ELEMENT_ARRAYS));

Upvotes: 0

Related Questions