Search code examples
phparrayssoapsoap-client

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


Hi Im 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 figure out a way to pass this in the $params list before the call to the service.

Im building the params like so:- (excert)

'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 cant be null or empty.

Thanks For any help.


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, if you compare my array structure to that in the original you see what changes I had to make.

    Paul