18

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

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

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

4

7 回答 7

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

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

于 2014-03-31T17:52:37.973 回答
5

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

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

于 2012-06-13T09:05:13.590 回答
4

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

$options = array(
    'login' => $username,
    'password' => $password,
);
$client = new SoapClient($wsdl, $options);
于 2012-04-20T06:20:16.583 回答
3

我通过使用 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);
于 2015-02-02T19:40:36.750 回答
2

这个解决方案怎么样:

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

这个对我有用 :)

于 2017-10-19T10:06:49.260 回答
0

这是使用soapClient 对webservice 进行身份验证的简单示例

$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>
于 2016-08-08T06:37:08.190 回答
-1

我一直在尝试解决这个问题,但据我了解,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)); 
于 2017-03-29T21:25:49.250 回答