NuSOAP: how to change content-type of request?

42,203

Solution 1

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

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

something like this should do the trick,

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

Solution 2

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

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

Solution 3

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:

Response not of type text/xml: application/soap+xml; charset=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!

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.

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

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

Share:
42,203
jao
Author by

jao

.NET developer

Updated on October 27, 2020

Comments

  • jao
    jao over 3 years

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

    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'.

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

  • jao
    jao almost 15 years
    I can't change it to basicHttpBinding, as we need wsHttpBinding for our certificates.
  • Adam F
    Adam F about 11 years
    Wow That was easy. Thanks!