【发布时间】:2014-02-16 06:04:15
【问题描述】:
在带有 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
<
[...]
请注意,请求标头中缺少“授权:基本 ...”行。如果我手动设置这样的请求标头,它可以正常工作:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(base64_encode('test:test')));
运行 Mac OS 10.7.5 和 PHP 5.4.11 和 curl 7.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 上,它们都无法发送除非我手动设置授权标头。为什么?
【问题讨论】: