I am using the curl option CURLOPT_HEADERFUNCTION
along with a closure to perform some basic data manipulation. As per the php docs, the function / callback must return the length of the header on each call.
$headers = [];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADERFUNCTION, function($curl, $headerLine) use (&$headers) {
$headers[] = $headerLine;
return strlen($headerLine);
});
$response = curl_exec($curl);
curl_close($curl);
return $headers;
I am using references to return the value which works fine. I am just curious if there are other ways to return this value without using references or using a callback?