Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SoapClient: how to pass multiple elements with same name?

I have following code:

$telnums = array(10, 20, 30);
$obj = new StdClass();
$obj->telnums = new StdClass();
foreach ($telnums as $telnum) {
    $obj->telnums = $telnum;
}

call_user_func(array($this->client, 'createDomain'), new SoapVar($obj, SOAP_ENC_OBJECT));

There $this->client is an instance of SoapClient class.

And it generates following request:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="...">
    <SOAP-ENV:Body>
        <ns1:createDomain>
            <createDomainRequest>
                <telnums>30</telnums>
            </createDomainRequest>
        </ns1:createDomain>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

But I need

            <createDomainRequest>
                <telnums>10</telnums>
                <telnums>20</telnums>
                <telnums>30</telnums>
            </createDomainRequest>

How I can achieve this?

P.S.: PHP 5.2.6-3ubuntu4.5 with Suhosin-Patch 0.9.6.2 (cli) (built: Jan 6 2010 22:25:33)

Thanks in advance!

like image 941
Slava Semushin Avatar asked Sep 01 '10 11:09

Slava Semushin


2 Answers

I had landed into a similar scenario recently and I found this pattern usually does the trick.

$obj = new StdClass();
foreach ($telnums as $telnum) {
    $obj->telnums[] = $telnum;
}

The reason this works is because it closely emulates the same data structure as prescribed by your WSDL

like image 192
Akshay Agarwal Avatar answered Nov 08 '22 02:11

Akshay Agarwal


The correct answer should have been:

$options = array(
  'createDomainRequest' => array(
    'telnums' => array(
      '10',
      '20',
      '30'
    )
  )
);

:)

like image 8
denormalizer Avatar answered Nov 08 '22 04:11

denormalizer