20

I need to build php classes from a WSDL that is behind basic auth.

It has tons of namespaces so it looks burdensome to do this by hand.

I have tried a few tools but looks like the auth session isn't presistent.

hakre
  • 193,403
  • 52
  • 435
  • 836
BigPoppa
  • 1,205
  • 2
  • 13
  • 21
  • see this link..... This link may help you .. http://stackoverflow.com/a/38784772/5634447 –  Sep 01 '16 at 07:03

7 Answers7

27
$options = array(
     'login' => $username,
     'password' => $password,
);
$client = new SoapClient($wsdl, $options);

Yes, it works! I tried in a solution that I was building and it connects to my customer WS which is with HTTP Basic Auth.

aemerich
  • 398
  • 3
  • 5
5

HTTP Auth works with SOAP Client, however you cannot access password protected WSDL files

See https://bugs.php.net/bug.php?id=27777

Kinetic
  • 1,714
  • 1
  • 13
  • 38
  • Easy solution for the Auth error is to add HTTP auth to the soap stream: ```stream_context_create([ 'ssl' => [ // set some SSL/TLS specific options 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true, 'header' => array( "Authorization: Basic ".base64_encode("user:pass"), ) ] ]);``` – Feras Jan 15 '19 at 01:30
  • @Feras SoapClient won't send the Authorization header – Brad Kent Sep 14 '22 at 21:49
4

Using built in SOAP client, you should have something like this:

$options = array(
    'login' => $username,
    'password' => $password,
);
$client = new SoapClient($wsdl, $options);
hakre
  • 193,403
  • 52
  • 435
  • 836
nutrija
  • 317
  • 2
  • 8
  • 2
    How is this an answer? According to https://bugs.php.net/bug.php?id=27777 HTTP authentication is not supported for WSDL files that way. – hakre Nov 02 '12 at 13:29
3

I solved this by using the lib nusoap. See if it helps

$params = array(
  "param" => "value"
);

$soap_client = new nusoap_client($wsdl_url, true);
$soap_client->setCredentials(USER_SERVICE, PASS_SERVICE, 'basic');
$soap_client->soap_defencoding = 'UTF-8'; //Fix encode erro, if you need
$soap_return = $soap_client->call("method_name", $params);
Liko
  • 2,130
  • 19
  • 20
2

How about this solution:

  1. Download the WSDL and save into a local file
  2. Create SoapClient with the local file

Something like this (in a simplified version) :

class MySoap {

    private $WSDL = 'https://secure-wsdl.url?wsdl';

    private $USERNAME = 'dummy';
    private $PASSWORD = 'dummy';

    private $soapclient;

    private function localWSDL()
    {
        $local_file_name = 'local.wsdl';
        $local_file_path = 'path/to/file/'.$local_file_name;

        // Cache local file for 1 day
        if (filemtime($local_file_path) < time() - 86400) {

            // Modify URL to format http://[username]:[password]@[wsdl_url]
            $WSDL_URL = preg_replace('/^https:\/\//', "https://{$this->USERNAME}:{$this->PASSWORD}@", $this->WSDL);

            $wsdl_content = file_get_contents($WSDL_URL);
            if ($wsdl_content === FALSE) {

                throw new Exception("Download error");
            }

            if (file_put_contents($local_file_path, $wsdl_content) === false) {

                throw new Exception("Write error");
            }
        }

        return $local_file_path;
    }

    private function getSoap()
    {
        if ( ! $this->soapclient )
        {
            $this->soapclient = new SoapClient($this->localWSDL(), array(
                'login'    => $this->USERNAME,
                'password' => $this->PASSWORD,
            ));
        }

        return $this->soapclient;
    }

    public function callWs() {

        $this->getSoap()->wsMethod();
    }
}

It works for me :)

BCsongor
  • 869
  • 7
  • 11
0

This is Simple example to authenticate webservice using soapClient

$apiauth =array('UserName'=>'abcusername','Password'=>'xyzpassword','UserCode'=>'1991');
$wsdl = 'http://sitename.com/service.asmx?WSDL';
$header = new SoapHeader('http://tempuri.org/', 'AuthHeader', $apiauth);
$soap = new SoapClient($wsdl); 
$soap->__setSoapHeaders($header);       
$data = $soap->methodname($header);        

This code internally parse header as follow

<soap:Header>
    <AuthHeader xmlns="http://tempuri.org/">
      <UserName>abcusername</UserName>
      <Password>xyzpassword</Password>
      <UserCode>1991</UserCode>
    </AuthHeader>
</soap:Header>
Hina Vaja
  • 314
  • 1
  • 5
  • 15
-1

i’ve been trying to resolve this issue, but from what i understand, soap client connections to ssl+httpauth web services are more pain. I’ve googled and from what i understand, with my problem being solved, you can use the example below to solve your problem too(by using HttpAuth infos in both url and soapClient setup).

$username="test";
$password="test";
$url = "https://".urlencode($username).":".urlencode($password)."@example.com/service.asmx?WSDL";

$context = stream_context_create([
'ssl' => [
// set some SSL/TLS specific options
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
]]);

$client = new SoapClient($url, [
'location' => $url,
'uri' => $url,
'stream_context' => $context,
'login' => $username,
'password' => $password
]);

$params=array(
'operation'=>’arguments',
'and’=>'other bull',
'goes’=>'here'
);

$response = $client->__soapCall('OperationName', array($params)); 
Kambaa
  • 467
  • 3
  • 8