2014-09-29 56 views
0

我在創建一個使用此WSDL的客戶端時遇到困難。 (我是新手,當談到在PHP消費web服務。)(出於保密的原因,我不得不創建一個副本XML。我希望你能理解。)使用SoapClient類的PHP SOAP調用問題

http://brique.in/wsdl.xml

我用下面的代碼打印從功能和類型:

$client = new SoapClient("http://brique.in/wsdl.xml"); 
var_dump($client->__getFunctions()); 
var_dump($client->__getTypes()); 

我想編寫PHP代碼來調用方法inwardProcess。它接受一個XML文件作爲輸入。 我試過以下內容:

$xmlInput = htmlentities('<XML><Refno>H9999999</Refno><Type>getDetails</Type><UserID>BO</UserID></XML>'); 
$result = $client->__soapCall("inwardProcess", array($xmlInput)); 

它沒有工作。看WSDL規範後,我也試過,

$xmlInput = htmlentities('<XML><Refno>H9999999</Refno><Type>getDetails</Type><UserID>BO</UserID></XML>'); 
class inwardProcess { 
    function inwardProcess($xmlInput) 
    { 
     $this->xmlInput = $xmlInput; 
    } 
} 
$inwardProcess = new inwardProcess($xmlInput); 
$webservice = new SoapClient($url, $soap_options); 
echo "Attempting Inward<br/>"; 
try { 
    var_dump($webservice->__getTypes()); 
    //I also tried passing just $inwardProcess Object in place of array($inwardProcess) 
    $result = $webservice->__soapCall("inwardProcess", array($inwardProcess)); 
    var_dump($result); 
} catch (SOAPFault $f) { 
    echo "SOAPFault".$f; 
} 

我不斷收到錯誤

Server was unable to process request. ---> Object reference not set to an instance of an object 

不知怎的,我不能夠去解決它。任何幫助將受到高度讚賞,因爲我在截止日期。

+0

您應該在$ client上調用以下函數來輸出完整的請求/響應,並查看它是否有什麼錯誤:__getLastRequest,__getLastRequestHeaders,__getLastResponse和finally __getLastResponseHeaders。 你有權訪問遠程服務器嗎?如果是這樣,你可以看看Apache的日誌文件,看看是否有任何錯誤彈出。 – NaeiKinDus 2014-09-29 13:53:20

回答

0

當作爲結構一樣複雜:

<s:element name="inwardProcess"> 
    <s:complexType> 
     <s:sequence> 
      <s:element minOccurs="0" maxOccurs="1" name="xmlInput"> 
       <s:complexType mixed="true"> 
        <s:sequence> 
         <s:any/> 
        </s:sequence> 
       </s:complexType> 
      </s:element> 
     </s:sequence> 
    </s:complexType> 
</s:element> 

和響應像

<s:element name="inwardProcessResponse"> 
    <s:complexType> 
     <s:sequence> 
      <s:element minOccurs="0" maxOccurs="1" name="inwardProcessResult"> 
       <s:complexType mixed="true"> 
        <s:sequence> 
         <s:any/> 
        </s:sequence> 
       </s:complexType> 
      </s:element> 
     </s:sequence> 
    </s:complexType> 
</s:element> 

你必須創建一個類的每個結構。在這種情況下,這將是:

class xmlInput 
{ 
    public $any = null; 
    public function __construct($any) 
    { 
     $this->any = $any; 
    } 
} 

class inwardProcess 
{ 
    public $xmlInput = null; 
    public function __construct($xmlInput) 
    { 
     $this->xmlInput = $xmlInput; 
    } 
} 
class inwardProcessResponse 
{ 
    public $inwardProcessResult = null; 
    public function __construct($inwardProcessResult) 
    { 
     $this->inwardProcessResult = $inwardProcessResult; 
    } 
} 

最後調用...

$xmlInput = new xmlInput('<XML><Refno>H9999999</Refno><Type>getDetails</Type><UserID>BO</UserID></XML>'); 
$inwardProcess = new inwardProcess($xmlInput); 
$soap_options = array(
    'trace'  => 1,  // traces let us look at the actual SOAP messages later 
    'exceptions' => 1); 
$url = "<WSDL URL>"; 
$client = new SoapClient($url, $soap_options); 
try { 
    $result = $client->__soapCall("inwardProcess", array($inwardProcess)); 
    echo htmlentities($result->inwardProcessResult->any); 
} catch (SOAPFault $f) { 
    echo "-1"; 
} 

工作!