代码之家  ›  专栏  ›  技术社区  ›  Mads Mobæk

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

  •  7
  • Mads Mobæk  · 技术社区  · 15 年前

    我正在尝试使用 Zend_Soap_Client 从Zend框架v1.9.0:

    <?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
    

    知道会出什么问题吗?URL是正确的,因为我在调用时得到了可用的函数

    $client->getFunctions()
    
    2 回复  |  直到 9 年前
        1
  •  37
  •   googletorp    13 年前

    根据 this listing ,异常表明承载Web服务的服务器不满意您的请求编码:

    指示对等HTTP服务器 不支持使用的内容类型 对请求消息进行编码。这个 消息交换被视为 完成失败。

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

    如果您正在使用 SOAP_1_2 是要改变 SOAP_1_1 因为这将改变所提出的请求。

        2
  •  6
  •   physicalattraction    9 年前

    我没有使用Zend框架,但在JavaScript中的XMLHttpRequest也有类似的问题。解决方案是在SOAP请求头中指定内容类型。

    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);