2017-09-21 219 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版本,完全匹配你使用的那个。 –