【发布时间】:2015-07-06 23:33:14
【问题描述】:
我遇到了我在 PHP 代码中执行的 cURL 请求的奇怪行为。 我在标准 WAMP Apache 服务器上本地运行代码。 代码如下:
$auth_info = "...";
$some_url = "...";
$channel = curl_init();
curl_setopt($channel, CURLOPT_URL, $some_url);
curl_setopt($channel, CURLOPT_HTTPHEADER,
array("Authorization: BASIC " . base64_encode($auth_info))
);
curl_setopt($channel, CURLOPT_RETURNTRANSFER, true);
curl_setopt($channel, CURLOPT_HEADER, true);
$response = curl_exec($channel);
var_dump(curl_getinfo($channel));
$header_size = curl_getinfo($channel, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
var_dump($header);
var_dump($body);
curl_close($channel);
如果我通过我的 CLI(Powershell,因为我在 Windows 上运行)执行这个小的 PHP 代码 sn-p,一切都很好,所有 var_dumps工作,我可以看到 $header 和 $body 以及我期望的所有内容以及实际存在的数据。
现在是奇怪的行为。如果我在任何浏览器中使用上述 sn-p 打开脚本文件,它只会给我:
array (size=26)
'url' => string 'the_url_here' (length=39)
'content_type' => null
'http_code' => int 0
'header_size' => int 0
'request_size' => int 0
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 0
'total_time' => float 1.234
'namelookup_time' => float 0
'connect_time' => float 0.109
'pretransfer_time' => float 0
'size_upload' => float 0
'size_download' => float 0
'speed_download' => float 0
'speed_upload' => float 0
'download_content_length' => float -1
'upload_content_length' => float -1
'starttransfer_time' => float 0
'redirect_time' => float 0
'redirect_url' => string '' (length=0)
'primary_ip' => string 'here_is_the_ip' (length=12)
'certinfo' =>
array (size=0)
empty
'primary_port' => int 443
'local_ip' => string 'here_is_my_ip' (length=13)
'local_port' => int -> my_local_port_here
boolean false
boolean false
我完全感到困惑,因为我看不出 CLI 启动的脚本和浏览器启动的脚本之间有什么区别。有人对此有想法吗?
编辑注意:如果我对请求使用 Guzzle,那么在 CLI 和浏览器中一切正常。我仍然对为什么 cURL 在这里造成问题的答案感兴趣。
【问题讨论】:
-
也许你需要一个正确的 URL 来运行测试?
-
我怀疑您的 CLI 和 WAMP 指向不同的 PHP 安装。使用
php_info()编写一个快速页面,并查看两者的 php 可执行文件在哪里。 -
我在任何 php_info() 输出中都找不到指向 php.exe 的直接路径,但我可以确认,浏览器和 CLI 对“加载的配置文件”有不同的值。可能已经是这样了。
-
@DevanLoper 执行此脚本的脚本/命令是什么?
-
@sitilge 脚本存储在 example.php 文件中,在 Powershell 中它以“php example.php”开头,在浏览器中我使用“localhost/example.php”。这是你想知道的吗?
标签: php apache curl browser command-line-interface