PHP 脚本中 cURL 请求的奇怪行为
Weird behaviour of cURL request in PHP script
我在 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 代码片段), 一切都很好,所有 var_dumps 工作,我可以看到 $header 和 $body 以及我期望的所有内容和数据实际存在。
现在说说奇怪的行为。如果我在 任何浏览器 中打开带有上述片段的脚本文件,它只会给我:
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 在这里引起问题的答案感兴趣。
您是否尝试记录 Curl 请求的详细输出?
通常我发现这是了解幕后情况的最佳方式...
也没有那么流行,但这种方法看起来很容易实现,而且更简洁...
我在 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 代码片段), 一切都很好,所有 var_dumps 工作,我可以看到 $header 和 $body 以及我期望的所有内容和数据实际存在。
现在说说奇怪的行为。如果我在 任何浏览器 中打开带有上述片段的脚本文件,它只会给我:
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 在这里引起问题的答案感兴趣。
您是否尝试记录 Curl 请求的详细输出?
通常我发现这是了解幕后情况的最佳方式...
也没有那么流行,但这种方法看起来很容易实现,而且更简洁...