How can I create a soap header like this?

Making some SOAP calls to a third-party application. They provide this soap header as an example of what the application expects. How can I create a SOAP header like this in PHP?

<SOAP-ENV:Header>
    <NS1:Security xsi:type="NS2:Security" xmlns:NS1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:NS2="urn:dkWSValueObjects">
        <NS2:UsernameToken xsi:type="NS2:TUsernameToken">
            <Username xsi:type="xsd:string">XXXX</Username> 
            <Password xsi:type="xsd:string">XXX</Password> 
        </NS2:UsernameToken>
    </NS1:Security>
</SOAP-ENV:Header>

I do what I think is right and continue to receive in return so that no headers are sent.

Here is an example from my code.

class SOAPStruct 
{
    function __construct($user, $pass) 
    {
        $this->Username = $user;
        $this->Password = $pass;
    }
}

$client = new SoapClient("http://www.example.com/service");

$auth = new SOAPStruct("username", "password");
$header = new SoapHeader("http://example.com/service", "TUsernameToken", $auth);

$client->__setSoapHeaders(array($header));
$client->__soapCall("GetSubscriptionGroupTypes", array(), NULL, $header)

And this is the SOAP header that I will return. (there is more of it, but I removed information that may be sensitive)

<SOAP-ENV:Header>
    <ns2:TUsernameToken>
        <Username>username</Username> 
        <Password>password</Password> 
    </ns2:TUsernameToken>
</SOAP-ENV:Header>
+3
source share
1 answer

SOAP PHP , , SoapHeader - .

, - XML , SoapClient::__doRequest() , SoapClient.

class My_SoapClient extends SoapClient
{
    public function __doRequest($request, $location, $action, $version, $one_way = 0)
    {
        $xmlRequest = new DOMDocument('1.0');
        $xmlRequest->loadXML($request);

        /*
         * Do your processing using DOM 
         * e.g. insert security header and so on
         */

        $request = $xmlRequest->saveXML();
        return parent::__doRequest($request, $location, $action, $version, $one_way);
    }
}

SoapClient::__doRequest .

+3

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


All Articles