pogo
pogo

Reputation: 2307

Can I modify the http headers sent via a SOAP request

I'm having trouble getting SOAP compression working and after reading the documentation for the service I've realised it's because they've decided to ignore the HTTP standard and do their own thing.

Basically I need to be able to set the content-encoding header to:

Content-Encoding: accept-gzip,accept-deflate

Rather than using Accept-Encoding, they've decided to use content-encoding which is incredibly annoying and there's zero chance of them changing it. If I set the compression option in the SOAP client it sends

Content-Encoding: gzip

Which then causes the SOAP client to throw an exception saying "Unknown Content-Encoding".

So is it possible to alter the http request that gets sent using the default php SOAP client?

Upvotes: 1

Views: 3218

Answers (3)

David8701
David8701

Reputation: 21

Maye this help somebody:

$mode = array (
        'soap_version'  => 'SOAP_1_1', // use soap 1.1 client
        'keep_alive'    => true,
        'trace'         => 1,
        'encoding'      =>'UTF-8',
        'compression'   => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE,
        'exceptions'    => true,
        'cache_wsdl'    => WSDL_CACHE_NONE,
        'stream_context' => stream_context_create ( 
            array (
                'http' => array('header' => 'Content-Encoding: XXXXXXX'),
            )
        )
    );

    // initialize soap client
    $client = new LoEnvioSoapClient ( $this->wsdl, $mode );

Upvotes: 2

Matt Browne
Matt Browne

Reputation: 12429

It's possible to change the HTTP headers used but only by extending PHP's native SoapClient class.

Something like this:

class MySoapClient extends \SoapClient
    public function __doRequest($request, $location, $action, $version, $one_way = 0)
    {
        //Send the HTTP request and get the response using curl or fsockopen,
        //of course setting Content-Encoding to accept-gzip,accept-deflate.
        //Also set Accept-Encoding to deflate
        //Put the response in a variable called $response

        //Set the headers used for this request; this is how you would do it if you used curl:
        $this->__last_request_headers = curl_getinfo($curlHandle, CURLINFO_HEADER_OUT);

        $this->__last_request = $request;

        //decompress the response
        $response = gzinflate( substr($response, 10, -8) );

        return $response;
    }
}

It seems that the OP is already aware of this, but here's a tip for others who may not be aware of this: to see the SOAP request as it would be sent out by PHP's native SoapClient class, set the 'trace' option to true:

$client = new \SoapClient($wsdlPath, array('trace'=>true));

Then, after executing your SOAP request you can do this to see the headers that were used:

var_dump( $client->__getLastRequestHeaders() );

Upvotes: 0

Viper_Sb
Viper_Sb

Reputation: 1817

Why not set your own soap headers? If needed, extend the default class and implemented your own version of it.

Upvotes: 0

Related Questions