【问题标题】:Set xml rquest header in non-wsdl soap client using php使用 php 在非 wsdl 肥皂客户端中设置 xml 请求标头
【发布时间】:2016-06-29 06:40:56
【问题描述】:

我正在尝试使用 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 的 Last request header 部分,它看起来像这样:

<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 客户端调用来制作像上面那样的标头部分?因为它不起作用并给出由“如果在 AuthenticateClient Soap 标头请求中未提供用户名令牌或用户名”引起的错误

提前感谢您的帮助。

请注意,我故意更改了网址和密码,因为我无法透露它们。

【问题讨论】:

    标签: php soap wsdl soapheader


    【解决方案1】:

    您可以手动创建标题部分并将其插入到 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      相关资源
      最近更新 更多