2014-03-05 65 views
1

下面是我想達到的XML結構:PHP SoapClient的 - 做多根標籤的來電請求

<?xml version="1.0" encoding="ISO-8859-1"?> 
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <SOAP-ENV:Body> 
    <ns1:method> 
     <auth> 
     ... 
     </auth> 
     <data> 
     ... 
     </data> 
    </ns1:method> 
    </SOAP-ENV:Body> 
    </SOAP-ENV:Envelope> 
</XML> 

我的代碼:

<?php 
$client = new SoapClient(null, array('location' => "http://www.test.com/soap.php", 
            'uri'  => "http://tempuri.org/", 
'trace' => true,'exceptions' => true,'cache_wsdl' => WSDL_CACHE_NONE)); 

$request = new stdClass(); 
$request->auth = new stdClass(); 
$request->data = new stdClass(); 

$client->__soapCall("method", array($request)); 

請注意:SoapClient的不允許我在第二個參數中插入除了數組以外的任何東西到__soapCall方法。

所以這裏就是我得到:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <SOAP-ENV:Body> 
    <ns1:method> 
     <param0 xsi:type="SOAP-ENC:Struct"> 
     <auth xsi:type="SOAP-ENC:Struct"/> 
     <data xsi:type="SOAP-ENC:Struct"/> 
     </param0> 
    </ns1:method> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

正如你所看到的,PHP增加了一個根標籤爲我的請求 - 參數0。有沒有辦法逃避這個?我需要在該請求參數中有2個「根」元素。我怎樣才能做到這一點?

回答

0

謝謝你所有的回答。我在SoapParam的幫助下解決了這個問題:

$client->__soapCall("method", array(new SoapParam($request->auth, "auth"), new SoapParam($request->data, "data"))); 
$client->__soapCall("method", array(new SoapParam($request->auth, "auth"), new SoapParam($request->data, "data"))); 
0

您是否試過這種方式:$client->__soapCall("method", $request);

+0

如果我想在第二個參數中傳遞數組以外的任何其他類型的提示,則會引發異常。 –

+0

然後你嘗試一個WSDL到PHP生成器,這將緩解你的方式來發送參數?嘗試使用WsdlToPhp或https://www.wsdltophp.com –

+0

感謝@MikaëlDELSOL,但這是非WSDL模式。 –