2012-12-04 37 views
1

我想创建一个WSS头来验证安全的Web服务。使用SimpleXML创建WS-Security标头?

我可以用一个丑陋的做到这一点:

$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 :: __结构()的SimpleXMLElement .--构建]: 命名错误:命名空间前缀wsse on安全未定义

我想我错过了创建命名空间xml的方法,你能给我一些提示吗?

回答

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命名空间。