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:

<TradeInItems>
    <TradeInItem>
        <Content>test</Content>
        <Property>true</Property>
    </TradeInItem>
    <TradeInItem>
        <Content>test2</Content>
        <Property>false</Property>
    </TradeInItem>
</TradeInItems>

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

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

<?php
$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');

$all_items->append($item);
$all_items->append($item2);

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

$client->userFunction($tradein);
?>

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


Comments

Comments Are Not Yet Implemented.