SoapClient: how to pass multiple elements with same name?

妖精的绣舞 提交于 2019-11-28 09:21:58

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

The correct answer should have been:

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

:)

It is a pain in the buttock to find a working solution, but eventually it's not that hard. Even surprisingly easy and neat by using SoapParam's:

$soapClient = new SoapClient($wsdl);
$soapClient->__call('createDomain', array(
    new SoapParam('10', 'telnums'),
    new SoapParam('20', 'telnums'),
    new SoapParam('30', 'telnums'),
));

Here is the code format that I used:

$wsdl = 'https://your.api/path?wsdl';
$client = new SoapClient($wsdl);
$multipleSearchValues = [1, 2, 3, 4];
$queryData = ['yourFieldName' => $multipleSearchValues];
$results = $client->YourApiMethod($queryData);
print_r($results);

Fixed it by extends SoapClient and overrides __doRequest() method where I modify request as descibed here: http://www.php.net/manual/en/soapclient.dorequest.php#57995

Looks terrible for me, but it works "right here, right now".

Jesy
$telnums=array(10, 20, 30);
$createDomainRequest=array('createDomainRequest' => array(
  'telnums' => $telnums)
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!