2014-09-12 120 views
0

我在創建從PHP應用程序到.NET Web服務的連接時遇到了一些麻煩。我在很多網站上看過要做什麼,但似乎無法讓它工作。PHP SoapClient連接到NET Web服務

Web服務需要用戶名和密碼,以及所有我試圖做的是通過發送用戶的ID來獲得用戶數據管理系統的一些用戶資料數據。

這就是供應商說,SOAP請求應該是這樣的:

POST /feed30/clientdataservice.asmx HTTP/1.1 
Host: ws-ic-pilot.acme.com 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "GetUser" 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
<GetUser xmlns="urn:Acme:ClientDataService"> 
    <strUserId>string</strUserId> 
</GetUser> 
</soap:Body> 
</soap:Envelope> 

這裏是我已經根據我在互聯網上的各種網站上發現寫的代碼:

$client = new SoapClient('https://ws-ic-pilot.acme.com/feed30/clientdataservice.asmx 
WSDL',Array("trace" => 1)); 
$header = new SoapHeader(
    array(
      'UserName' => 'myusername', 
      'Password' => 'mypassword', 
      'strUserID' => '12345' 
    )); 

$client->__setSoapHeaders($header); 

$client->GetUser(); 

當我使用__getLastRequestHeaders和__getLastRequest我創建的SOAP消息如下所示:

POST /feed30/clientdataservice.asmx HTTP/1.1 
Host: ws-pilot.acme.com 
Connection: Keep-Alive 
User-Agent: PHP-SOAP/5.3.21 
Content-Type: text/xml; charset=utf-8 
SOAPAction: "GetUser" 
Content-Length: 247 

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns1="urn:Acme:ClientDataService"><SOAP-ENV:Header/> 
<SOAP-ENV:Body><ns1:GetUser/> 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

當我發佈SOAP消息到.NET服務器我得到如下回應:

<ExceptionInfo>Exception Message:  Security requirements are not satisfied because the 
security header is not present in the incoming message.;Exception Target Method: 
ValidateMessageSecurity</ExceptionInfo></detail> 
</soap:Body></soap:Envelope> 

任何人都可以提供一個建議/建議,我怎麼解決這個問題?

在此先感謝您的幫助!

回答

0

所以我找到了我的問題的答案。似乎SOAPClient不是在我的情況下做到這一點的最佳方式。相反,更好的方式是通過CURL創建,發送和接收SOAP消息。以下是我在另一個Stackoverflow頁面上找到的示例。我適應了我的情況,它效果很好!

$credentials = "username:pass"; 
$url = "https://url/folder/sample.wsdl"; 
$body = ''; /// Your SOAP XML needs to be in this variable 

$headers = array( 
'Content-Type: text/xml; charset="utf-8"', 
'Content-Length: '.strlen($body), 
'Accept: text/xml', 
'Cache-Control: no-cache', 
'Pragma: no-cache', 
'SOAPAction: "customerSearch"' 
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']); 

curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_USERPWD, $credentials); 

$data = curl_exec($ch);