WSDL to PHP with Basic Auth

后端 未结 7 713
青春惊慌失措
青春惊慌失措 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: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 :)

提交回复
热议问题