SoapClient设置自定义HTTP标头

| 我正在写一些使用基于PHP5的SOAP库的基于PHP的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数组。 您还可以使用Zend Framework的SOAP客户端,该客户端提供了一种便捷的方法。     

要回复问题请先登录注册