簡體   English   中英

SoapFault異常:從PHP訪問Java Web服務時[HTTP]不支持的媒體類型

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

我正在嘗試使用Zend Framework v1.9.0中的Zend_Soap_Client連接到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>'; 
?>

輸出:

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

知道有什么問題嗎? 網址是正確的,因為調用時我得到了可用的功能

$client->getFunctions()

根據此清單 ,該異常表明托管Web服務的服務器對您的請求編碼不滿意:

指示對等HTTP服務器不支持用於對請求消息進行編碼的Content-type。 消息交換被視為未成功完成。

因此,您應該向Web服務提供商咨詢他們期望的內容類型/編碼。

如果您使用的是SOAP_1_2則可能的解決方案是更改為SOAP_1_1因為這會更改發出的請求。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM