2016-04-25 41 views

回答

0

我没有找到一种方法,使用数组来实现它创建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的示例。

+0

谢谢@Eduardo Galvin。 –

+0

'$ response = $ client-> hola(new SoapParam($ postCodeRequest,'RequestDetails'));' 那么,'RequestDetails'是否必要?它似乎没有用在XML中。 –

+0

@JulianJeyarajah它是必需的,并用于命名节点。如果您将其更改为,例如,'rd',那么元素名称将是 ... ...'。 –