0

I'm using PHP SOAPSERVER class.

As a response I'm sending associative php array:

function getItems()    
{    
   ...
   $items[] = Array("itemID" =>$itemID,"itemName"=>$itemName);
   return $items;
 }

SOAP return is like this:

...
<Items>
<item type="Map">
    <item>
        <key type="string">
            itemID
        </key>
        <value type="string">
            17558
        </value>
    </item>
    <item>
        <key type="string">
            itemName
        </key>
        <value type="string">
            I-17558
        </value>
    </item>
</item>
</Items>
...

Such return is pretty hard to analyze for human (given bigger array). The preferred form would be like this:

    ...
<Items>
    <item>
        <itemID>17558</itemID>
        <itemName>I-17558</itemName>
    </item>
    <item>
        <itemID>17559</itemID>
        <itemName>I-17559</itemName>
    </item>
</Items>
    ...

Is such SOAP return possible (not changing the return type - array)? How?

I have just started with SOAP and most tutorials show how to return simple types like string.

user1463822
  • 837
  • 9
  • 24
  • Why do you want to read the SOAP messages at all? Usually the client converts it into whatever native structures/types fit the response's data. – ThiefMaster Jun 25 '12 at 12:18
  • @ThiefMaster, I want to parse SOAP message as a XML using DOM methods. It looks quite messy with the current response message structure. – user1463822 Jun 25 '12 at 13:06
  • Ugh, use an existing SOAP client... anyway, that's how SOAP looks like. You cannot simply use a different format. It's standardized. – ThiefMaster Jun 25 '12 at 13:07
  • @ThiefMaster, thanks for the info. Thoughts of being able to change that structure was killing me. – user1463822 Jun 25 '12 at 14:09

1 Answers1

5

Instead of sending an associative array, send a stdclass object.

Example :

$return = new stdclass;
$return->ItemID = 1;
$return->ItemName = 'foo';

return $return;

Then the SOAP results will be just the way you want it!

SphynXz
  • 64
  • 1
  • 2
  • The SOAP Client uses object notation when sending objects, so this should answer the OP's question. You can also instantiate the predefined class by using typical object instantiation syntax, ie "new stdClass()". – Leo Bedrosian Aug 14 '14 at 20:54