Add repeating params to SOAP call

时间:2015-09-14 15:30:32

标签: php soap

I need to generate the following XML (there's more but I've shortened it):

<ns1:SubmitNewApp>
    <ns1:newApp>
        <ns1:Goods Type="EG0">
            <ns1:Description>Description 1</ns1:Description>
            <ns1:Quantity>1</ns1:Quantity>
        </ns1:Goods>
        <ns1:Goods Type="EG1">
            <ns1:Description>Description 2</ns1:Description>
            <ns1:Quantity>5</ns1:Quantity>
        </ns1:Goods>
        <ns1:User>
            <ns1:Title>Mr</ns1:Title>
            <ns1:Forename>John</ns1:Forename>
            <ns1:Initial/>
            <ns1:Surname>Doe</ns1:Surname>
        </ns1:User>
    </ns1:newApp>
</ns1:SubmitNewApp>

Most of which can easily be done with a nested array but the Goods item needs to be repeating with the Type attribute being different for each.

I know I can extend the SoapClient class and overwrite the __doRequest function and send over the raw XML or overwrite it but it seems a bit hacky and is best done using SoapVar/SoapParam but I can't figure out how, especially with the nested associative array.


To anyone with the same issue, I used the following __doRequest override as the implementation of SOAP isn't great in PHP.

class SoapCustomClient extends SoapClient {
  function __doRequest($request, $location, $action, $version) {
    $dom = new DomDocument('1.0', 'UTF-8');
    $dom->loadXML($request);
    $goods = $dom->getElementsByTagName('Goods');
    foreach ($goods as $i => $good) {
      $good->setAttribute('Type', 'EG'. $i);
      $i++;
    }
    $request = $dom->saveXML();
    return parent::__doRequest($request, $location, $action, $version);
  }
}

1 个答案:

答案 0 :(得分:1)

Unfortunately PHP SOAP is not that good yet. You just cannot do nested associative arrays in SoapVar. That's why overloading of __doRequest() at the moment is only way to make it happen. Just make sure you using DOM and not trying to manipulate XML as a string. Hacky? Yes, it is. But unfortunately we don't live in perfect world :(