如何在 php 的 SoapClient 中设置和禁用 headers?
How to set and disable headers in php's SoapClient?
SoapClient 发送 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
我想禁用 Expect: 100-continue
。我该怎么做?
我发现我应该可以通过以下方式设置自定义 header:
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));
}
但这不起作用。
如何设置自定义 header 并禁用某些?
我也没有使用本机 php,而是 HipHop VM 3.12.1(rel)。
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
例子,.
所以构造将是:
'stream_context' => stream_context_create(
[
'http' => [
'header' => "SomeCustomHeader: value\r\n".
"Expect: \r\n",
],
]
),
或者:
'stream_context' => stream_context_create(
[
'http' => [
'header' => [
'SomeCustomHeader: value',
'Expect: ',
],
],
]
),
但在您的情况下,最有可能的原因是您使用的 HHVM 版本 - 这是 a bug in HHVM which doesn't appears to have been fixed in 3.15.0 因此您可能想尝试升级 HHVM 并重试。
是的,我已经使用 stream_context_create
来设置一些额外的 HTTP header。我不知道删除其中一个,但也许你可以覆盖一个。那么,您是否尝试过设置一个空的 Expect
header?
$headers = [
'stream_context' => stream_context_create(
[
'http' => [
'header' => 'Expect: ',
],
]
),
];
一般来说,这种方法有点矫枉过正,但如果你想解决 stream_context_create
周围的 HHVM 错误,你可以尝试完全覆盖 SoapClient::__doRequest
有点喜欢这个
public function __doRequest($request, $location, $action, $version, $one_way=0)
{
// @note Omitted Expect: 100-continue
$headers = [
'Accept: */*',
'Accept-Encoding: deflate, gzip',
'SOAPAction: ""',
'Content-Type: text/xml; charset=utf-8',
'Content-Length: ' . strlen($request),
'HTTP/1.1 100 Continue',
];
$ch = curl_init($location);
curl_setopt_array(
$ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $request,
CURLOPT_HTTPHEADER => $headers,
]);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
我将这种方法用于 implement SOAP with Attachments,并且效果很好,尽管是在标准 PHP 解释器下。
SoapClient 发送 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
我想禁用 Expect: 100-continue
。我该怎么做?
我发现我应该可以通过以下方式设置自定义 header:
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));
}
但这不起作用。
如何设置自定义 header 并禁用某些?
我也没有使用本机 php,而是 HipHop VM 3.12.1(rel)。
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
例子,.
所以构造将是:
'stream_context' => stream_context_create(
[
'http' => [
'header' => "SomeCustomHeader: value\r\n".
"Expect: \r\n",
],
]
),
或者:
'stream_context' => stream_context_create(
[
'http' => [
'header' => [
'SomeCustomHeader: value',
'Expect: ',
],
],
]
),
但在您的情况下,最有可能的原因是您使用的 HHVM 版本 - 这是 a bug in HHVM which doesn't appears to have been fixed in 3.15.0 因此您可能想尝试升级 HHVM 并重试。
是的,我已经使用 stream_context_create
来设置一些额外的 HTTP header。我不知道删除其中一个,但也许你可以覆盖一个。那么,您是否尝试过设置一个空的 Expect
header?
$headers = [
'stream_context' => stream_context_create(
[
'http' => [
'header' => 'Expect: ',
],
]
),
];
一般来说,这种方法有点矫枉过正,但如果你想解决 stream_context_create
周围的 HHVM 错误,你可以尝试完全覆盖 SoapClient::__doRequest
有点喜欢这个
public function __doRequest($request, $location, $action, $version, $one_way=0)
{
// @note Omitted Expect: 100-continue
$headers = [
'Accept: */*',
'Accept-Encoding: deflate, gzip',
'SOAPAction: ""',
'Content-Type: text/xml; charset=utf-8',
'Content-Length: ' . strlen($request),
'HTTP/1.1 100 Continue',
];
$ch = curl_init($location);
curl_setopt_array(
$ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $request,
CURLOPT_HTTPHEADER => $headers,
]);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
我将这种方法用于 implement SOAP with Attachments,并且效果很好,尽管是在标准 PHP 解释器下。