使用php在非wsdl soap客户端中设置xml请求头


Set xml rquest header in non-wsdl soap client using php

我正在尝试使用php进行非wsdl SOAP客户端调用。我的代码是这样的:

try {
$URL = 'http://example.com/webservices/security/accesscontrol.asmx';
$sc = new SoapClient(null, array(
  'location' => $URL,
  'uri' => 'http://example.com/webservices/security/',
  'trace' => 1
  ));
$usertoken = array('UserNameToken' =>
array(
  'UserName' => 'test',
  'Password' => 'test123'
));
$header = new SoapHeader('http://www.docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', $usertoken);
$sc->__setSoapHeaders($header);
$test = $sc->__soapCall("AuthenticateClient",
  array(),
  array('soapaction' => 'http://example.com/webservices/security/AuthenticateClient')
);

如果我调试并看到xml的最后请求头部分,它看起来像这样:

<SOAP-ENV:Header>
    <ns2:Security>
        <item><key>UserNameToken</key><value><item><key>UserName</key><value>test</value></item><item><key>Password</key><value>test123</value></item></value></item>
    </ns2:Security>
</SOAP-ENV:Header>

但是如果我使用wsdl文件,xml头看起来像这样:

<SOAP-ENV:Header>
    <ns2:Security>
        <ns2:UserNameToken>
            <ns2:UserName>test</ns2:UserName>
            <ns2:Password>test123</ns2:Password>
        </ns2:UserNameToken>
    </ns2:Security>
</SOAP-ENV:Header>

我如何使用非wsdl SOAP客户端调用使标题部分像上面一样?因为它不工作,并给出由"如果UserName令牌或UserName未在AuthenticateClient Soap报头请求中提供"引起的错误

提前感谢您的帮助。

请注意,我已经故意更改了URL和密码,因为我不能透露它们

您可以手动创建报头的一部分并将其插入SoapHeader,尝试这样做:

    $URL = 'http://example.com/webservices/security/accesscontrol.asmx';
    $soapClient = new SoapClient(null, array(
        'location' => $URL,
        'uri' => 'http://example.com/webservices/security/',
        'trace' => 1
    ));
    $headerPart = '
            <SOAP-ENV:Header>
                <ns2:Security>
                    <ns2:UserNameToken>
                        <ns2:UserName>DASKO</ns2:UserName>
                        <ns2:Password>welcome1</ns2:Password>
                    </ns2:UserNameToken>
                </ns2:Security>
            </SOAP-ENV:Header>
    ';
    $soapVarHeader = new SoapVar($headerPart, XSD_ANYXML, null, null, null);
    $header = new SoapHeader(
        'http://www.docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', // Namespace - namespace of the WebService
        'Security',
        $soapVarHeader,
        false // mustunderstand
    );
    $soapClient->__setSoapHeaders($header);