In some case, we need to read a first part of a website page or a file (eg a big file). How to limit read size?
We can use CURLOPT_WRITEFUNCTION:
Example 1:
<?php set_time_limit(0); $size = 1440; $ch = curl_init(); $content = ''; $callback = function ($ch, $str) { global $content,$size; $content.=$str; if(curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD)>$size) return 0; return strlen($str); }; curl_setopt($ch, CURLOPT_URL, 'https://cs508406.userapi.com/4/u330693590/videos/92b2dc68ca.240.mp4?extra=4Gcx1QRbTb9L-KnybR62GHqd-yXkkermpNo9FXlTXyzjAmIy4riNKfKvI6_CNu3_nqAYCj8Tzlk5bj0bZBh5LsvI8tDi4hqaKDbTcCjJWDlpwa7kDw3NGOZzTsco9IathZzQ-Ds5Z-GI'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10000); curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback); curl_exec($ch); curl_close($ch); var_dump($content);
Example 2: get header and more information
<?php set_time_limit(0); $size = 1440; $headers = array(); $ch = curl_init(); $content = ''; $callback = function ($ch, $str) { global $content,$size; $content.=$str; if(curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD)>$size) return 0; return strlen($str); }; curl_setopt($ch, CURLOPT_URL, 'https://cs508406.userapi.com/4/u330693590/videos/92b2dc68ca.240.mp4?extra=4Gcx1QRbTb9L-KnybR62GHqd-yXkkermpNo9FXlTXyzjAmIy4riNKfKvI6_CNu3_nqAYCj8Tzlk5bj0bZBh5LsvI8tDi4hqaKDbTcCjJWDlpwa7kDw3NGOZzTsco9IathZzQ-Ds5Z-GI'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10000); curl_setopt($ch, CURLOPT_HEADERFUNCTION,'header_callback'); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback); curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); function header_callback($ch, $header_line) { global $headers; $headers[] = $header_line; return strlen($header_line); } var_dump($headers,strlen(implode("",$headers))); var_dump($info);
Result:
array(12) { [0]=> string(17) "HTTP/1.1 200 OK " [1]=> string(16) "Server: Apache " [2]=> string(37) "Date: Thu, 11 May 2017 00:13:43 GMT " [3]=> string(25) "Content-Type: video/mp4 " [4]=> string(26) "Content-Length: 77416932 " [5]=> string(46) "Last-Modified: Sat, 07 Nov 2015 07:18:55 GMT " [6]=> string(24) "Connection: keep-alive " [7]=> string(26) "ETag: "563da5df-49d49e4" " [8]=> string(40) "Expires: Sun, 28 May 2017 00:13:43 GMT " [9]=> string(32) "Cache-Control: max-age=1468800 " [10]=> string(22) "Accept-Ranges: bytes " [11]=> string(2) " " } int(313) array(27) { ["url"]=> string(214) "https://cs508406.userapi.com/4/u330693590/videos/92b2dc68ca.240.mp4?extra=4Gcx1QRbTb9L-KnybR62GHqd-yXkkermpNo9FXlTXyzjAmIy4riNKfKvI6_CNu3_nqAYCj8Tzlk5bj0bZBh5LsvI8tDi4hqaKDbTcCjJWDlpwa7kDw3NGOZzTsco9IathZzQ-Ds5Z-GI" ["content_type"]=> string(9) "video/mp4" ["http_code"]=> int(200) ["header_size"]=> int(313) ["request_size"]=> int(244) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(19) ["redirect_count"]=> int(0) ["total_time"]=> float(1.654) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0.328) ["pretransfer_time"]=> float(0.983) ["size_upload"]=> float(0) ["size_download"]=> float(16071) ["speed_download"]=> float(9716) ["speed_upload"]=> float(0) ["download_content_length"]=> float(77416932) ["upload_content_length"]=> float(0) ["starttransfer_time"]=> float(1.654) ["redirect_time"]=> float(0) ["redirect_url"]=> string(0) "" ["primary_ip"]=> string(13) "87.240.163.60" ["certinfo"]=> array(0) { } ["primary_port"]=> int(443) ["local_ip"]=> string(11) "192.168.1.4" ["local_port"]=> int(51182) ["request_header"]=> string(244) "GET /4/u330693590/videos/92b2dc68ca.240.mp4?extra=4Gcx1QRbTb9L-KnybR62GHqd-yXkkermpNo9FXlTXyzjAmIy4riNKfKvI6_CNu3_nqAYCj8Tzlk5bj0bZBh5LsvI8tDi4hqaKDbTcCjJWDlpwa7kDw3NGOZzTsco9IathZzQ-Ds5Z-GI HTTP/1.1 Host: cs508406.userapi.com Accept: */* " }