Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 982
Bernie Davies Avatar asked Feb 11 '23 08:02

Bernie Davies


2 Answers

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

like image 147
Bernie Davies Avatar answered Feb 13 '23 22:02

Bernie Davies


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);
like image 31
diamondsea Avatar answered Feb 13 '23 22:02

diamondsea