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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1617857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:53:11  来源:igfitidea点击:

SoapFault exception: [HTTP] Unsupported Media Type when accessing Java web-service from PHP

javaphpzend-frameworksoapweb-services

提问by Mads Mob?k

I'm trying to connect to a Java web-service using the Zend_Soap_Clientfrom the Zend Framework v1.9.0:

我正在尝试使用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>'; 
?>

Outputs:

输出:

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

Any idea what could be wrong? The url is correct, since I get the availible functions when calling

知道有什么问题吗?网址是正确的,因为我在调用时获得了可用的功能

$client->getFunctions()

采纳答案by Henrik Opel

According to this listing, the exception indicates that the server hosting the web-service is not happy with your requests encoding:

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

Indicates that the peer HTTP server does not support the Content-type used to encode the request message. The message exchange is regarded as having completed unsuccessfully.

表示对端 HTTP 服务器不支持用于对请求消息进行编码的 Content-type。消息交换被视为未成功完成。

So you should check with the web-service provider concerning the content-type/encoding they expect.

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

A possible solution if you are using SOAP_1_2is to change to SOAP_1_1since that will alter the requests made.

如果您正在使用,一个可能的解决方案SOAP_1_2是更改为,SOAP_1_1因为这会改变发出的请求。

回答by physicalattraction

I am not using Zend framework, but had a similar problem with XMLHttpRequest in JavaScript. The solution was to specify the Content-Type in the SOAP request header.

我没有使用 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);