SoapFault异常:[HTTP]从PHP访问Java Web服务时不支持的媒体类型

Mad*_*bæk 7 php java soap web-services zend-framework

我正在尝试使用Zend_Soap_ClientZend Framework v1.9.0中的Java Web服务连接:

<?php
include( 'Zend/Loader/Autoloader.php');
$autoloader = Zend_Loader_Autoloader::getInstance();
$client = new Zend_Soap_Client('https://webservice.com/webservice-war/webservice?wsdl'
    , array('encoding'=> 'UTF-8'));

try{
    $result = $client->find_customer(array('username' => 'user', 
                         'password' => '123'), array('city' => 'some city'));
} catch(Exception $e){
    echo $e;
}

echo '<pre>' . $client->getLastRequestHeaders() . '</pre>'; 
?>
Run Code Online (Sandbox Code Playgroud)

输出:

SoapFault exception: [HTTP] Unsupported Media Type in 
/Library/ZendFramework-1.9.0/library/Zend/Soap/Client.php:937 
Stack trace: 
 #0 [internal function]:
SoapClient->__doRequest('_doRequest(Object(Zend_Soap_Client_Common),
    '__doRequest('__soapCall('find_customer', Array, NULL, NULL, Array) 
 #6 [internal function]:  
 Zend_Soap_Client->__call('find_customer', Array) 
 #7 /Users/webservicetest/index.php(8): 
 Zend_Soap_Client->find_customer(Array, Array) 
 #8 {main}

POST /webservice-war/webservice HTTP/1.1
Host: webservice.com
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.2.6
Content-Type: application/soap+xml; charset=utf-8; action=""
Content-Length: 315
Run Code Online (Sandbox Code Playgroud)

知道什么可能是错的吗?网址是正确的,因为我在通话时获得了可用的功能

$client->getFunctions()
Run Code Online (Sandbox Code Playgroud)

Hen*_*pel 37

根据此列表,该异常表示托管Web服务的服务器对您的请求编码不满意:

表示对等HTTP服务器不支持用于对请求消息进行编码的Content-type.消息交换被视为已完成失败.

因此,您应该向Web服务提供商咨询他们期望的内容类型/编码.

如果您使用的可能解决方案SOAP_1_2是更改为,SOAP_1_1因为这将改变所做的请求.

  • 更具体地说:http://framework.zend.com/issues/browse/ZF-5286.通过在选项中设置'soap_version'=> SOAP_1_1解决问题,使请求通过text/xml而不是application/soap + xml传递 (9认同)
  • 尝试添加标题Content-Type:text/xml; charset = utf-8 for soap 1.1 and Content-Type:application/soap + xml; charset = utf-8 for soap 1.2 (3认同)

phy*_*ion 6

我没有使用Zend框架,但在JavaScript中与XMLHttpRequest有类似的问题.解决方案是在SOAP请求标头中指定Content-Type.

var sr = '<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.w3schools.com/webservices/">  <SOAP-ENV:Body><ns1:CelsiusToFahrenheit><ns1:Celsius>32</ns1:Celsius></ns1:CelsiusToFahrenheit></SOAP-ENV:Body></SOAP-ENV:Envelope>';
http_request = new XMLHttpRequest();
http_request.open('POST', 'http://www.w3schools.com/webservices/tempconvert.asmx', true);
http_request.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
http_request.send(sr);
Run Code Online (Sandbox Code Playgroud)