0

I have an xml file in format below

<?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;

Given the following SOAP header

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

How can I pass the username and password to the SoapHeader class. I have tried using the code below but I am getting an error

Authentication Credentials are required

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();
}
jaahvicky
  • 438
  • 2
  • 8
  • 20
  • Have you seen [SoapHeader::SoapHeader](http://php.net/manual/en/soapheader.soapheader.php) –  Jun 29 '16 at 09:43
  • failing to understand the documentation. how would you go about to make this work using the link you provided? – jaahvicky Jun 29 '16 at 12:49

1 Answers1

1

I think you're looking for something like this

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

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

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

$client->__setSoapHeaders($header);
  • Still doesn't work, when I use your code I am now getting error below `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` – jaahvicky Jun 29 '16 at 14:42
  • Try replacing $ns in my answer with http://tempuri.org/IInDirect/AddProduct –  Jun 29 '16 at 14:51