Dav*_*vid 14 php xml namespaces soapheader
PHP SoapClient标头.我在子节点中获取命名空间时遇到问题.这是我正在使用的代码:
$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);
Run Code Online (Sandbox Code Playgroud)
这是它生成的XML:
<ns2:Security>
<UsernameToken>
<Password>MyPassword</Password>
<Username>MyUsername</Username>
</UsernameToken>
</ns2:Security>
Run Code Online (Sandbox Code Playgroud)
这是我希望它生成的XML:
<ns2:Security>
<ns2:UsernameToken>
<ns2:Password>MyPassword</ns2:Password>
<ns2:Username>MyUsername</ns2:Username>
</ns2:UsernameToken>
</ns2:Security>
Run Code Online (Sandbox Code Playgroud)
我需要将名称空间引用添加到UsernameToken,Password和Username节点中.任何帮助将非常感激.
谢谢.
del*_*ala 13
大卫有正确的答案.他也是对的,它需要花费太多精力和思想.这是一个变体,它包含了处理这个特定的wsse安全头的任何人的丑陋.
清理客户端代码
$client = new SoapClient('http://some-domain.com/service.wsdl');
$client->__setSoapHeaders(new WSSESecurityHeader('myUsername', 'myPassword'));
Run Code Online (Sandbox Code Playgroud)
并实施......
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*vid 11
弄清楚了.我使用嵌套的SoapVars和数组.
$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);
Run Code Online (Sandbox Code Playgroud)
这需要花费太多的精力和思想......