2

I need to make a call to the webservice bellow, but so far failed to create the code for it.

https://www.nightline-delivers.com/ParcelMotel_UAT/RetailService.svc?wsdl

The request needs to look like the following:

<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ret="Retail">
   <soapenv:Header>
      <UserName>WebServiceSample</UserName>
      <Password>password</Password>
   </soapenv:Header>
   <soapenv:Body>
      <ret:ValidateCredentials>
         <ret:motelId>BAC032</ret:motelId>
         <ret:mobileNumber>0865596800</ret:mobileNumber>
      </ret:ValidateCredentials>
   </soapenv:Body>
</soapenv:Envelope>

My code so far looks like this:

$client = new SoapClient("https://www.nightline-delivers.com/ParcelMotel_UAT/RetailService.svc?wsdl");

$location = new StdClass();
$location->motelId = 'BAC032';
$location->mobileNumber = '0866696800';

$auth = array(
    'UserName'=>'WebServiceSample',
    'Password'=>'password',
);
$header = new SoapHeader('NAMESPACE','Auth',$auth,false);

$client->__setSoapHeaders( $header );

try {
    $response = $client->ValidateLocation( $location );
    var_dump($response);
} catch( Exception $e ) {
    echo $e->getMessage();
}

2 Answers 2

1
+25

According to this it is impossible in PHP to get a header element without namespace (which is the case in your example) so that is not possible with SoapClient. However you may try to send request where both UserName and Password fields are in the Retail namespace, maybe server will accept this:

<?php

$client = new SoapClient("https://www.nightline-delivers.com/ParcelMotel_UAT/RetailService.svc?wsdl", array('trace' => TRUE));

$location = new StdClass();
$location->motelId = 'BAC032';
$location->mobileNumber = '0866696800';

$auth = array(
    'UserName'=>'WebServiceSample',
    'Password'=>'password',
);
$headers = array();
foreach($auth as $k=>$v) {
    $headers[] = new SoapHeader('Retail', $k, $v);
}
$client->__setSoapHeaders( $headers );

try {
    $response = $client->ValidateCredentials( $location );
    var_dump($response);
} catch( Exception $e ) {
    echo "REQUEST: ".$client->__getLastRequest();
    echo $e->getMessage();
}

This code also will show you request if an exception occurs, and I have changed ValidateLocation to ValidateCredentials as in your example message. It produces following message:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="Retail">
<SOAP-ENV:Header>
    <ns1:UserName>WebServiceSample</ns1:UserName>
    <ns1:Password>password</ns1:Password>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
    <ns1:ValidateLocation>
        <ns1:motelId>BAC032</ns1:motelId>
        <ns1:mobileNumber>0866696800</ns1:mobileNumber>
    </ns1:ValidateLocation>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Sign up to request clarification or add additional context in comments.

Comments

0

I got it working. Here is the code:

<?php

$username = 'WebServiceSample';
$password = 'password';

function formatXML($xml)
{
    libxml_use_internal_errors(true);

    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = false;
    $doc->formatOutput = true;
    $doc->loadXML($xml);

    return $doc->saveXML();
}

try {
    $client = new SoapClient("https://www.nightline-delivers.com/ParcelMotel_UAT/RetailService.svc?wsdl", array(
        'trace' => true,
    ));

    $headers = array();

    $headers[] = new SoapHeader('Retail', 'UserName', 'WebServiceSample');
    $headers[] = new SoapHeader('Retail', 'Password', 'password');

    $client->__setSoapHeaders($headers);

    $params = array(
        'motelId' => 'BAz031',
        'mobileNumber' => '0866696800'
    );

    $result = $client->__soapCall('ValidateLocation', array('ValidateLocation' => $params), array(
        'location' => 'https://www.nightline-delivers.com/ParcelMotel_UAT/RetailService.svc/basic?APIKey=7C3A0EA9-C8D6-4DB0-ADF0-615BFD29DC1D'
    ));

} catch (SoapFault $e) {
    exit($e->getMessage());
}

$request  = $client->__getLastRequest();
$response = $client->__getLastResponse();

echo '<pre>';
echo htmlspecialchars( formatXML($request) );
echo "\n";
echo htmlspecialchars( formatXML($response) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.