phpweb-servicessoap

replace <SOAP-ENC:Struct> / repeated elements in php soap


I wrote the following code:

<?php
$array = array();
$array[] = new Inventory(50,"Mr. P. Erson");
$array[] = new Inventory(51,"Mrs. F. Emale");


$client = new SoapClient('http://localhost/logistics/wsdl/logisticsService2.wsdl', array('trace' => 1));
$client->addPickticket($array);

echo $client->__getLastRequest();

class Inventory {
    public $Sku;
    public $Description;

    public function __construct($sku,$desc) {
        $this->Sku = $sku;
        $this->Description = $desc;
    }
}
?>

this produces the following soap-envelope:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://logistics.website.eu" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body><ns1:addPickTicket><PickTicket>
        <SOAP-ENC:Struct>
            <Sku>50</Sku>
            <Description>Mr. P. Erson</Description>
        </SOAP-ENC:Struct>
        <SOAP-ENC:Struct>
            <Sku>51</Sku>
            <Description>Mrs. F. Emale</Description>
        </SOAP-ENC:Struct>
    </PickTicket></ns1:addPickTicket></SOAP-ENV:Body>
</SOAP-ENV:Envelope>

How can I replace this <SOAP-ENC:Struct> by some self-chosen name? I know it's quite obvious now that it doesn't fill in any name; I don't specify this name anywhere but I can't really find out how to do this. I definitely need this because I want multiple elements with the same name, so a simple array won't work; I wouldn't be able to add multiple items with the same name.


Solution

  • Found the answer in a combination of these 2:

    resulting in the following (example)code:

    <?php
    // Note, this is a method which can be used in simple and complex responses
    
    $dogParams = new ArrayObject(); // Dog Parameters
    $dogs = new ArrayObject(); // Dog Element
    $competition = new stdClass(); // Competition Element
    $namespace="";
    
    // * Note the hard-coded 0, because this is an example
    $dogParams[0]->Gender = 'Male';
    $dogParams[0]->Breed = 'Aidi';
    
    // If you are building a complex response, you could have gone for this
    // example using a SoapVar
    $dogParams[1]->Gender = new SoapVar("Female", XSD_STRING, NULL, $namespace, 'Gender', $namespace);
    $dogParams[1]->Breed= new SoapVar("Labrador", XSD_STRING, NULL, $namespace, 'Breed', $namespace);
    
    $dog0 = new SoapVar($dogParams[0],SOAP_ENC_OBJECT,NULL,$namespace,'Dog',$namespace);
    $dog1 = new SoapVar($dogParams[1],SOAP_ENC_OBJECT,NULL,$namespace,'Dog',$namespace);
    
    // Now that we have two (2) dogs information, simply append to 'Dogs'
    
    $dogs->append($dog0);
    $dogs->append($dog1);
    
    // And now our outside structure, Competition element
    $competition->Dogs = new SoapVar($dogs, SOAP_ENC_OBJECT, NULL, $namespace, 'Dogs ', $namespace);
    
    
    $client = new SoapClient('http://localhost/logistics/wsdl/logisticsService2.wsdl', array('trace' => 1));
    $client->OutgoingNoosGoodsOrder($competition);
    echo $client->__getLastRequest();
    ?>
    

    this results in the following soap envelope:

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://logistics.website.eu">
        <SOAP-ENV:Body>
            <ns1:OutgoingNoosGoodsOrder>
                <PickTicket>
                    <Dogs>
                        <Dog>
                            <Gender>Male</Gender>
                            <Breed>Aidi</Breed>
                        </Dog>
                        <Dog>
                            <Gender>Female</Gender>
                            <Breed>Labrador</Breed>
                        </Dog>
                    </Dogs>
                </PickTicket>
            </ns1:OutgoingNoosGoodsOrder>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    (Please note; OutgoingNoosGoodsOrder and Pickticket are terms that come from my (temp) wsdl file.)