2012-04-18 30 views
7

Tôi cần xây dựng các lớp php từ WSDL phía sau auth cơ bản.WSDL to PHP với Basic Auth

Nó có rất nhiều không gian tên nên có vẻ khó chịu để làm điều này bằng tay.

Tôi đã thử một vài công cụ nhưng có vẻ như phiên xác thực không phù hợp.

+0

xem liên kết này ..... Liên kết này có thể giúp bạn .. http://stackoverflow.com/a/38784772/5634447 –

Trả lời

1

Sử dụng được xây dựng trong ứng dụng SOAP, bạn nên có một cái gì đó như thế này:

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

Câu trả lời này như thế nào? Theo https://bugs.php.net/bug.php?id=27777 Xác thực HTTP không được hỗ trợ cho các tệp WSDL theo cách đó. – hakre

4

HTTP Auth làm việc với SOAP Client, tuy nhiên bạn không thể truy cập vào mật khẩu bảo vệ file WSDL

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

+0

Để giải quyết sự cố, hãy xem http://www.php.net/manual/soapclient.soapclient.php#101503 – hakre

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

Có, nó hoạt động! Tôi đã thử trong một giải pháp mà tôi đã xây dựng và nó kết nối với khách hàng của tôi WS, đó là với HTTP Basic Auth.

+1

Điều này hoạt động để xác thực trong API, nhưng không sử dụng xác thực để truy xuất WSDL, vì một lý do nào đó. –

1

Tôi đã giải quyết vấn đề này bằng cách sử dụng lib nusoap. Xem nếu nó giúp

$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

Đây là ví dụ đơn giản để xác thực webservice bằng 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);   

Mã này trong nội bộ phân tích tiêu đề như sau

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

tôi đã cố gắng để giải quyết vấn đề này, nhưng từ những gì tôi hiểu, các kết nối máy khách xà phòng đến các dịch vụ web ssl + httpauth là nhiều đau đớn hơn. Tôi đã googled và từ những gì tôi hiểu, với vấn đề của tôi đang được giải quyết, bạn có thể sử dụng ví dụ dưới đây để giải quyết vấn đề của bạn quá (bằng cách sử dụng HttpAuth infos trong cả hai url và soapClient thiết lập).

$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

Làm thế nào về giải pháp này:

  1. Tải về WSDL và lưu vào một tập tin địa phương
  2. Tạo SoapClient với các tập tin địa phương

Something như thế này (trong một phiên bản đơn giản):

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

Nó hoạt động cho tôi :)