WSDL to PHP with Basic Auth

后端 未结 7 650
青春惊慌失措
青春惊慌失措 2020-11-30 10:28

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 to

相关标签:
7条回答
  • 2020-11-30 10:31

    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)); 
    
    0 讨论(0)
  • 2020-11-30 10:38

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

    $options = array(
        'login' => $username,
        'password' => $password,
    );
    $client = new SoapClient($wsdl, $options);
    
    0 讨论(0)
  • 2020-11-30 10:39

    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);
    
    0 讨论(0)
  • 2020-11-30 10:40

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

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

    0 讨论(0)
  • 2020-11-30 10:46
    $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.

    0 讨论(0)
  • 2020-11-30 10:50

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

    0 讨论(0)
提交回复
热议问题