cli-curl成功;php libcurl调用失败

cli-curl成功;php libcurl调用失败,php,macos,curl,Php,Macos,Curl,在带有PHP 5.4.17和curl 7.30.0的Mac OS 10.9.1上,此curl请求在命令行中运行良好: curl -u test:test http://localhost/protected/ 但是这个使用curl库的PHP脚本失败了: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://localhost/protected/'); curl_setopt($ch, CURLOPT_HTTPAUTH, 'CURLA

在带有PHP 5.4.17和curl 7.30.0的Mac OS 10.9.1上,此curl请求在命令行中运行良好:

curl -u test:test http://localhost/protected/
但是这个使用curl库的PHP脚本失败了:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/protected/');
curl_setopt($ch, CURLOPT_HTTPAUTH, 'CURLAUTH_BASIC');
curl_setopt($ch, CURLOPT_USERPWD, 'test:test');
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
echo curl_exec($ch);
curl_close($ch);
输出为:

$ php -e ./test.php 
* Adding handle: conn: 0x7fe1b303de00
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fe1b303de00) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 80 (#0)
*   Trying ::1...
* Connected to localhost (::1) port 80 (#0)
> GET /protected/ HTTP/1.1
Host: localhost
Accept: */*

< HTTP/1.1 401 Authorization Required
< Date: Sat, 25 Jan 2014 03:12:40 GMT
* Server Apache/2.2.24 (Unix) DAV/2 PHP/5.4.17 mod_ssl/2.2.24 OpenSSL/0.9.8y is not blacklisted
< Server: Apache/2.2.24 (Unix) DAV/2 PHP/5.4.17 mod_ssl/2.2.24 OpenSSL/0.9.8y
< WWW-Authenticate: Basic realm="Restricted Files"
< Content-Length: 401
< Content-Type: text/html; charset=iso-8859-1
<
[...]
一个运行Mac OS 10.7.5、PHP5.4.11和Curl7.21.4的旧系统正确地发送授权头。我尝试了PHP(5.4.11、5.4.17、5.4.24、5.5.8)和curl(7.30.0、7.30.4)的多种不同组合,但在Mac OS 10.9.1上,它们都无法发送授权头,除非我手动设置。为什么?

这是错误的:

curl_setopt($ch, CURLOPT_HTTPAUTH, 'CURLAUTH_BASIC');
                                   ^--            ^--
使用引号,您试图将字符串设置为选项。但是CURL使用
define()
'd常量,这些常量没有被引用

试一试


所以你使用的是实际的旋度常数,而不是一些看起来像常数的随机字符串。

你完全正确;CURLAUTH_BASIC应该是常量,而不是字符串。但现在我想知道引用的版本是如何工作的。它在一个图书馆里,我已经每天用了5年了。
curl_setopt($ch, CURLOPT_HTTPAUTH, 'CURLAUTH_BASIC');
                                   ^--            ^--
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);