2

I have to call a soap WebService that needs Basic Authentication. I tryed SoapUI with Basic Authentication and its working. WSDL page can be load from Browser correctly. There is no SSL releated problem.

I'm trying to do same call with SoapClient but not working. As I understand (with help of Burp Software) Authentication header isn't send with soapClient.

While I have this problem, I checked nearly all releated questions on StackOverflow and tryed everything that they suggest.

Here is my PHP code I'm working on it:

echo "<pre>";

$WSDL_URL='https://myserver.com/services/?wsdl';

$username='xxx';    
$password='yyy';

$params = array(
    'login' => $username,
    'password' => $password,
    'trace' => 0,
    'exceptions' => 0,
    'location' => $WSDL_URL,
    'uri' => $WSDL_URL
);

$soap = new SoapClient(null, $params);

$RESULT = $soap->serviceTest( 'HELLO' );
echo "<h1>ServiceTEST Result:</h1>";
print_r($RESULT);

Here is my error:

SoapFault Object
(
    [message:protected] => Client Error
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => /Applications/MAMP/htdocs/soap/test.php
    [line:protected] => 24
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => /Applications/MAMP/htdocs/soap/test.php
                    [line] => 24
                    [function] => __call
                    [class] => SoapClient
                    [type] => ->
                    [args] => Array
                        (
                            [0] => serviceTest
                            [1] => Array
                                (
                                    [0] => HELLO
                                )
                        )
                )
        )
    [previous:Exception:private] => 
    [faultstring] => Client Error
    [faultcode] => s:Client
)
Nuri Akman
  • 792
  • 3
  • 18
  • 41
  • Maybe try another SoapClient library like `zend-soap` or just post the [SOAP data via Curl](https://stackoverflow.com/questions/7120586/soap-request-in-php-with-curl/7157785#7157785). – odan Oct 30 '18 at 08:41
  • Dear @DanielO, I'm still use Curl to connect this webservice. Curl is ok. But, I want to go with coapClient. – Nuri Akman Oct 31 '18 at 06:27
  • Please paste the soapui request that is working for you. – Kamal Soni Nov 01 '18 at 11:13
  • Try to use this workaround: https://php.net/manual/en/soapclient.soapclient.php#101503 – AndreyP Nov 02 '18 at 15:59
  • The wsdl method names are in other language so it would be helpful if you post the soapui request in the comments which works for you. It will help clarify what works for you. – Kamal Soni Nov 04 '18 at 00:21

2 Answers2

1

I managed to get the list of the services without including login/password in the wsdl request, so yes soapclient is working with this server.

I got a 500 error when using the login and password so it seems to be an issue on server side.

Here is the code that can help you to have more details on soap response

<?php

$username='xxx'; 
$password='yyy';
$WSDL_URL='https://myserver.com/services/?wsdl';


$params = array(
    // witout params it works at least for the wsdl
    //'login' => $username,
    //'password' => $password
);
try {
    $soap = new SoapClient( $WSDL_URL, $params);


    $RESULT = $soap->__getFunctions();
    print_r($RESULT);
    // uncomment that and you'll have the error
    //$soap->serviceTest('bob');
}
catch (SoapFault $fault) {
    echo "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})";
}
catch (Exception $e) {
    echo $e->getMessage();
    echo $e->getTraceAsString();
}
Nuri Akman
  • 792
  • 3
  • 18
  • 41
C.Vergnaud
  • 857
  • 6
  • 15
0

There is a basic auth possible with the php SoapClient class. As stated out in the documentation for the SoapClient class constructor, you can use the options login and password for a basic authentication.

$client = new \SoapClient($wsdl, [
    'login' => $username,
    'password' => $password,
]);

That 's all. No further headers needed.

You have to know, that php will send the credentials only when invoking the service itself and not when fetching the wsdl file.

Edit after commenting

Here 's a way you can read the wsdl file with basic auth, too.

$client = new \SoapClient(
    'http://' . urlencode($login) . ':' . urlencode($password) . '@www.server.com/path/to/wsdl',
    [
        'login' => $login,
        'password' => $password
    ]
);

This works pretty well as well.

Marcel
  • 4,854
  • 1
  • 14
  • 24
  • Warning: SoapClient::SoapClient(https://myserver.com/?wsdl): failed to open stream: HTTP request failed! HTTP/1.1 **403 Forbidden** in /Applications/MAMP/htdocs/soap/index.php on line 42 Warning: SoapClient::SoapClient(): I/O warning : **failed to load external entity** "https://myserver.com/?wsdl" in /Applications/MAMP/htdocs/soap/index.php on line 42 Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://myserver.com/?wsdl' : **failed to load external** entity "https://myserver.com/?wsdl" in /Applications/MAMP/htdocs/soap/index.php on line 42 – Nuri Akman Oct 29 '18 at 00:41
  • This result could appear for several reasons. Can you call your wsdl file directly in the browser or is it protected by the basis auth, too? I 've added a way to call a wsdl file with basic auth. If the wsdl file could be called in the browser, you don 't need the basic auth for reading the wsdl file. Another reason could be that the WSDL file contains references to W3C hosted files that have a delay. These delayed files cannot be read by the soap client. Another reason could be, that the wsdl file is malformed or contains references, that don 't work. – Marcel Oct 29 '18 at 07:36
  • Maybe it's an SSL related issue. [Read more](https://stackoverflow.com/questions/12875409/soap-php-fault-parsing-wsdl-failed-to-load-external-entity) – odan Oct 29 '18 at 10:55
  • @Marcel, I tryed username and pass in WSDL URL. But, result is same. – Nuri Akman Oct 29 '18 at 14:09
  • It 's a bit of guesswork right now. I don 't know your wsdl file. I guess there are some xsd files referenced, which could not be fetched by the php soap client. – Marcel Oct 29 '18 at 14:23
  • @Marcel, I can call wsdl file directly in the browser. – Nuri Akman Oct 29 '18 at 14:24