在本机PHP中使用Authorization Basic的Soap Header

时间:2016-02-12 10:30:21

标签: php soap teradata soap-client

我需要连接到TeraData SOAP API,现在需要使用登录凭据发送授权基本标头。我不知道如何解决这个问题。在添加基本授权标题时,我获得了在SoapUI中的访问权限 - 请任何人都可以帮助我直接获取代码:

这里的Header SoapUI发送并且响应良好:

POST http://example.com/api/soap/v6 HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
Content-Length: 302
Host: example.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
Authorization: Basic [user&pass-base64-encoded]

以下是我尝试设置SoapClient的方法:

$namespace =  "http://ecircle.com/developer/ecmapi";
$wsdl = "https://sslh.teradatadmc.com/teradata/api/soap/v6?wsdl";
$client = new SoapClient($wsdl, array("trace" => 1, "exceptions" => 0));
$login = 'xxx';
$password = 'yyy';
$header = new SoapHeader($namespace, 'Authorization: Basic',   
base64_encode("$login:$password"));
$client->__setSoapHeaders($header);

它不被接受,我只是得到HTTP / 1.1 401未经授权的错误

stacktrace向我显示我发送的标题 - 那里缺少授权部分。

public '__last_request_headers' => string 'POST /api/soap/v6 HTTP/1.1
Host: example.com
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.5.12
Content-Type: text/xml; charset=utf-8
SOAPAction: ""
Content-Length: 584

2 个答案:

答案 0 :(得分:2)

我太愚蠢了:

$client = new SoapClient($wsdl, array("trace" => 1, "exceptions" => 0,
                     "login" => $login, "password" => $password) );

For HTTP authentication, the login and password options can be used to supply credentials.

答案 1 :(得分:-1)

那是因为您应该以这种方式对登录凭据进行编码:

base64_encode("$login:$password")

代替: base64_encode($login) . ':' . base64_encode($password)