如何将我正在使用的 php 库中的以下匿名函数转换为与 php 5.2(不支持匿名函数)兼容:
curl_setopt($curl, CURLOPT_WRITEFUNCTION, function($handle, $data) use (&$headers, &$body, &$header_length, $max_data_length) {
$body .= $data;
if ($headers == '') {
$headers_end = strpos($body, "\r\n\r\n");
if ($headers_end !== false) {
$header_length = $headers_end;
$headers = substr($body, 0, $header_length);
$body = substr($body, $header_length + 4);
# Now that we have headers, if the content type is not HTML, we do
# not need to download anything else. Prevents us from downloading
# images, videos, PDFs, etc. that won't contain redirects
# Until PHP 5.4, you can't import $this lexical variable into a closure,
# so we will need to duplicate code from contentTypeFromHeader()
# and hasHTMLContentType()
if (preg_match('/^\s*Content-Type:\s*([^\s;\n]+)/im', $headers, $matches)) {
if (stripos($matches[1], 'html') === false) { return 0; }
}
}
}
# If we have downloaded the maximum amount of content, we're done.
if (($header_length + strlen($body)) > $max_data_length) { return 0; }
return strlen($data);
});
谢谢!