简体   繁体   中英

How can I put a custom xsi:type in a SOAP request created using PHP arrays?

How can I create the following part as part of a soap request?

<RequestDetails xsi:type="PostcodeRequest">
  <Postcode>SW1A 1AA</Postcode>
</RequestDetails>

I am creating the soap request using arrays

$aPostcode = array('Postcode'=>'SW1A 1AA')
$aPostcodeRequest = array('PostcodeRequest' => $aPostcode);
$GetLineCharacteristicsRequest = array('RequestDetails' => aPostcodeRequest);

I didn't find a way to achieve it using arrays, but I could do it with classes. The code:

try {
    $options = [
        'trace'=> 1,
        'location' => 'http://localhost/pruebas/soap-server-nowsdl.php',
        'uri' => 'http://localhost/pruebas'
    ];

    class PostCodeRequest {
        function __construct($pc)
        {
            $this->Postcode = $pc;
        }
    }

    $client = new SOAPClient(null, $options);

    $pc = new PostcodeRequest('SW1A 1AA');
    $postCodeRequest = new SoapVar($pc, SOAP_ENC_OBJECT, 'PostCodeRequest', 'http://soapinterop.org/xsd');
    $response = $client->hola(new SoapParam($postCodeRequest, 'RequestDetails'));

    header('Content-type:text/xml');
    echo $client->__getLastRequest();
}
catch (SoapFault $e) {
    echo $e;
}

Will give this as request:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost/pruebas" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://soapinterop.org/xsd" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns1:hola>
            <RequestDetails xsi:type="ns2:PostCodeRequest">
                <Postcode xsi:type="xsd:string">SW1A 1AA</Postcode>
            </RequestDetails>
        </ns1:hola>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Of course, this is assuming you have a "hola" function in your SOAP server. Replace it with whatever you're calling.

This solution is based in the example of the SoapVar constructor .

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