基于PHP的SOAP头如何生成特定格式?

时间:2017-10-17 04:25:28

标签: php soap soap-client

我想生成以下SOAP标头格式

<soapenv:Header>
    <SoapHeaderMsg xmlns="http://xyz.com.au">
        <opt:UserSoapHeader>
            <opt:IdentityName>TEST</opt:IdentityName>
            <opt:AuthenticationToken>jjjkjkjkjkjkj</opt:AuthenticationToken>
        </opt:UserSoapHeader>
    </SoapHeaderMsg>
</soapenv:Header>

所以我使用以下php函数来生成它,

$this->__setSoapHeaders(array(
            new SoapHeader('http://xyz.com.au', 'SoapHeaderMsg', array(
                new SoapHeader('http://xyz.com.au', 'IdentityName', 'TEST'),
                new SoapHeader('http://xyz.com.au', 'AuthenticationToken', 'jkjkjkk')
            )),
        ));

哪个生成以下标题与上面我想要的完全不同?如何使用上面的PHP函数生成完全相同的标题?

<SOAP-ENV:Header>
        <ns1:SoapHeaderMsg>
            <SOAP-ENC:Struct>
                <namespace>http://xyz.com.au</namespace>
                <name>IdentityName</name>
                <data>TEST</data>
                <mustUnderstand>false</mustUnderstand>
            </SOAP-ENC:Struct>
            <SOAP-ENC:Struct>
                <namespace>http://xyz.com.au</namespace>
                <name>AuthenticationToken</name>
                <data>hjhhjjhjhjhj</data>
                <mustUnderstand>false</mustUnderstand>
            </SOAP-ENC:Struct>
        </ns1:SoapHeaderMsg>
    </SOAP-ENV:Header>

1 个答案:

答案 0 :(得分:1)

array主要用于PHP端,转换为Struct。您可以尝试使用object,看看您是否取得了任何成功。

$this->__setSoapHeaders(array(
        new SoapHeader('http://xyz.com.au', 'SoapHeaderMsg', 
            (object)array( 
                'opt:UserSoapHeader' => (object)array(
                    'opt:IdentityName' => 'TEST',
                    'opt:AuthenticationToken' => 'jkjkjkk'
                )
        )),
    ));

请求如下所示:

<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:ns1="http://test.domain.com/"
                   xmlns:ns2="http://xyz.com.au">
      <SOAP-ENV:Header>
        <ns2:SoapHeaderMsg>
              <opt:UserSoapHeader>
                    <opt:IdentityName>TEST</opt:IdentityName>
                    <opt:AuthenticationToken>jkjkjkk</opt:AuthenticationToken>
              </opt:UserSoapHeader>
        </ns2:SoapHeaderMsg>
      </SOAP-ENV:Header>
      <SOAP-ENV:Body>
            ......
      </SOAP-ENV:Body>
</SOAP-ENV:Envelope>