2011-05-30 182 views
6

我正在編寫一個基於PHP的SOAP客戶端應用程序,它使用PHP5原生的SOAP庫。我需要發送一個HTTP cookie和一個額外的HTTP頭作爲請求的一部分。 cookie的部分是沒有問題的:SoapClient設置自定義HTTP標頭

代碼:

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

我的問題是HTTP頭。我希望有會是一個名爲

__setHeader

__setHttpHeader

在SOAP庫

功能。但沒有這樣的運氣。

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

(I這裏http://www.phpfreaks.com/forums/index.php?topic=125387.0發現這個unanswerd問題,我複製它二/三我同樣的問題)

回答

-2

SoapClient::__soapCall方法具有$input_headers論點,這需要的SoapHeader秒的陣列。

您也可以使用Zend Framework的SOAP客戶端,該客戶端提供addSoapInputHeader便利方法。

+1

這對的SOAPHeaders但我需要/尋找一種方式來修改由SoapClient的產生的請求的HTTP頭。 – user308891 2011-05-30 18:10:02

+0

呵呵,HTTP頭文件!對不起,關於:) nuSOAP支持cookies,但你必須修改它來添加標題。 – igorw 2011-05-30 18:36:38

11

嘗試設置流上下文的SOAP客戶端:

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

這答案盡在PHP 5.3+ SoapClient set custom HTTP Header

但是正確的方法,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); 
    } 
} 
+0

我甚至不得不用PHP 5.3.10來做到這一點。 – leedm777 2017-07-25 16:21:54

2

我遇到了一種情況,我不得不提供一個哈希值,用於驗證目的的請求的HTTP頭中的soap請求的所有文本。

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

也許有人搜索會發現這個有用:我通過繼承SoapClient並使用stream_context選項設置頁眉做到了這一點。

0

其容易的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');