Multiple elements of same name in PHP SOAP Call

不问归期 提交于 2019-12-10 18:17:25

问题


I know this type of question has been asked a number of times. I have spent several hours reading and trying the offered solutions - but none appear to work for my situation.

I need to send a SOAP request to an API that can contain an element that repeats like so:

<operationNumbers>
    <operationNumber>1234</operationNumber>
    <operationNumber>1235</operationNumber>
    <operationNumber>1236</operationNumber>
    <operationNumber>1237</operationNumber>
</operationNumbers>

I did read that perhaps I could do this:

  $buildRequest = Array(
      'myheader' => Array(
      'date' => MY_DATE,
      'id' => Array(
          'client' => CLIENT,
          'clientRef' => MYREF
          )
      ),
      'operationNumbers' => Array (
          Array('operationNumber' => '1234'),
          Array('operationNumber' => '1235')
      )
   ); 

   $request = $client->__soapCall( 'getMultiOpDets', array($buildRequest) );

Sadly this does not work and results in 'invalid request', if I send in a single operation number eg:

 ...
  'operationNumbers' => Array (
      'operationNumber' => '1234'
   )
 ...

The request is successful. I've tried soapVars/soapParams but cannot get it working using this approach. Any hints/tips/help appreciated.


回答1:


So, I solved it.

 $operationNumbersArray = array('1234','1235');

 ...

       'operationNumbers' => array(
           'operationNumber' => $operationNumbersArray
        )

During my testing and fiddling about, I had inadvertently removed another value that was mandatory. The API did not give warning of it's omission (sadly).




回答2:


Here is the code I use:

$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);


来源:https://stackoverflow.com/questions/27546087/multiple-elements-of-same-name-in-php-soap-call

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