SoapClient设置了自定义HTTP标头


SoapClient set custom HTTP Header

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

代码:

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

我的问题是HTTP标头。我希望有一个名为的函数

__setHeader

__setHttpHeader

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

还有其他人处理过这个吗?有变通办法吗?使用不同的SOAP库会更容易吗?谢谢

(我在这里发现了这个无法回答的问题http://www.phpfreaks.com/forums/index.php?topic=125387.0,我复制了它b/c我有同样的问题)

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

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

这个答案是在PHP 5.3+SoapClient中设置自定义HTTP标头的正确方法

然而,PHP 5.2并没有考虑流上下文中的所有值。为了解决这个问题,您可以创建一个子类来为您处理它(以一种巧妙的方式,但它很有效)。

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);
  }
}

我遇到了一种情况,为了进行身份验证,我必须在请求的HTTP标头中提供soap请求的所有文本的哈希。我通过将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)); 
    }       
}

也许有人搜索会发现这很有用。

它很容易在nuSoap:中实现

NUSOAP.PHP

添加到类nusoap_base:

var additionalHeaders = array();

则转到函数发送同级的

并添加

foreach ($this->additionalHeaders as $key => $value) {
    $http->setHeader($key, $value);
}

附近的某个地方

$http->setSOAPAction($soapaction); (line 7596)

现在您可以轻松设置标题:

$soapClient = new nusoap_client('wsdl adress','wsdl');
$soapClient->additionalHeaders = array('key'=>'val','key2'=>'val');

SoapClient::__soapCall方法有一个$input_headers参数,该参数采用SoapHeader s的数组。

您还可以使用ZendFramework的SOAP客户端,它提供了一个addSoapInputHeader方便的方法。