Jamie Teuma
Jamie Teuma

Reputation: 346

PHP SoapClient with BasicAuth

I have a PHP script trying to connect to a WSDL. I need to allow self signed AND give basic auth details. Using SOAP UI, when I connect to the WSDL I am prompted for username / password. I got this working.

I also found out that each request also requires basic auth (so on the request screen, I have to select Auth, then basic, enter same credentials as I used on the prompt).

How to I do this auth in PHP As I said, I can connect, not a problem, I seem to kill the service or timeout if I try to make a request

<?php
$context = stream_context_create(array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
 ));

$data = array(
    'columnA' => 'dataA',
    'columnB' => 'dataB',
    'columnC' => 'dataC');

$url = 'https://111.111.111.111:1234/dir/file';
$login = 'username';
$pwd = 'password';


$client = new soapClient(null, array(
     'location' => $url,
     'uri' => '',
     'login' => $login,
     'password' => $pwd,
     'stream_context' => $context
));
echo "\n\r---connected---\n\r";
$result = $client ->requestName($data);

print_r($result);
?>

My output is ---connected---

Then it seems to hang. I have tried wrapping it round a try catch and I had the same result.

Any suggestions??

Upvotes: 1

Views: 1058

Answers (2)

LF-DevJourney
LF-DevJourney

Reputation: 28529

From the Manual soapclient support the http basic auth.

For HTTP authentication, the login and password options can be used to supply credentials. For making an HTTP connection through a proxy server, the options proxy_host, proxy_port, proxy_login and proxy_password are also available. For HTTPS client certificate authentication use local_cert and passphrase options. An authentication may be supplied in the authentication option. The authentication method may be either SOAP_AUTHENTICATION_BASIC (default) or SOAP_AUTHENTICATION_DIGEST.

$wsdl = "http://example/services/Service?wsdl";
$option = array(
    "trace"=>1,
    "login"=>"admin",
    "password"=>"admin",
);
$client = new SoapClient($wsdl,$option);

But when I initiate the soapclient, it will throw this error

Exception: Unauthorized

I also have tried to put the auth in the url, like

$wsdl = "http://admin:admin@example/services/Service?wsdl";

But it also doesn't works.

Finally I solved it by add authentication to the option. The manual says the authentication default value is the basic auth, but only when I explicitly set it, it can work.

$option = array(
    "trace"=>1,
    "login"=>"admin",
    "password"=>"admin",
    "authentication"=>SOAP_AUTHENTICATION_BASIC
);

Upvotes: 1

Mor&#242;Switie
Mor&#242;Switie

Reputation: 1516

Try url encoding your username and password inside the url that you are using:

$url = 'http://'.urlencode('yourLogin').':'.urlencode('yourPassword').'@111.111.111.111:1234/dir/file';

Also I don't see you make use of the wsdl in your code example. You can always download a copy of the wsdl locally and then reference that local copy. You can download the wsdl anyway you want (with php, curl, manually).

Upvotes: 0

Related Questions