簡體   English   中英

SoapClient 設置自定義 HTTP Header

[英]SoapClient set custom HTTP Header

我正在編寫一個基於 PHP 的 SOAP 客戶端應用程序,該客戶端應用程序使用 PHP5 原生的 SOAP 庫。 我需要發送一個 HTTP cookie 和一個額外的 HTTP header 作為請求的一部分。 cookie部分沒問題:

代碼:

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

我的問題是 HTTP header。 我希望會有一個名為 function

__setHeader

或者

__setHttpHeader

在 SOAP 庫中。 但沒有這樣的運氣。

其他人處理過這個嗎? 有解決方法嗎? 不同的 SOAP 庫會更容易使用嗎? 謝謝。

(我在這里http://www.phpfreaks.com/forums/index.php?topic=125387.0找到了這個未回答的問題,我復制了它 b/c 我有同樣的問題)

嘗試為 soap 客戶端設置 stream 上下文:

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

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. 我通過繼承SoapClient並使用stream_context選項設置 header 來完成此操作:

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

也許有人搜索會發現這很有用。

這個答案是在 PHP 5.3+ SoapClient 設置自定義 HTTP Header中的正確方法

但是,PHP 5.2 並未考慮 stream 上下文中的所有值。 為了解決這個問題,您可以創建一個為您處理它的子類(以一種 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);
  }
}

它很容易在 nuSoap 中實現:

NUSOAP.PHP

添加到 class nusoap_base

var additionalHeaders = array();

然后轉到 function發送相同的 class

並添加

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數組。

您還可以使用 Zend Framework 的 SOAP 客戶端,它提供了一個方便的addSoapInputHeader方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM