PHP cURL CURLAUTH_NEGOTIATE与POST请求有关的问题

时间:2019-06-13 12:10:26

标签: php iis php-curl

使用CURLAUTH_NEGOTIATE身份验证时,我在PHP cURL中遇到了一个非常奇怪的错误。当我发送GET请求时,一切正常。但是在将请求类型设置为POST之后,服务器会返回401响应代码。

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/example/path");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$fields = array(
   'data1' => 'value1',
   'data2' => 'value2',
);

// ----- Removing this two lines below makes everything work fine -----
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
// --------------------------------------------------------------------

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NEGOTIATE);
curl_setopt($ch, CURLOPT_USERPWD, 'domain\user:password');

$output = curl_exec($ch);
curl_close($ch);

// With POST request response contains standard IIS unauthorized error
// (with 401 response code). With GET request everything works fine 
// and response contains expected results returned from PHP script
var_dump(htmlentities($output));

服务器上的PHP版本是7.3.6。 cURL的配置如下:

cURL support => enabled
cURL Information => 7.64.0
Age => 4

AsynchDNS => Yes
CharConv => No
Debug => No
GSS-Negotiate => No
IDN => Yes
IPv6 => Yes
krb4 => No
Largefile => Yes
libz => Yes
NTLM => Yes
NTLMWB => No
SPNEGO => Yes
SSL => Yes
SSPI => Yes
TLS-SRP => No
HTTP2 => Yes
GSSAPI => No
KERBEROS5 => Yes
UNIX_SOCKETS => No
PSL => No
HTTPS_PROXY => Yes
MULTI_SSL => No
BROTLI => No
Protocols => dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, ldaps, pop3, pop3s, rtsp, scp, sftp, smb, smbs, smtp, smtps, telnet, tftp
Host => x86_64-pc-win32
SSL Version => OpenSSL/1.1.1b
ZLib Version => 1.2.11
libSSH Version => libssh2/1.8.2

目标服务器可直接使用而无需任何代理。

有人遇到过类似的问题吗?这是PHP中的错误,还是我做错了?预先感谢您的任何答复。

0 个答案:

没有答案