php SoapClient 设置自定义 HTTP Header
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6179138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SoapClient set custom HTTP Header
提问by user308891
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:
我正在编写一个基于 PHP 的 SOAP 客户端应用程序,该应用程序使用 PHP5 原生的 SOAP 库。我需要发送一个 HTTP cookie 和一个额外的 HTTP 标头作为请求的一部分。cookie部分没有问题:
Code:
代码:
$client = new SoapClient($webServiceURI, array("exceptions" => 0, "trace" => 1, "encoding" => $phpInternalEncoding));
$client->__setCookie($kkey, $vvalue);
My problem is the HTTP header. I was hoping there would have been a function named
我的问题是 HTTP 标头。我希望会有一个名为的函数
__setHeader
__setHeader
or
或者
__setHttpHeader
__setHttpHeader
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.
还有人处理过这个吗?有解决方法吗?使用不同的 SOAP 库会更容易吗?谢谢。
(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)
(我在这里发现了这个悬而未决的问题http://www.phpfreaks.com/forums/index.php?topic=125387.0,我复制了它 b/c 我有同样的问题)
回答by bedeabza
Try setting a stream context for the soap client:
尝试为soap客户端设置一个流上下文:
$client = new SoapClient($webServiceURI, array(
"exceptions" => 0,
"trace" => 1,
"encoding" => $phpInternalEncoding,
'stream_context' => stream_context_create(array(
'http' => array(
'header' => 'SomeCustomHeader: value'
),
)),
));
回答by Jose Torres
This answer is the proper way to do it in PHP 5.3+ SoapClient set custom HTTP Header
此答案是在 PHP 5.3+ SoapClient set custom HTTP Header 中执行此操作的正确方法
However, PHP 5.2 does not take all of the values from the stream context into consideration. To get around this, you can make a subclass that handles it for you (in a hacky way, but it works).
但是,PHP 5.2 并未考虑流上下文中的所有值。为了解决这个问题,你可以创建一个为你处理它的子类(以一种hacky的方式,但它有效)。
class SoapClientBackport extends SoapClient {
public function __construct($wsdl, $options = array()){
if($options['stream_context'] && is_resource($options['stream_context'])){
$stream_context_options = stream_context_get_options($options['stream_context']);
$user_agent = (isset($stream_context_options['http']['user_agent']) ? $stream_context_options['http']['user_agent'] : "PHP-SOAP/" . PHP_VERSION) . "\r\n";
if(isset($stream_context_options['http']['header'])){
if(is_string($stream_context_options['http']['header'])){
$user_agent .= $stream_context_options['http']['header'] . "\r\n";
}
else if(is_array($stream_context_options['http']['header'])){
$user_agent .= implode("\r\n", $stream_context_options['http']['header']);
}
}
$options['user_agent'] = $user_agent;
}
parent::__construct($wsdl, $options);
}
}
回答by Pete Neisen
I ran into a situation where I had to provide a hash of all the text of the soap request in the HTTP header of the request for authentication purposes. I accomplished this by subclassing SoapClientand using the stream_contextoption to set the header:
我遇到了一种情况,为了身份验证,我必须在请求的 HTTP 标头中提供肥皂请求的所有文本的哈希值。我通过子类化SoapClient并使用stream_context选项设置标头来完成此操作:
class AuthenticatingSoapClient extends SoapClient {
private $secretKey = "secretKeyString";
private $context;
function __construct($wsdl, $options) {
// Create the stream_context and add it to the options
$this->context = stream_context_create();
$options = array_merge($options, array('stream_context' => $this->context));
parent::SoapClient($wsdl, $options);
}
// Override doRequest to calculate the authentication hash from the $request.
function __doRequest($request, $location, $action, $version, $one_way = 0) {
// Grab all the text from the request.
$xml = simplexml_load_string($request);
$innerText = dom_import_simplexml($xml)->textContent;
// Calculate the authentication hash.
$encodedText = utf8_encode($innerText);
$authHash = base64_encode(hash_hmac("sha256", $encodedText, $this->secretKey, true));
// Set the HTTP headers.
stream_context_set_option($this->context, array('http' => array('header' => 'AuthHash: '. $authHash)));
return (parent::__doRequest($request, $location, $action, $version, $one_way));
}
}
Maybe someone searching will find this useful.
也许有人搜索会发现这很有用。
回答by AlexRausch
its easy to implement in nuSoap:
它很容易在 nuSoap 中实现:
NUSOAP.PHP
NUSOAP.PHP
add to class nusoap_base:
添加到类nusoap_base:
var additionalHeaders = array();
then goto function sendof the same class
然后转到同一个类的函数发送
and add
并添加
foreach ($this->additionalHeaders as $key => $value) {
$http->setHeader($key, $value);
}
somewhere around (just before)
某处(就在之前)
$http->setSOAPAction($soapaction); (line 7596)
now you can easy set headers:
现在您可以轻松设置标题:
$soapClient = new nusoap_client('wsdl adress','wsdl');
$soapClient->additionalHeaders = array('key'=>'val','key2'=>'val');
回答by igorw
The SoapClient::__soapCall
method has an $input_headers
argument, which takes an array of SoapHeader
s.
该SoapClient::__soapCall
方法有一个$input_headers
参数,它接受一个SoapHeader
s数组。
You could also use Zend Framework's SOAP client, which provides an addSoapInputHeader
convenience method.
您还可以使用 Zend Framework 的 SOAP 客户端,它提供了一种addSoapInputHeader
方便的方法。