【问题标题】:Passing Authentication Credentials in SoapHeader class在 SoapHeader 类中传递身份验证凭据
【发布时间】:2016-06-29 09:40:18
【问题描述】:

我有一个格式如下的 xml 文件

<?php

$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tusa="http://schemas.datacontract.org/2004/07/Tusa.Services.ConsumerConnect" xmlns:tem="http://tempuri.org/">
<soap:Header>
    <AuthenticationCredentials>
    <tusa:Password>XXXXX</tusa:Password>
    <tusa:Username>XXXXX</tusa:Username>
    </AuthenticationCredentials>
</soap:Header>
<soap:Body>
    <tem:AddProduct>
        <tem:CustomerDetail>
                <tusa:AddressLine1>52 TEST DRIVE</tusa:AddressLine1>
                <tusa:City>JOHANNESBURG</tusa:City>
                <tusa:MaritalStatus>Unknown</tusa:MaritalStatus>
                //more nodes
        </tem:CustomerDetail>
    </tem:AddProduct>
</soap:Body>
</soap:Envelope>
XML;

给定以下 SOAP 标头

<soap:Header>
    <AuthenticationCredentials>
    <tusa:Password>XXXXX</tusa:Password>
    <tusa:Username>XXXXX</tusa:Username>
    </AuthenticationCredentials>
</soap:Header>

如何将用户名和密码传递给 SoapHeader 类。我尝试使用下面的代码,但出现错误

需要身份验证凭据

try
{
    $auth = array(
        'Username'=>'XXXXX',
        'Password'=>'XXXXX',
    );

    $client = new \SoapClient('https://test.com//Indirect.svc?wsdl', array('soap_version' => SOAP_1_2, 'trace' => 1));

    $actionHeader = new \SoapHeader('http://www.w3.org/2005/08/addressing', Action', http://tempuri.org/IInDirect/AddProduct');

    $client->__setSoapHeaders($actionHeader);

    $response = $client->AddProduct($xmlstr);
}

catch (\Exception $e)
{
    echo "Error!";
    echo $e -> getMessage ();
    echo 'Last response: '. $client->__getLastResponse();
}

【问题讨论】:

  • 未能理解文档。您将如何使用您提供的链接完成这项工作?

标签: php soap wsdl soap-client soapheader


【解决方案1】:

我认为您正在寻找类似的东西

$auth = [
    'Username' => 'XXXXX',
    'Password' => 'XXXXX',
];

$ns = 'http://www.w3.org/2003/05/soap-envelope/';

$header = new SOAPHeader($ns, 'AuthenticationCredentials', $auth);        

$client->__setSoapHeaders($header);

【讨论】:

  • 仍然不起作用,当我使用您的代码时,我现在在Error!The SOAP action specified on the message, '', does not match the HTTP SOAP Action, 'http://tempuri.org/IInDirect/AddProduct'. Last response: http://www.w3.org/2005/08/addressing/faults:Sendera:ActionMismatchThe SOAP action specified on the message, '', does not match the HTTP SOAP Action, 'http://tempuri.org/IInDirect/AddProduct'. a:Action下方收到错误
  • 尝试将我的答案中的 $ns 替换为 tempuri.org/IInDirect/AddProduct
猜你喜欢
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
相关资源
最近更新 更多