WSDL to PHP with Basic Auth


WSDL to PHP with Basic Auth

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

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

我已经尝试了一些工具,但看起来身份验证会话并不抗拒。

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

是的,它有效!我尝试了我正在构建的解决方案,它连接到我的客户WS,即HTTP基本身份验证。

HTTP Auth 适用于 SOAP 客户端,但无法访问受密码保护的 WSDL 文件

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

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

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

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

这个解决方案怎么样:

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

它对我有用:)

这是使用 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>
我一直在

尝试解决这个问题,但据我了解,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));