PHP SOAP客户端http标头错误

时间:2017-09-21 07:31:39

标签: php web-services wcf soap

我需要在PHP中使用Web服务https://www.example.com/example.svc?wsdl。为此,我使用PHP SOAP客户端。代码片段如下:

$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' =>falseconnection_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

请注意,此网络服务在.Net应用程序中运行良好。  任何帮助高度赞赏。提前谢谢。

2 个答案:

答案 0 :(得分: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已被弃用。

  

不推荐直接调用此方法。通常,SOAP函数   可以作为SoapClient对象的方法调用;在哪些情况下   这是不可能的,或者需要使用其他选项   SoapClient::__soapCall()

答案 1 :(得分:0)

您好您是否尝试将肥皂标头写为wssecurity you can see samples

或此in stackoverflow