【问题标题】:WSDL to PHP with Basic Auth带有基本身份验证的 WSDL 到 PHP
【发布时间】:2012-04-18 23:15:43
【问题描述】:

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

它有大量的命名空间,所以手工操作看起来很麻烦。

我尝试了一些工具,但似乎身份验证会话不存在。

【问题讨论】:

标签: php soap wsdl basic-authentication


【解决方案1】:
$options = array(
     'login' => $username,
     'password' => $password,
);
$client = new SoapClient($wsdl, $options);

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

【讨论】:

  • 这适用于 API 中的身份验证,但由于某种原因不使用身份验证来检索 WSDL。
【解决方案2】:

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

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

【讨论】:

  • Auth 错误的简单解决方案是将 HTTP auth 添加到 soap 流中: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"), ) ] ]);
【解决方案3】:

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

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

【讨论】:

【解决方案4】:

我通过使用库 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);

【讨论】:

    【解决方案5】:

    这个解决方案怎么样:

    1. 下载 WSDL 并保存到本地文件中
    2. 使用本地文件创建 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();
        }
    }
    

    它对我有用:)

    【讨论】:

    • url 中的基本身份验证已在 rfc3986 中删除,因此可能对其他人不起作用,而且很快也不适用于您 ;)
    【解决方案6】:

    这是使用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>
    

    【讨论】:

      【解决方案7】:

      我一直在尝试解决这个问题,但据我了解,soap 客户端连接到 ssl+httpauth 网络服务更痛苦。我已经用谷歌搜索了,据我了解,随着我的问题得到解决,您也可以使用下面的示例来解决您的问题(通过在 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)); 
      

      【讨论】:

        猜你喜欢
        • 2014-07-05
        • 2020-06-08
        • 2018-01-24
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-28
        • 2014-09-23
        相关资源
        最近更新 更多