使用Basic Auth的WSDL到PHP

时间:2012-04-18 23:15:43

标签: php soap wsdl basic-authentication

我需要从基础身份验证后面的WSDL构建php类。

它有大量的命名空间,因此手动执行此操作看起来很麻烦。

我尝试了一些工具,但看起来auth会话不是先发制人。

7 个答案:

答案 0 :(得分:18)

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

是的,它有效!我尝试了一个我正在构建的解决方案,它连接到我的客户WS,它使用HTTP Basic Auth。

答案 1 :(得分:5)

HTTP Auth与SOAP Client一起使用,但是您无法访问受密码保护的WSDL文件

请参阅https://bugs.php.net/bug.php?id=27777

答案 2 :(得分:3)

使用内置的SOAP客户端,你应该有这样的东西:

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

答案 3 :(得分:1)

我通过使用lib nusoap解决了这个问题。看看是否有帮助

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

答案 4 :(得分:1)

这个解决方案怎么样:

  1. 下载WSDL并保存到本地文件
  2. 使用本地文件
  3. 创建SoapClient

    像这样(简化版):

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

    它对我有用:)

答案 5 :(得分:0)

这是使用soapClient验证Web服务的简单示例

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

此代码在内部解析标题,如下所示

<soap:Header>
    <AuthHeader xmlns="http://tempuri.org/">
      <UserName>abcusername</UserName>
      <Password>xyzpassword</Password>
      <UserCode>1991</UserCode>
    </AuthHeader>
</soap:Header>

答案 6 :(得分:0)

我一直在努力解决这个问题,但据我所知,soap客户端与ssl + httpauth Web服务的连接更加痛苦。我用谷歌搜索,从我的理解,我的问题得到解决,你可以使用下面的例子来解决你的问题(通过在url和soapClient设置中使用HttpAuth信息)。

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