php - 带有 ntlm 身份验证的 curl 在命令行中有效,但不在 php 中

标签 php curl ntlm

我必须使用 curl 从 Centos 主机连接到 IIS。

使用 curl --ntlm 选项在命令行上工作正常,但在 php 中无效。服务器是 Microsoft IIS,首先回答 401 Unauthorized,第二遍回答 200 OK。

经过几个小时的调查,我没有找到一个有效的解决方案..

当我在命令行(centos 6.4)上使用 curl 来检索服务的内容时:

curl -v http://example.com/do.asmx --ntlm -u DOMAIN\\username:password

服务器使用以下代码成功应答:
* About to connect() to example.com port 80 (#0)
*   Trying nn.nn.nn.nn... connected
* Connected to example.com (nn.nn.nn.nn) port 80 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* Server auth using NTLM with user 'DOMAIN\username'
> GET /do.asmx HTTP/1.1
> Authorization: NTLM xxxxxxxxxxxxxxxxxx
> User-Agent: curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: example.com
> Accept: */*
> 
< HTTP/1.1 401 Unauthorized
< Content-Type: text/html; charset=us-ascii
< Server: Microsoft-HTTPAPI/2.0
< WWW-Authenticate: NTLM xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
< Date: Thu, 07 Jan 2016 19:28:04 GMT
< Content-Length: 341
< 
* Ignoring the response-body
* Connection #0 to host example.com left intact
* Issue another request to this URL: 'http://example.com/do.asmx'
* Re-using existing connection! (#0) with host example.com
* Connected to example.com (nn.nn.nn.nn) port 80 (#0)
* Server auth using NTLM with user 'DOMAIN\username'
> GET /do.asmx HTTP/1.1
> Authorization: NTLM xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> User-Agent: curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: example.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=utf-8
< Server: Microsoft-IIS/7.5
< X-AspNet-Version: 4.0.30319
< Persistent-Auth: true
< X-Powered-By: ASP.NET
< X-UA-Compatible: IE=EmulateIE8
< Date: Thu, 07 Jan 2016 19:28:04 GMT
< Content-Length: 5340
< 


<html>
...

好的,到目前为止,这工作正常。现在我尝试在 php 中做同样的事情:
$ch = curl_init();

curl_setopt( $ch, CURLOPT_URL, 'http://example.com/do.asmx' );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM );
curl_setopt( $ch, CURLOPT_USERPWD, "DOMAIN\\username:password" );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
curl_setopt( $ch, CURLOPT_VERBOSE, true );


$output = curl_exec($ch);

print_r(curl_getinfo($ch));
echo curl_error($ch);
curl_close($ch);
var_dump($output);

但什么也没有发生。结果是

打印_r(curl_getinfo($ch)):
Array
(
    [url] => http://example.com/do.asmx
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0
    [namelookup_time] => 0.021611
    [connect_time] => 0
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

)

echo curl 错误($ch):
connect() timed out!

var_dump($output) :
bool(false)

这个问题怎么解决?这超出了我的知识范围..

最佳答案

我建议您从 http header 中删除 base64 字符串 Authorization: NTLM TL...因为有可能对其进行解码并读取您的域和凭据,从而使您面临巨大的安全风险。

除非这些 IP 地址仅供内部使用,否则也要删除这些 IP 地址(即使如此,无论如何也要删除它们)。

关于php - 带有 ntlm 身份验证的 curl 在命令行中有效,但不在 php 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34664487/

相关文章:

php - MYSQL , PHP 插入空格

php - 遍历数组并查询数据

ruby-on-rails - 在 ubuntu 14.04 中安装 rvm

php - 我如何获得 LinkedIn 群组成员

asp.net - 如何为 Google Chrome 启用自动登录用户身份验证

Excel VBA : NTLM/Kerberos & Negotiate Authentication in VBA-Web/WinHttp

php - 有没有办法在 php 中获取两个数组之间的公共(public)值?

JavaScript 删除不起作用

php - curl 穿过已经打开的套接字

http-headers - 从 NTLM 身份验证 header 获取用户名?