Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I force SoapClient to return arrays as arrays?

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?

like image 990
Tom Avatar asked Jul 30 '16 12:07

Tom


2 Answers

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

$client = new SoapClient("some.wsdl", ['features' => SOAP_SINGLE_ELEMENT_ARRAYS]);
like image 87
Ismail RBOUH Avatar answered Nov 19 '22 10:11

Ismail RBOUH


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;
like image 38
DanieleV Avatar answered Nov 19 '22 10:11

DanieleV