Soap Header with Authorization Basic in native PHP

痴心易碎 提交于 2019-12-01 11:10:28

问题


i need to connect to TeraData SOAP API which now demands a Authorization Basic Header to be sent with the login credentials. I do not know how to adress the problem. I got the access working in SoapUI when adding Basic Authorization Header - please can anyone help me to get the code straight:

Here's the Header SoapUI sends and the response is good:

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]

Here's how i am trying to set up my 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);

it's not accepted and i just get an HTTP/1.1 401 Unauthorized Error

The stacktrace shows me the header that i sent - the Authorization part is missing there.

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

回答1:


man i was so stupid:

$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.




回答2:


That's because you should encode your login credentials this way:

base64_encode("$login:$password")

instead of: base64_encode($login) . ':' . base64_encode($password)



来源:https://stackoverflow.com/questions/35360282/soap-header-with-authorization-basic-in-native-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!