SoapHeader 子节点中的 PHP 命名空间

标签 php xml namespaces soapheader

PHP SoapClient header 。我在获取子节点中的 namespace 时遇到问题。这是我正在使用的代码:

$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>

我需要将 namespace 引用获取到 UsernameToken、Password 和 Username 节点中。非常感谢任何帮助。

谢谢。

最佳答案

Davidright answer .他也说得对,这需要太多的努力和思考。这是一个变体,它封装了任何使用此特定 wsse 安全 header 的人的丑陋之处。

清理客户端代码

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

}

关于SoapHeader 子节点中的 PHP 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13465168/

相关文章:

java - 有没有办法将xml文件数据插入cassandra?

java - 在 Android/Java 中更改 XML 值

excel - 从 DOCX4j 中的 xml 中删除不必要的命名空间

php - 使用 PHP 删除编码

javascript - 将数据从 PHP 脚本发送到 jQuery Ajax 请求

php - 000webhosting 数据库不允许我向 phpmyadmin 发送值

xml - mule esb 中 #[message.payloadAs(java.lang.String)] 的目的是什么

vb.net - 命名空间 - 对类进行逻辑分组

node.js - 如何将参数传递给 jake 任务,例如命名空间 :task parameter?

php - 内连接 : where clause is too ambiguous