0

I'm querying a SOAP web-service using a WSDL file and sometimes you get a single or many elements of the same name. In the former case, PHP will turn it into a single object, in the latter it will become an array of objects.

Isn't there a sane way to always have these entries in an array? Currently, I'm checking if a property of the object is set, and if it is, I place the whole object into a new array. Then I can always just loop through them, and not have to worry about that from this point on.

I feel like I'm missing something here, since this appears to be a common problem. Do I really have to write some abstraction myself to handle this? I mean, it's in the XSD, so PHP should know that there can be more than one element of the same name and do the sane thing, right?

DanMan
  • 11,323
  • 4
  • 40
  • 61
  • I'd say this is less a problem with SOAP but of an inconsitent API mixing collections and single objects as return type. Proxying and wrapping is the key here to apply the taste you like I'd say. – hakre May 28 '14 at 06:17

1 Answers1

0

Turns out there's an option for the SoapClient class called SOAP_SINGLE_ELEMENT_ARRAYS which will turn every element into an array. Not exactly what I wanted, but probably the best i can get without doing it myself, right? Used like this:

$client = new SoapClient($wsdl,
    array('features' => SOAP_SINGLE_ELEMENT_ARRAYS)
);

P.S.: SO's on-site search stinks. I found this on Google after searching and asking here.

Community
  • 1
  • 1
DanMan
  • 11,323
  • 4
  • 40
  • 61