PHP SOAP Client creating an array of items of the same tag.

August 1st, 2019

I really shouldn’t be writing a post about SOAP in 2019, but sadly there are still some use cases that are relevant even today.

Recently I needed to be able to submit an array of <TradeIn> tags under one <TradeInItems> tag, but none of the issues on StackExchange were proving helpful, and I didn’t want to get attacked so I couldn’t post either.

Below is the solution I ended up using in order to create a structure like this:


You can submit as many ‘s as you would need.

The code I used to generate this structure looks like this:

$all_items = new ArrayObject();

$tradeinitem = new TradeInItem(1, '13.00', 15632, 16, $quantity, 1, 3);
$tradeinitem2 = new TradeInItem(2, '15.00', 156323, 15, $quantity, 1, 3);

$item = new SoapVar($tradeinitem, SOAP_ENC_OBJECT, null, null, 'TradeInItem');
$item2 = new SoapVar($tradeinitem2, SOAP_ENC_OBJECT, null, null, 'TradeInItem');


$tradeins = new SoapVar($all_items, SOAP_ENC_OBJECT);
$tradein = new TradeIn($address, $email, $num, $ReferenceID, $tradeins);


Where TradeInItem is a class containing the structure set out in public variables.


Leave a Reply

Your email address will not be published. Required fields are marked *