0

I'm trying to do a SOAP call in PHP, it works normally, but i'm with a doubt: How do I can pass arguments to XML creating new nodes according to an array of product's quantity? See this...

That's my XML in SoapUI (with the parts that are important: ITEMSITM > TITEMSITM. The first TITEMSITM is with the fields, the others its the same thing):

   <soapenv:Header/>
   <soapenv:Body>
      <ns:MANUTENCAOSITM>
         <ns:SITM>
            <ns:CABECALHOSITM>
               ...
            </ns:CABECALHOSITM>
            <ns:ITEMSITM>
               <!--Zero or more repetitions:-->
               <ns:TITEMSITM>
                  <ns:CODIGOPRODUTO>0000265</ns:CODIGOPRODUTO>
                  <ns:DESCRICAOPRODUTO>REQ.CAT.0,410 POLI (PL10)</ns:DESCRICAOPRODUTO>
                  <ns:PERCENTUALDESCONTO>-1.03</ns:PERCENTUALDESCONTO>
                  <ns:PESOUNITARIO>0.41</ns:PESOUNITARIO>
                  <ns:PRECOBONIFICADO>10</ns:PRECOBONIFICADO>
                  <ns:PRECOTABELA>9.700</ns:PRECOTABELA>
                  <ns:PRECOUNITARIO>9.6</ns:PRECOUNITARIO>
                  <ns:QUANTIDADEBONIFICADA>20</ns:QUANTIDADEBONIFICADA>
                  <ns:QUANTIDADEVENDA>200</ns:QUANTIDADEVENDA>
                  <ns:SALDOBONIFICADO>0</ns:SALDOBONIFICADO>
                  <ns:TOTALBRUTO>1940.000</ns:TOTALBRUTO>
                  <ns:TOTALLIQUIDO>1920.000</ns:TOTALLIQUIDO>
                  <ns:TOTALPESO>82.000</ns:TOTALPESO>
                  <ns:VALORBONIFICADO>9.700</ns:VALORBONIFICADO>
                  <ns:VALORLIQUIDO>8.9550</ns:VALORLIQUIDO>
               </ns:TITEMSITM>
               <ns:TITEMSITM>
                  ...
               </ns:TITEMSITM>
               <ns:TITEMSITM>
                  ...
               </ns:TITEMSITM>
            </ns:ITEMSITM>
            <ns:RODAPESITM>
               <ns:CRESCIMENTOANTERIOR>?</ns:CRESCIMENTOANTERIOR>
               <ns:TOTALINVESTIMENTO>0.1303</ns:TOTALINVESTIMENTO>
            </ns:RODAPESITM>
         </ns:SITM>
         <ns:TIPOOPERACAO>3</ns:TIPOOPERACAO>
      </ns:MANUTENCAOSITM>
   </soapenv:Body>

I need to repeat this node (TITEMSITM) for each product in PHP, but it doesn't work, it just store the last item, like this code below that I try to do, but with no success.

$arguments = array(
    'SITM' => array(
        'CABECALHOSITM' => $pars1,
        'ITEMSITM' => array(
            'TITEMSITM' => $parsItem[0],
            'TITEMSITM' => $parsItem[1],
            'TITEMSITM' => $parsItem[2]
            // ...
        ),
        'RODAPESITM' => $pars2
    ),
    'TIPOOPERACAO' => $pars3
);

$inserirItens = $cliente->MANUTENCAOSITM($arguments);

The code above calls with no problems, but when I print_r or var_dump the $arguments, I see that the repetition of the TITEMSITM sends only one product. I think it's simple, but I'm not getting. Can someone help me, please?

References:

Community
  • 1
  • 1
brunohdev
  • 1
  • 1
  • 2
  • Yes it's simple. Within a PHP array, a key can only exist once. That's why the later assignment to the key `TITEMSITM` is overwriting a previous one (re-read http://php.net/array for just these fundamental PHP array basics - it can't hurt). So the last one wins. Remove the keys in the inner-most array and try again, that *should* work but I'm not totally sure for SOAPClient and it's array notation for parameters. – hakre Jul 21 '14 at 18:33
  • I put the keys to exemplify my doubt and the way how I would like to fix. Now, I just need to know how to do that way, no overwriting the last 'TITEMSITM', but creating news 'TITEMSITM's and adding the products informations =/ – brunohdev Jul 21 '14 at 18:56
  • Try with something along the lines like: `'ITEMSITM' => array('TITEMSITM' => array($parsItem[0], $parsItem[1], ... ) ...` so that you have the key once with with an array of these items. IIRC that should work but as written I'm not 100% sure. It's since some time I did this last. – hakre Jul 21 '14 at 19:02
  • By this way, it catches all the product's informations, all right, but don't create others 'TITEMSITM', it understands like only one item. The XML needs to separate each one by the 'TITEMSITM'. You know? Thanks for the comments.. – brunohdev Jul 21 '14 at 19:15
  • Yes I know, that was clear from the beginning. But if that doesn't work (I saw it double, you might cut the inner one), I'm out of hints. Play around with it and try and you could also search for similar Q&A. You can even link here in comments or within your question (that's good practice btw. linking and then explaining what did or didn't work for you with that, here an example: http://stackoverflow.com/q/24570784/367456) – hakre Jul 21 '14 at 19:21
  • Ok man, thanks a lot. I will continue trying to fix that, and I'll follow your tips! :) – brunohdev Jul 21 '14 at 19:29

2 Answers2

2

Here is the code style 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);
diamondsea
  • 2,980
  • 1
  • 12
  • 9
1
$ITEMSITM = new stdClass();    
foreach ($parsItem as $item) {
    $ITEMSITM->TITEMSITM[] = $item;
}

The reasons this should work is because it will closely emulate data structures of your WSDL.

Akshay Agarwal
  • 1,959
  • 1
  • 16
  • 14