PHP CURLOPT_WRITEFUNCTION returns headers 在 body
PHP CURLOPT_WRITEFUNCTION returns headers in body
当我包含 curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'curl_write_flush')
时,我将 headers 插入到 $body
中。但是没有 CURLOPT_WRITEFUNCTION
一切正常。我该怎么做才能阻止 headers 被插入到 $body
中?
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://whosebug.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
function curl_write_flush($curl_handle, $chunk) {
echo $chunk;
ob_flush();
flush();
return strlen($chunk);
}
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'curl_write_flush'); // WORKS WITHOUT
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close($ch);
$headers = substr($response, 0, $header_size);
$body = substr($response, $header_size);
die($body);
?>
https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html
If CURLOPT_HEADER is enabled, which makes header data get passed to the write callback, you can get up to CURL_MAX_HTTP_HEADER bytes of header data passed into it. This usually means 100K.
要么删除 curl_setopt($ch, CURLOPT_HEADER, 1);
,要么您必须自己从回调数据中解析出 headers。
您也可以使用 CURLOPT_HEADERFUNCTION
,就像这个答案一样:
当我包含 curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'curl_write_flush')
时,我将 headers 插入到 $body
中。但是没有 CURLOPT_WRITEFUNCTION
一切正常。我该怎么做才能阻止 headers 被插入到 $body
中?
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://whosebug.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
function curl_write_flush($curl_handle, $chunk) {
echo $chunk;
ob_flush();
flush();
return strlen($chunk);
}
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'curl_write_flush'); // WORKS WITHOUT
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close($ch);
$headers = substr($response, 0, $header_size);
$body = substr($response, $header_size);
die($body);
?>
https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html
If CURLOPT_HEADER is enabled, which makes header data get passed to the write callback, you can get up to CURL_MAX_HTTP_HEADER bytes of header data passed into it. This usually means 100K.
要么删除 curl_setopt($ch, CURLOPT_HEADER, 1);
,要么您必须自己从回调数据中解析出 headers。
您也可以使用 CURLOPT_HEADERFUNCTION
,就像这个答案一样: