2012-04-18 105 views
7

我需要從基本身份驗證後面的WSDL構建php類。WSDL to PHP with Basic Auth

它有大量的命名空間,所以看起來很麻煩,需要手工完成。

我已經嘗試了一些工具,但看起來像auth會話不持久。

+0

看到這個鏈接.....這個鏈接可能會幫助你.. http://stackoverflow.com/a/38784772/5634447 – 2016-09-01 07:03:10

回答

1

使用內置的SOAP客戶端,你應該有這樣的事情:

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

這是一個答案?根據https://bugs.php.net/bug.php?id=27777這種方式不支持WSDL文件的HTTP認證。 – hakre 2012-11-02 13:29:04

4

HTTP認證工作與SOAP客戶端,但是不能訪問受密碼保護的WSDL文件

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

+0

有關解決方法,請參見http://www.php.net/manual/soapclient.soapclient.php#101503 – hakre 2012-11-02 13:26:46

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

是的,它的工作原理!我嘗試了一個我正在構建的解決方案,它連接到我的客戶WS,它使用HTTP基本身份驗證。

+1

這適用於API中的身份驗證,但由於某種原因,不使用身份驗證來檢索WSDL。 – 2014-09-11 00:16:22

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

這是一個簡單的例子和​​SoapClient

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

驗證Web服務這個代碼解析內部頭如下

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

我一直在試圖解決這個問題,但從我的理解來看,soap客戶端連接到ssl + httpauth web服務更加痛苦。我google了一下,從我的理解,我的問題得到解決,你可以使用下面的例子來解決你的問題(通過在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)); 
0

這個怎麼樣的解決方案:

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

它適用於我:)