如何将我正在使用的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);
});
谢谢!
您需要将匿名函数转换为命名函数,并在curl_setopt
调用中引用它,大致如下:
function curl_writefunction_callback($handle, $data) {
global $headers;
global $body;
global $header_length;
global $max_data_length;
# [function body here]
};
curl_setopt($curl, CURL_WRITEFUNCTION, "curl_writefunction_callback");