How to add HTTP headers to a SOAP request?

From PHP-script I send the SOAP request. In addition to the data in the request body, the server expects from me two more of the HTTP header. Can't figure out how to send them to him.

What I am doing.

$oSoapClient = new SoapClient( $url, $aOptions );

$aHeaders = array();
$aHeaders[] = new SoapHeader( 'http://soapinterop.org/echoheader/', 'token', $token ); 
$aHeaders[] = new SoapHeader( 'http://soapinterop.org/echoheader/', 'user-token', $user_id ); 

$oSoapClient->__setSoapHeaders( $aHeaders );

$result = $oSoapClient->__doRequest( $sXml, $url, null, SOAP_1_1 );


And the server returns an error stating that the required headers not found.
From the documentation for the SOAP php.net anything new is not found.

What am I doing wrong?
April 7th 20 at 15:47
1 answer
April 7th 20 at 15:49
Solution
__setSoapHeaders sets the soap headers, not http. They are placed in the Header element. Your example code something like this:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="yourns"
xmlns:ns2="http://soapinterop.org/echoheader/">
<SOAP-ENV:Header>
<ns2:token>token</ns2:token>
<ns2:user-token>user_id</ns2:user-token>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
 <!-- the body of the request -->
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


To add what http headers you need to create a stream context:
$aOptions['stream_context'] = stream_context_create([
 'http' => [
 'header' => "token: abc\r\nuser-token: 123"
]
]);

$oSoapClient = new SoapClient( $url, $aOptions );
$result = $oSoapClient->__doRequest( $sXml, $url, null, SOAP_1_1 );
Thank you for the tip. I also have come to understand that she was trying to pass the headers of the body of a SOAP request, not the HTTP headers. And also now mess around with the function stream_context_create().

I now the server says: access to the service for token is prohibited. And now a mystery to me in this.
"token" here probably refers to the name of the http header that I am passing, not the value of the token. And why it causes the error, I can't understand, because the header name I take from guide to connect to the API.

Headers should look like this: FNS-OpenApi-Token and FNS-OpenApi-UserToken.
The second should be represented as a base64-encoded string. Now my code looks like this.

$options[ 'stream_context' ] = stream_context_create([

 'http' => array(

 'header' => "FNS-OpenApi-token:" . $token . "\r\nFNS-OpenApi-UserToken: ". $userToken

)

]);

Well, in response I get an access denied message for the token.
Calling the method $oSoapClient->__getLastRequestHeaders() shows these headers:

POST /open-api/ais3/KktService/0.1 HTTP/1.1
Host: openapi.nalog.EN:8090
Connection: Keep-Alive
User-Agent: PHP-SOAP/7.2.24-0ubuntu0.18.04.2
Content-Type: text/xml; charset=utf-8
SOAPAction: "urn:SendMessageRequest"
Content-Length: 1520
FNS-OpenApi-token: d23597c6486f4b53b873eed030dae3fb
FNS-OpenApi-UserToken: 0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtga0l7qutcw


That is, the headers are sent, but not accepted, for some reason. - jaden commented on April 7th 20 at 15:52
Once the request is technically in compliance with the documentation, then the problem is administrative and can only be resolved with a support request. Ask them why with your token requests are denied. - Oma commented on April 7th 20 at 15:55
@ethel_Rowe10, thank you for the advice. Took care of it, fixed all errors, it works.

Am now facing a different problem. Using JavaScript to access the camera of your smartphone to read the QR code, and process information from it. But that's another topic ) - jaden commented on April 7th 20 at 15:58

Find more questions by tags PHPSOAPXMLXSD