如何在使用PHP数组创建的SOAP请求中放置自定义xsi:type?

时间:2016-04-25 15:49:57

标签: php soap xsitype

如何在soap请求中创建以下部分?

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

我正在使用数组创建soap请求

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

1 个答案:

答案 0 :(得分:0)

我没有找到使用数组实现它的方法,但我可以用类来完成它。代码:

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;
}

请将此作为请求:

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

当然,假设您的SOAP服务器中有“hola”功能。用你正在呼唤的任何东西替换它。

此解决方案基于SoapVar constructor

的示例