Search code examples
phpsoapwsdlsoapheader

Set xml rquest header in non-wsdl soap client using php


I am trying to make a non-wsdl SOAP client call using php. My code is something like this:

try {
$URL = 'http://example.com/webservices/security/accesscontrol.asmx';

$sc = new SoapClient(null, array(
  'location' => $URL,
  'uri' => 'http://example.com/webservices/security/',
  'trace' => 1
  ));

$usertoken = array('UserNameToken' =>
array(
  'UserName' => 'test',
  'Password' => 'test123'
));

$header = new SoapHeader('http://www.docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', $usertoken);

$sc->__setSoapHeaders($header);

$test = $sc->__soapCall("AuthenticateClient",
  array(),
  array('soapaction' => 'http://example.com/webservices/security/AuthenticateClient')
);

If I debug and see the Last request header part of xml it looks like this:

<SOAP-ENV:Header>
    <ns2:Security>
        <item><key>UserNameToken</key><value><item><key>UserName</key><value>test</value></item><item><key>Password</key><value>test123</value></item></value></item>
    </ns2:Security>
</SOAP-ENV:Header>

But if I use wsdl file, the xml header looks like this:

<SOAP-ENV:Header>
    <ns2:Security>
        <ns2:UserNameToken>
            <ns2:UserName>test</ns2:UserName>
            <ns2:Password>test123</ns2:Password>
        </ns2:UserNameToken>
    </ns2:Security>
</SOAP-ENV:Header>

How can I make the header part like above using non-wsdl SOAP client call? Becasue its not working and giving an error that is caused by "if either the UserName Token or the UserName was not provided in the AuthenticateClient Soap Header Request"

Thanks in advance for your help.

Please note that I have changed the URL and password intentionally as I can not disclose them.


Solution

  • You can create the part of the header manually and insert it into the SoapHeader, try to do something like this:

        $URL = 'http://example.com/webservices/security/accesscontrol.asmx';
    
        $soapClient = new SoapClient(null, array(
            'location' => $URL,
            'uri' => 'http://example.com/webservices/security/',
            'trace' => 1
        ));
    
        $headerPart = '
                <SOAP-ENV:Header>
                    <ns2:Security>
                        <ns2:UserNameToken>
                            <ns2:UserName>DASKO</ns2:UserName>
                            <ns2:Password>welcome1</ns2:Password>
                        </ns2:UserNameToken>
                    </ns2:Security>
                </SOAP-ENV:Header>
        ';
    
        $soapVarHeader = new SoapVar($headerPart, XSD_ANYXML, null, null, null);
    
        $header = new SoapHeader(
            'http://www.docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', // Namespace - namespace of the WebService
            'Security',
            $soapVarHeader,
            false // mustunderstand
        );
    
        $soapClient->__setSoapHeaders($header);