带有基本身份验证的 WSDL 到 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10219463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:41:18  来源:igfitidea点击:

WSDL to PHP with Basic Auth

phpsoapwsdlbasic-authentication

提问by BigPoppa

I need to build php classes from a WSDL that is behind basic auth.

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

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.

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

回答by aemerich

$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 基本身份验证连接到我的客户 WS。

回答by Kinetic

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

HTTP 身份验证与 SOAP 客户端一起使用,但是您无法访问受密码保护的 WSDL 文件

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

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

回答by nutrija

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

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

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

回答by Liko

I solved this by using the lib nusoap. See if it helps

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

回答by Briganti

How about this solution:

这个解决方案怎么样:

  1. Download the WSDL and save into a local file
  2. Create SoapClient with the local file
  1. 下载 WSDL 并保存到本地文件
  2. 使用本地文件创建 SoapClient

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

这个对我有用 :)

回答by Hina Vaja

This is Simple example to authenticate webservice using soapClient

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

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>

回答by Kambaa

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

我一直在努力解决这个问题,但据我所知,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));