简体   繁体   中英

WSDL to PHP with Basic Auth

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.

$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.

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

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

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

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

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

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 :)

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM