简体   繁体   中英

Multiple elements of same name in PHP SOAP Call

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.

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM