SoapClient sets custom HTTP header

I am working on writing a PHP-based SOAP client application that uses SOAP libraries native to PHP5. I need to send an HTTP cookie and an additional HTTP header as part of the request. Part of the cookie is not a problem:

the code:

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

My problem is the HTTP header. I was hoping there would be a function called

__ SetHeader

or

__ setHttpHeader

in SOAP libraries. But there is no such luck.

Has anyone else dealt with this? Is there a workaround? Would it be easier to work with another SOAP library? Thanks.

<sub> (I found this question unanswered here http://www.phpfreaks.com/forums/index.php?topic=125387.0 , I copied it b / c, I have the same problem)

+6
source share
5 answers

Try setting the thread context for the soap client:

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

This answer is the right way to do this in PHP 5.3+ SoapClient to set a custom HTTP header

However, PHP 5.2 does not take into account all values ​​from the context of the stream. To get around this, you can create a subclass that processes it for you (in a hacky manner, but it works).

 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); } } 
+2
source

I was faced with a situation where I had to provide a hash of the entire text of the soap request in the HTTP request header for authentication purposes. I accomplished this by subclassing SoapClient and using the stream_context parameter to set the 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)); } } 

Perhaps someone will find this useful.

+2
source

it is easy to implement in nuSoap:

nusoap.php

add to nusoap_base class:

 var additionalHeaders = array(); 

then goto function send the same class

and add

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

somewhere nearby (before)

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

Now you can easily set the headers:

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

SoapClient::__soapCall has the argument $input_headers , which takes an array of SoapHeader s.

You can also use the SOAP client for the Zend Framework, which provides a convenient addSoapInputHeader method.

-2
source

Source: https://habr.com/ru/post/889396/


All Articles