Soap client, how to call a function via HTTPS with basic HTTP authentication

This problem is killing me all day.

Do I have a https: // * .asmx client web service? WSDL, with basic HTTP authentication ..

If I use SoapUI to connect to a web service, everything works fine, without any errors ..

The problem is when I switch to PHP ...

I managed to connect to it and I can get a list of functions from the server using the soapclient extension for php (I am using PHP 5.3.0).

When I try to call a function using the soapclient extension for PHP, I get the following error:

Fatal error: throw exception SoapFault: [HTTP] Error Retrieving http headers in C: \ www \ xpto_Atestes \ soapclient.php: 26 Stack trace: # 0 [internal function]:> SoapClient โ†’ __ doRequest ('*** ... ',' http: // **** ... ', 1, 0) # 1> C: \ www \ xpto_Atestes \ soapclient.php (26): SoapClient โ†’ __ soapCall (' login ', Array) # 2 {main}> thrown at C: \ www \ xpto_Atestes \ soapclient.php on line 26

A more obvious error (when I do print_r of the call result):

[faultstring] => Error retrieving HTTP headers

[faultcode] => HTTP

When sending the following headers:

POST / * .asmx HTTP / 1.1 Host: * .pt Connection: active user agent:> PHP-SOAP / 5.3.0 Content-Type: text / xml; charset = utf-8 SOAPAction: "http: // ** / Login"> Content-Length: 264 Authorization: Basic AUTHSECETETTE

I call it this way:

$ soapParams = array ('login' => 'HTTP_LOGIN',
'password' => 'HTTP_PASS',
'authentication' => SOAP_AUTHENTICATION_BASIC,
'trace' => 1,
'exceptions' => 0
);
$ client = new SoapClient ("https: // **** .asmx? WSDL", $ soapParams); echo "Connected
"; $ params = array ('account' => '', 'msisdn' => 'NUMBER', 'password' => 'PASSWORD'); $ result = $ client โ†’ __ soapCall (" login ", $ params) ;

After some time googling and trying to figure out what the problem is, I switched to nusoap .., which, unfortunately, also gave an error:

Error

HTTP error: timeout reading header headers

Request

POST / ** .asmx HTTP / 1.0 Host: * .pt User-agent: NuSOAP / 0.9.5 (1.123) Content-Type: text / xml; encoding = ISO-8859-1 SOAPAction: "http: // **** / Login" Authorization: Basic AUTHSECRETPASS Content-Length: 481

NUMBERPASSWORD

answer

I call it this way:

$ client = new nusoap_client ("https: // ** .asmx? WSDL", true); $ Client-> SetCredentials ('HTTPUSER', 'HTTPPASS', 'primary');
$ params = array ('account' => '', 'MSISDN' => 'NUMBER', 'Password' => 'PASSWORD');
$ result = $ client-> call ('Login', $ params);

Using nusoap, I also tried to enable curl extension to make calls:

$ client-> setUseCurl (true);

But there is no such luck, he continued to give an error:

HTTP error: cURL ERROR: 56: Failure when receiving data from the partner url: http: // ********* .asmx
content_type:
http_code: 0
header_size: 0
request_size: 750
filetime: -1
ssl_verify_result: 0
redirect_count: 0
total_time: 21.015
namelookup_time: 0
connect_time: 0
pretransfer_time: 0
size_upload: 462
size_download: 0
speed_download: 0
speed_upload: 21
download_content_length: -1
upload_content_length: -1
starttransfer_time: 0
redirect_time: 0

I am 100% sure that there is nothing wrong with webservice, if I can call it from SoapUI without any problems, I donโ€™t understand why I canโ€™t call it from PHP.

I rally I donโ€™t know what else I can do. I think I tried everything ...

Edit: I just tried another Soap class for PHP, this time from Zend ..

Result:

Fatal error: SoapFault exception: [WSDL] SOAP-ERROR: WSDL analysis: Could not load from 'https: // ** .asmx? WSDL ': Failed to load external object "https: // * .asmx? WSDL" in C: \ www \ xpto_Atestes \ Zend \ Soap \ Client \ Common.php: 51 Stack trace:
# 0 C: \ www \ xpto_Atestes \ Zend \ Soap \ Client \ Common.php (51): SoapClient-> SoapClient ('https: // ** ...', Array)
# 1 C: \ www ** ** _Atestes \ Zend \ Soap \ Client.php (1032): Zend_Soap_Client_Common โ†’ __ construct (Array, 'https: // ** ...', Array)
# 2 C: \ www ** ** _Atestes \ Zend \ Soap \ Client.php (1188): Zend_Soap_Client โ†’ _ initSoapClientObject ()
# 3 C: \ www ** * _Atestes \ Zend \ Soap \ Client.php (1112): Zend_Soap_Client-> getSoapClient () # 4 [internal function]: Zend_Soap_Client โ†’ __ call ('Login', Array)
# 5 C: \ www ** ** _Atestes \ zend.php (28): Zend_Soap_Client-> Input (array)
# 6 {main} thrown at

+4
source share
1 answer

Just to answer myself if someone else wants to know how I solved it.

The problem was this: the XML request for one of the web services should have been in this format:

<?xml version="1.0" encoding="utf-8" ?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <ns123:IsTokenValid xmlns:ns123="http://www.xxxxxxx.com"> <ns123:token xsi:type="xsd:string">'.$this->loginToken.'</ns123:token> </ns123:IsTokenValid> </SOAP-ENV:Body> </SOAP-ENV:Envelope> 

When I mention the prefix, I mean the ns123 part. The problem is that none of the libraries created the correct XML for sending to the server, they never set a prefix, so the request never worked.

At first I solved this by going directly to the NuSoap class and changing the code in one class, but then I found that I could make my own XML request using NuSoap, and as a result, the following code appeared:

 $nsValue = rand(1000, 9000); $requestXml = '<?xml version="1.0" encoding="utf-8" ?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <ns'.$nsValue.':IsTokenValid xmlns:ns'.$nsValue.'="http://www.xxxxxxx.com"> <ns'.$nsValue.':token xsi:type="xsd:string">'.$this->loginToken.'</ns'.$nsValue.':token> </ns'.$nsValue.':IsTokenValid> </SOAP-ENV:Body> </SOAP-ENV:Envelope>'; $result = WebService::CallWebService($this->endPoint, 'http://www.xxxxxxx.com/IsTokenValid', $requestXml, $this->httpUser, $this->httpPass); 

WebService :: CallWebService is just a wrapper for the NuSoap class that does something like this (and also has error handling, sets HTTP authentication, etc.):

 $client = new nusoap_client($webServiceLink, false); $result = $client->send($request, $soapAction, '', self::$timeout, self::$responseTimeout); 

Ian and everyone else, hope this is clear.

+2
source

Source: https://habr.com/ru/post/1339253/


All Articles