SoapHeader子节点中的PHP命名空间

时间:2012-11-20 01:12:27

标签: 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);

以下是它生成的XML:

<ns2:Security>
  <UsernameToken>
    <Password>MyPassword</Password>
    <Username>MyUsername</Username>
  </UsernameToken>
</ns2:Security>

以下是我希望它生成的XML:

<ns2:Security>
  <ns2:UsernameToken>
    <ns2:Password>MyPassword</ns2:Password>
    <ns2:Username>MyUsername</ns2:Username>
  </ns2:UsernameToken>
</ns2:Security>

我需要将名称空间引用添加到UsernameToken,Password和Username节点中。任何帮助都会非常感激。

感谢。

2 个答案:

答案 0 :(得分:13)

Davidright answer。他也是对的,需要花费太多精力和思想。这是一个变体,它包含了处理这个特定的wsse安全头的任何人的丑陋。

清理客户端代码

$client = new SoapClient('http://some-domain.com/service.wsdl');
$client->__setSoapHeaders(new WSSESecurityHeader('myUsername', 'myPassword'));

实施......

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);
    }

}

答案 1 :(得分: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);

这需要花费太多的精力和思想......