php NuSOAP:如何更改请求的内容类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/945922/
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-25 00:24:25  来源:igfitidea点击:

NuSOAP: how to change content-type of request?

phpweb-servicescontent-typenusoap

提问by jao

When consuming a .NET WCF webservice I get the following response (error):

使用 .NET WCF webservice 时,我收到以下响应(错误):

Unsupported HTTP response status 415 Cannot process the message because the content type 'text/xml; charset=UTF-8' was not the expected type 'application/soap+xml; charset=utf-8'.

不支持的 HTTP 响应状态 415 无法处理消息,因为内容类型为 'text/xml; charset=UTF-8' 不是预期的类型 'application/soap+xml; 字符集=utf-8'。

How do I change the content type? I can't find it in the NuSOAP forums/docs, or I might be overlooking something....

如何更改内容类型?我在 NuSOAP 论坛/文档中找不到它,或者我可能忽略了一些东西......

回答by mark moore

i know this is an old post, but i ran in to this page looking for an answer.

我知道这是一个旧帖子,但我跑到这个页面寻找答案。

application/soap+xmlis the content-type passed when using SOAP 1.2, text/xmlis used with SOAP 1.1,

application/soap+xml是使用 SOAP 1.2 时传递的内容类型, text/xml与 SOAP 1.1 一起使用,

something like this should do the trick,

这样的事情应该可以解决问题,

$client = new SoapClient("some.wsdl", array('soap_version' => SOAP_1_1));

回答by Franck

You can specify the encoding of NuSOAP streams with the webservices like that :

您可以使用 webservices 指定 NuSOAP 流的编码,如下所示:

$client = new nusoap_client($params);
$client->soap_defencoding = 'UTF-8';

回答by Everett

It looks like there's a slight omission in the NuSOAP library... it assumes that the content headers MUST be "text/xml", so if your client is attempting to connect to a service that outputs application/soap+xml headers, you'll end up with errors like:

看起来 NuSOAP 库中有一个轻微的遗漏......它假设内容头必须是“text/xml”,所以如果你的客户端试图连接到一个输出 application/soap+xml 头的服务,你最终会出现如下错误:

Response not of type text/xml: application/soap+xml; charset=utf-8

响应不是 text/xml 类型:application/soap+xml;字符集=utf-8

To test this, you may benefit from the following little function pattern, which I used to login to a SOAP service. Remember, print out the client object! You may not actually get a result to look at!

为了对此进行测试,您可能会从以下我用来登录 SOAP 服务的小函数模式中受益。记住,打印出客户端对象!您实际上可能不会看到结果!

require_once('path/to/downloaded/libraries/nusoap.php');    
var $endpoint = 'https://somedomain.com/path/to/soap/server/Login';
var $client; // the soapclient object

function SOAP_Login()
{
    $this->client = new soapclient($this->endpoint);

    $err = $this->client->getError();
    if ($err) 
    {
        // Display the error
        echo '<p><b>SOAP Constructor error: ' . $err . '</b></p>';
        exit;
        // At this point, you know the call that follows will fail
    }

    $params = array( 
        'some' => 'thing.. depends on what the WSDL expects'
    );

    $result = $this->client->call('someFunction', $params);     

    print_r($result);  // Without the fix, this prints nothing (i.e. false) !!!

    print_r($this->client); // Instead, look at the state of the client object, specifically error_str and debug_str
}

When I printed my $result, I got nothing, but when I printed out the $client object, I could see that there were errors.

当我打印 $result 时,我什么也没得到,但是当我打印 $client 对象时,我可以看到有错误。

The little hack I implemented was in the nusoap.php file, around line 7500. Look for this if-statement:

我实现的小技巧是在 nusoap.php 文件中,大约在 7500 行。寻找这个 if 语句:

if (!strstr($headers['content-type'], 'text/xml')) {
    $this->setError('Response not of type text/xml: ' . $headers['content-type']);
    return false;
}

And change it to this:

并将其更改为:

if (!strstr($headers['content-type'], 'text/xml') && !strstr($headers['content-type'], 'application/soap+xml') ) {
    $this->setError('Response not of type text/xml: ' . $headers['content-type']);
    return false;
}

All this does is it lets NuSOAP handle responses that issue an "application/soap+xml" header (which is a valid xml header).

所有这一切都是让 NuSOAP 处理发出“application/soap+xml”标头(这是一个有效的 xml 标头)的响应。

回答by Everett

I was stuck on this as well.

我也被困在这个问题上。

The secret is in the web.config Change wsHttpBinding to basicHttpBinding

秘诀在 web.config 中,将 wsHttpBinding 更改为 basicHttpBinding

Like so:

像这样:

<endpoint address="" binding="basicHttpBinding" contract="YourProject.View.Whatever.IYourService">

Hope that helps! /Erik

希望有帮助!/埃里克

回答by Lau_gu

This worked for me:

这对我有用:

$client = new nusoap_client($params);

$client = new nusoap_client($params);

$client->soap_defencoding = 'UTF-8';

$client->soap_defencoding = 'UTF-8';

The answer that is ticked as correct is not for NUSOAP therefore not the appropriate answer.

勾选为正确的答案不适用于 NUSOAP,因此不是适当的答案。