8

I am fetching some data using SoapClient. I get this resuls from one of the calls:

stdClass Object
(
    [payTransIncome] => stdClass Object
        (
            [item] => stdClass Object
                (
                    [payTransId] => 141281
                    [payTransItId] => 630260
                    [payTransBuyerId] => 1311

                )
        )
)

However the docs of this WebAPI say payTransIncome is an array. Seems to me SoapClient found a one element array and converted it to a single stdClass object. And this makes it harder to parse because sometimes I think it might actually return more then 1 element.

Sure I can put everywhere checks if (is_array()) but maybe there is a simple, more elegant way?

Tom
  • 2,962
  • 3
  • 39
  • 69

2 Answers2

11

Please try to set features to SOAP_SINGLE_ELEMENT_ARRAYS in your SoapClient options:

$client = new SoapClient("some.wsdl", ['features' => SOAP_SINGLE_ELEMENT_ARRAYS]);
Ismail RBOUH
  • 10,292
  • 2
  • 24
  • 36
1

In older versions of PHP, SoapClient may ignore the "features" option. That was my case using PHP 5.3. But you can always cast an object of stdClass to array:

$client = new SoapClient("some.wsdl");
$objResult = $client->__soapCall("someFunction");
$objArray = (array)$objResult;
DanieleV
  • 11
  • 3