0

私が使用している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);
    });

ありがとうございました!

4

1 に答える 1