Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

Timeout on cURL connection with NTLM auth using PHP

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...