SoapClient 设置自定义 HTTP Header

2021-12-24 00:00:00 php soap-client soap

我正在编写一个基于 PHP 的 SOAP 客户端应用程序,该应用程序使用 PHP5 原生的 SOAP 库.我需要发送一个 HTTP cookie 和一个额外的 HTTP 标头作为请求的一部分.cookie部分没问题:

I am doing some work writing a PHP-based SOAP client application that uses the SOAP libraries native to PHP5. I need to send a an HTTP cookie and an additional HTTP header as part of the request. The cookie part is no problem:

代码:

$client = new SoapClient($webServiceURI, array("exceptions" => 0, "trace" => 1, "encoding" => $phpInternalEncoding));
$client->__setCookie($kkey, $vvalue);

我的问题是 HTTP 标头.我希望会有一个名为

My problem is the HTTP header. I was hoping there would have been a function named

__setHeader

__setHttpHeader

在 SOAP 库中.但没有这样的运气.

in the SOAP libraries. But no such luck.

有其他人处理过这个吗?有解决方法吗?使用不同的 SOAP 库会更容易吗?谢谢.

Anyone else dealt with this? Is there a workaround? Would a different SOAP library be easier to work with? Thanks.

<子>(我在这里发现了这个悬而未决的问题 http://www.phpfreaks.com/forums/index.php?topic=125387.0,我复制了 b/c 我有同样的问题)

(I found this unanswerd question here http://www.phpfreaks.com/forums/index.php?topic=125387.0, I copied it b/c i've the same issue)

推荐答案

尝试为 soap 客户端设置流上下文:

Try setting a stream context for the soap client:

$client = new SoapClient($webServiceURI, array(
    "exceptions" => 0, 
    "trace" => 1, 
    "encoding" => $phpInternalEncoding,
    'stream_context' => stream_context_create(array(
        'http' => array(
            'header' => 'SomeCustomHeader: value'
        ),
    )),
));

相关文章