I am experiencing a problem that seems to be like the one in the following post but unfortunately that post has no real solution.
curl with ntlm authentication works in command line but not inside php
I have two servers: one with Windows Server 2012 R2 that owns resources and the other one with Ubuntu 16.04 that sends requests to the first one to retrieve datas. They are in the same LAN.
So, the connection to the endpoint using NTLM authentication works fine using cURL from terminal with a command like this (NOT all the time):
curl --connect-timeout 60 "http://xyz.abc/endpoint/1" -v --ntlm -u username:password
Requesting the endpoint from my browser (from my local pc, not from the Ubuntu server), the server always responds fine with the requested data without going never in timeout.
Here's, instead, the code that my PHP function uses to retrieve data from the endpoint.
function getResource($url){
try
{
$req = curl_init();
curl_reset($req);
curl_setopt($req, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($req, CURLOPT_UNRESTRICTED_AUTH, true);
curl_setopt($req, CURLOPT_URL, $url);
curl_setopt($req, CURLOPT_USERPWD, WEBSERVICE_USER.':'.WEBSERVICE_PWD);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
curl_setopt($req, CURLOPT_POST, false);
curl_setopt($req, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($req, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($req, CURLOPT_TIMEOUT, 60);
curl_setopt($req, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
//verbose
curl_setopt($req, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'w+');
curl_setopt($req, CURLOPT_STDERR, $verbose);
//till here
$res = curl_exec($req);
//verbose
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
logme('curl_verbose.log',htmlspecialchars($verboseLog));
//till here
if (curl_errno($req)){
throw new Exception (curl_error($req) . "
");
}
curl_close($req);
$json = json_decode($res);
if(!$json)
throw new Exception ("Curl response can not be used.
");
} catch (Exception $e) {
exit(1);
}
return $json;
}
The function sometimes works fine but other ones go in timeout and this makes me foil because I can not understand the reasons.
Does anyone experienced the same problem or could give me some advice?
Any helps are appreciated. Thanks.
question from:
https://stackoverflow.com/questions/65860641/timeout-on-curl-connection-with-ntlm-auth-using-php