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.
source share