phparrayssoapsoap-client

How Do I Add an Attribute to an Array for a SoapClient Request In PHP


Hi I'm quite new to using SOAP, but all is going quite well, I have written a script using PHP SOAP-client, and all is well apart from one tag which has an attribute, and i can not figure out a way to pass this in the $params list before the call to the service.

I'm building the params like so: (excerpt)

'Goods' => array(
      'Description' => '$Description','Quantity' => '$qty');

however I need the XML to be:

<Goods Type="EGoods">              
           <Description>Test</Description>
           <Quantity>1</Quantity>
</Goods>

Note the type="EGoods" on the goods tag, how can i modify my array in PHP to account for this attribute?

When I submit the SOAP request as is, I get the error saying goods can't be null or empty.


Solution

  • After some digging around in the manual I managed to find a solution which works:

    'Goods' => array('_' => '', 'Type'=>'EGoods',
            'Description' => $Description, 'Quantity' => $qty);
    

    Produces

    <Goods Type="EGoods">              
      <Description>Test</Description>
      <Quantity>1</Quantity>
    </Goods>
    

    Solution Found Here PHP: SoapParam::SoapParam - Manual Comment 114146

    I had to slightly modify it to work for my attribute name, if you compare my array structure to that in the original you see what changes I had to make.