首页 > 解决方案 > 将 soapenv:mustUnderstand="1" 属性添加到带有 SimpleXMLElement 对象的 SOAP 请求的标头中的安全标记

问题描述

我的问题是 PHP 中的 SOAP 标头。我无法将 mustUnderstand 属性设置为肥皂请求。你能帮我看看我的代码吗?

$root = new SimpleXMLElement('<header/>');

$security = $root->addChild('wsse:Security', null, $ns_wsse);
$security->addAttribute('soapenv:mustUnderstand', true);

$usernameToken = $security->addChild('wsse:UsernameToken', null, $ns_wsse);
$usernameToken->addChild('wsse:Username', $username, $ns_wsse);
$usernameToken->addChild('wsse:Password', $password, $ns_wsse)->addAttribute('Type', $password_type);
$usernameToken->addChild('wsse:Nonce', $encoded_nonce, $ns_wsse)->addAttribute('EncodingType', $encoding_type);
$usernameToken->addChild('wsu:Created', $tm_created, $ns_wsu);

// Recovering XML value from that object
$root->registerXPathNamespace('wsse', $ns_wsse);

Header('Content-type: text/xml');
$full = $root->xpath('/header/wsse:Security');
$auth = $full[0]->asXML();
echo $auth;

输出:

<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" mustUnderstand="1">
<wsse:UsernameToken>
<wsse:Username></wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-usernametoken-profile-1.0#PasswordText"></wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">MTk5Njg4NjYzOQ==</wsse:Nonce>
<wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2018-08-13T10:57:52Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>

我想添加soapenv:mustUnderstand="1" 属性而不是mustUnderstand="1"。添加了其他属性,但无法添加“soapenv:”,因此我无法从 Web 服务接收数据。它给出了错误。

预期的标题:

 <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">

提前致谢。

标签: phpsoapsimplexmlsoapheaderwsse

解决方案


推荐阅读