我沒有找到一種方法,使用數組來實現它創建SOAP請求,但我可以帶班做。代碼:
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的示例。
謝謝@Eduardo Galvin。 –
'$ response = $ client-> hola(new SoapParam($ postCodeRequest,'RequestDetails'));' 那麼,'RequestDetails'是否必要?它似乎沒有用在XML中。 –
@JulianJeyarajah它是必需的,並用於命名節點。如果您將其更改爲,例如,'rd',那麼元素名稱將是 ... ... '。 –