Reputation: 391
PHP SoapClient Headers. I'm having a problem getting the namespaces in child nodes. Here's the code I'm using:
$security = new stdClass;
$security->UsernameToken->Password = 'MyPassword';
$security->UsernameToken->Username = 'MyUsername';
$header[] = new SOAPHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', $security);
$client->__setSoapHeaders($header);
Here's the XML it generates:
<ns2:Security>
<UsernameToken>
<Password>MyPassword</Password>
<Username>MyUsername</Username>
</UsernameToken>
</ns2:Security>
Here's the XML I want it to generate:
<ns2:Security>
<ns2:UsernameToken>
<ns2:Password>MyPassword</ns2:Password>
<ns2:Username>MyUsername</ns2:Username>
</ns2:UsernameToken>
</ns2:Security>
I need to get the namespace reference into the UsernameToken, Password and Username nodes. Any help would be really appreciated.
Thanks.
Upvotes: 16
Views: 9779
Reputation: 942
David has the right answer. And he is also right that it takes way too much effort and thought. Here's a variation that encapsulates the ugliness for anyone working this particular wsse security header.
Clean client code
$client = new SoapClient('http://some-domain.com/service.wsdl');
$client->__setSoapHeaders(new WSSESecurityHeader('myUsername', 'myPassword'));
And the implementation...
class WSSESecurityHeader extends SoapHeader {
public function __construct($username, $password)
{
$wsseNamespace = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
$security = new SoapVar(
array(new SoapVar(
array(
new SoapVar($username, XSD_STRING, null, null, 'Username', $wsseNamespace),
new SoapVar($password, XSD_STRING, null, null, 'Password', $wsseNamespace)
),
SOAP_ENC_OBJECT,
null,
null,
'UsernameToken',
$wsseNamespace
)),
SOAP_ENC_OBJECT
);
parent::SoapHeader($wsseNamespace, 'Security', $security, false);
}
}
Upvotes: 15
Reputation: 391
Figured it out. I used nested SoapVars and arrays.
$ns_s = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
$node1 = new SoapVar('MyUsername', XSD_STRING, null, null, 'Username', $ns_s);
$node2 = new SoapVar('MyPassword', XSD_STRING, null, null, 'Password', $ns_s);
$token = new SoapVar(array($node1,$node2), SOAP_ENC_OBJECT, null, null, 'UsernameToken', $ns_s);
$security = new SoapVar(array($token), SOAP_ENC_OBJECT, null, null, 'Security', $ns_s);
$header[] = new SOAPHeader($ns_s, 'Security', $security, false);
That took entirely too much effort and thought...
Upvotes: 13