使用 PHP 与 NTLM 身份验证的 cURL 连接超时

时间:2021-01-23 14:56:27

标签: php curl ntlm ntlm-authentication

我遇到的问题似乎与以下帖子中的问题类似,但不幸的是,该帖子没有真正的解决方案。

curl with ntlm authentication works in command line but not inside php

我有两台服务器:一台装有拥有资源的 Windows Server 2012 R2,另一台装有 Ubuntu 16.04,向第一台发送请求以检索数据。他们在同一个局域网中。

因此,使用 NTLM 身份验证到端点的连接工作正常,使用来自终端的 cURL 和这样的命令(并非总是如此):

curl --connect-timeout 60 "http://xyz.abc/endpoint/1" -v --ntlm -u 用户名:密码

从我的浏览器(从我的本地电脑,而不是从 Ubuntu 服务器)请求端点,服务器总是对请求的数据进行良好响应,而不会超时。

这是我的 PHP 函数用来从端点检索数据的代码。

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) . "\n");
        }

        curl_close($req);
        
        $json = json_decode($res);
        
        if(!$json)
            throw new Exception ("Curl response can not be used. \n");

    } catch (Exception $e) {
        
        exit(1);
    }

    return $json;
}

该功能有时可以正常工作,但其他功能会超时,这让我无法理解原因。

有没有人遇到过同样的问题或者可以给我一些建议?

感谢任何帮助。谢谢。

0 个答案:

没有答案