Search code examples
phpsoapsoap-clienthhvm

How to set and disable headers in php's SoapClient?


The SoapClient sends the HTTP header:

POST / HTTP/1.1
Accept: */*
Accept-Encoding: deflate, gzip
SOAPAction: ""
Content-Type: text/xml; charset=utf-8
Content-Length: 2085
Expect: 100-continue

HTTP/1.1 100 Continue

I want to disable the Expect: 100-continue. How can I do that?

I found out that I should be able to set a custom header via:

class SoapClient extends \SoapClient implements HttpClientInterface

    public function __construct($pathToWSDL, array $options, LoggerInterface $logger){
        ...
        $headers = [
            'stream_context' => stream_context_create(
                [
                    'http' => [
                        'header' => 'SomeCustomHeader: value',
                    ],
                ]
            ),
        ];
        parent::__construct($pathToWSDL, array_merge($options, $headers));
 }

yet this doesn't work.

How can I set a custom header and disable certain ones?

I am also not using native php, but HipHop VM 3.12.1 (rel).


Solution

  • The HTTP context options indicate that the header option can be a string or an array of strings, and will override other given options. If you want to use a single string with multiple options, you would separate with a carriage return and newline (\r\n) as illustrated in the first stream_context_create example,.

    So the construction would be:

    'stream_context' => stream_context_create(
        [
            'http' => [
                'header' => "SomeCustomHeader: value\r\n".
                            "Expect: \r\n",
            ],
        ]
    ),
    

    Or:

    'stream_context' => stream_context_create(
        [
            'http' => [
                'header' => [
                    'SomeCustomHeader: value',
                    'Expect: ',
                ],
            ],
        ]
    ),
    

    In your case though, most likely the cause is the version of HHVM you're using - this was a bug in HHVM which doesn't appears to have been fixed in 3.15.0 so you might want to try upgrading your HHVM and trying again.