我想创建一个WSS标头来对安全的Web服务进行身份验证。
我可以用丑陋的方式做到这一点:
$auth = '
<wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsu:Timestamp wsu:Id="Timestamp-28" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsu:Created>' . $timestamp . '</wsu:Created>
<wsu:Expires>' . $timestampExpires . '</wsu:Expires>
</wsu:Timestamp>
<wsse:UsernameToken wsu:Id="UsernameToken-27" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>' . $user . '</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">' . $passdigest . '</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">' . $encodedNonce . '</wsse:Nonce>
<wsu:Created>' . $timestamp . '</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>';
我现在正在尝试使用SimpleXML更清洁。
但如果我尝试做一个简单的事情:
$xml = new SimpleXMLElement('<wsse:Security/>', 0, false, 'wsse');
我明白了:
警告:SimpleXMLElement :: __ construct()[simplexmlelement .-- construct]: 名称空间错误:安全性上的名称空间前缀wsse未在
中定义
我想我错过了创建命名空间xmls的方法,你能给我一些提示吗?
答案 0 :(得分:0)
我找到了解决问题的方法:
$root = new SimpleXMLElement('<root/>');
$security = $root->addChild('wsse:Security', 'test', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
$root->registerXPathNamespace('wsse', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
$auth = $root->xpath('/root/wsse:Security');
echo htmlentities($auth[0]->asXML());
显示:
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">test</wsse:Security>
而且,我的XML中存在错误,我放了一个SOAP-ENV:mustUnderstand="1"
,但我从未定义SOAP-ENV
命名空间。