2017-09-21 217 views
1

我需要在PHP中使用Web服務https://www.example.com/example.svc?wsdl。爲此,我使用PHP SOAP客戶端。代碼段象下面這樣:PHP SOAP客戶端http頭錯誤

$client = new SoapClient("https://www.example.com/example.svc?wsdl", 
    array('soap_version' => SOAP_1_2, 
     'location'=>'https://www.example.com/example.svc', 
     'login'=> '#####', 
     'password'=> '#######', 
     'exceptions'=>true, 
     'trace'=>1, 
     'cache_wsdl'=>WSDL_CACHE_NONE, 
     'encoding'=>'UTF-8', 
     'connection_timeout' => 500, 
     'keep_alive' =>false)); 

$client->__call('getProductList',array()); 

然而,當我運行這個它通過以下錯誤:

Warning: SoapClient::__doRequest(): SSL: The operation completed successfully. in E:\location\test1.php on line 37 

Fatal error: Uncaught SoapFault exception: [HTTP] Error Fetching http headers in E:\location\test1:37 Stack trace: #0 [internal function]: SoapClient->__doRequest('<?xml version="...', 'https://travius...', 'Travius/ITraviu...', 2, 0) #1 E:\location\test1(37): SoapClient->__call('getProductList', Array) #2 {main} thrown in E:\location\test1.php on line 37 

我奮力幾天的時間來解決錯誤,但無法弄清楚。我嘗試了不同的解決方案,像堆棧溢出'keep_alive' =>false,connection_timeout但沒有任何效果。

我也嘗試使用php nusoap庫。它的請求和響應就像下面:

Error 

HTTP Error: Unsupported HTTP response status 400 Bad Request (soapclient->response has contents of the response) 

Request 

POST /#####.svc?wsdl HTTP/1.0 
Host: #####.#####.eu 
User-Agent: NuSOAP/0.9.5 (1.123) 
Content-Type: application/soap+xml;charset=UTF-8; charset=UTF-8 
SOAPAction: "" 
Authorization: Basic RGVtb2FwaTpEdXE1dmRWdA== 
Content-Length: 529 

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" 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/"><SOAP-ENV:Body><GetProduct><parameters><intProductID xsi:type="xsd:int">1266</intProductID><lang xsi:type="xsd:string">CN</lang></parameters></GetProduct></SOAP-ENV:Body></SOAP-ENV:Envelope> 

Response 

HTTP/1.1 400 Bad Request 
Server: Microsoft-IIS/7.5 
X-Powered-By: ASP.NET 
Date: Mon, 02 Oct 2017 16:03:04 GMT 
Content-Length: 0 

請注意,此Web服務在.NET應用程序工作正常。 任何幫助高度讚賞。提前致謝。

+1

請詳細說明「MySoapClient」的來源以及引發此錯誤的實際WSDL。根據錯誤,它看起來像你使用nusoap,一般來說工作正常。 – jwriteclub

+0

你應該嘗試使用本機php soapclient,看看它是否有幫助。您可以按照https://stackoverflow.com/questions/29934167/set-up-php-soap-extension-in-windows上的指南進行操作,它應該很容易測試。 – ksoni

+0

你也可以看到有些用戶發現nusoap連接到C#(.Net)的問題https://stackoverflow.com/questions/15402233/which-is-better-php-soap-or-nusoap#comment54533941_15402304 – ksoni

回答

1

從您在您的問題,請更改以下行張貼的錯誤判斷。

$client->__call('getProductList',array()); 

像這樣的東西

$client = false; 
try { 
$client = new SoapClient("https://www.example.com/example.svc?wsdl", 
    array('soap_version' => SOAP_1_2, 
     'location'=>'https://www.example.com/example.svc', 
     'login'=> '#####', 
     'password'=> '#######', 
     'exceptions'=>true, 
     'trace'=>1, 
     'cache_wsdl'=>WSDL_CACHE_NONE, 
     'encoding'=>'UTF-8', 
     'connection_timeout' => 500, 
     'keep_alive' =>false)); 
} catch (Exception $e) { 
    exit("SoapClient Error \"".$e->getMessage()."\""); 
} 
if(!empty($client)) { 
try { 
     $soapResponse = $client->__soapCall('getProductList',array()); 
     var_dump($soapResponse); 
    } catch (SoapFault $e) { 
     exit("Error using method \"getProductList\" having errorCode \"".$e->faultcode."\""); 
    } 
} 

我不知道你用的什麼版本的PHP(你還沒有發佈相關的信息),但我覺得你的情況_call已被棄用。

Calling this method directly is deprecated. Usually, SOAP functions can be called as methods of the SoapClient object; in situations where this is not possible or additional options are needed, use SoapClient::__soapCall() .

+1

我使用PHP版本5.6。我只是用__soapCall()嘗試,但結果相同 –

+0

@karim_fci同樣的結果是什麼意思?整個代碼片段被封裝在一個try-catch塊中,所以腳本正在退出,並顯示'Error using method \「getProductList \」errorsCode \「」。$ e-> faultcode。「\」'? –

+0

@karim_fci另外檢查這個帖子http://php.net/manual/en/class.soapclient.php#116724如果在這個例子中提到的PHP版本,完全匹配你使用的那個。 –