2011-05-30 220 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');