gpt4 book ai didi

php - 使用 SimpleXML 创建 WS-Security header ?

转载 作者:行者123 更新时间:2023-12-04 16:52:23 25 4
gpt4 key购买 nike

我想创建一个 WSS header 来对 protected 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');

我得到:

warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: namespace error : Namespace prefix wsse on Security is not defined in



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

最佳答案

我找到了解决问题的方法:

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

关于php - 使用 SimpleXML 创建 WS-Security header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13706549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com