Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single array element becomes object with Zend_Soap_Client

I'm using Zend_Soap_Client and encountering this issue:

<parent>
    <child><name>abc</name></child>
    <child><name>def</name></child>
</parent>

If there's more than one child element then Zend return array and I can access like

$result->parent->child[0]->name

but if there's only one child node it returns object like:

$result->parent->child->name

Can you please let me know what's wrong with my approach or how can I overcome it?

My sample code:

$client = new Zend_Soap_Client('url', array('wsdl'=>'url));
$result = $client->getResult();

I'm using zend 1.9. The same issue happens with PHP's native SoapClient

Thanks!

like image 973
aryan kumar Avatar asked Jan 12 '23 06:01

aryan kumar


1 Answers

Personally I do not see the need to use Zend_Soap_Client instead of SoapClient because the Zend version does not add anything beneficial, but on the other hand the solution applies to both:

There is an options array parameter in the original SoapClient that accepts plenty of things, and especially this below (ref):

The features option is a bitmask of SOAP_SINGLE_ELEMENT_ARRAYS,...

With this option, all array structures in the soap response are not reduced to one single element if they contain only one, but left as is. You are always accessing an array then, which is easier than switching depending on the content.

Example:

$s = new SoapClient($wsdl, array('features' => SOAP_SINGLE_ELEMENT_ARRAYS));
like image 86
Sven Avatar answered Jan 28 '23 19:01

Sven