CURL download speed is very slow on PHP Apache server

I have a service running on Apache server that use curl php to retrieve web pages. For unknown reasons, I notice that the download speed gets very slow.

But it's not normal, since I issues the request from my virtual host, it should be, like , super duper fast. Because the line is the ISP level bandwidth.

The following code will display the details of the connection and download process.

 
$curl_info = @curl_getinfo($ch);
 
l()->debugtitle ("the dump of curl_getinfo");
l()->debugpre(print_r($curl_info, true));
 
 

The output

 
Array
(
    [url] => https://www.google.com/search?q=curl+download+speed+slow+speed_download&newwindow=1&ei=2txxWN-KMMK00gSa1ZqQDg&start=10&sa=N
    [content_type] => text/html; charset=UTF-8
    [http_code] => 200
    [header_size] => 720
    [request_size] => 941
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 6.521647
    [namelookup_time] => 0.255913
    [connect_time] => 0.270253
    [pretransfer_time] => 0.405911
    [size_upload] => 0
    [size_download] => 299938
    [speed_download] => 45991
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0.484767
    [redirect_time] => 0
)
 

The output shows the total time of download is 6.5s, it's calculated by dividing the size_download with the speed_download. So the download speed is about 45kb per second.

This speed can not load even a medium size web page in reasonable time.

Another example

 
Array
(
    [url] => https://www.google.com/search?num=20&newwindow=1&site=&source=hp&q=in+resonable+time&oq=in+resonable+time&gs_l=hp.3...29657.30128.0.30341.7.7.0.0.0.0.426.426.4-1.1.0....0...1c.1j4.64.hp..6.0.0.0.lz6VZZUN_eQ
    [content_type] => text/html; charset=UTF-8
    [http_code] => 200
    [header_size] => 720
    [request_size] => 1107
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 19.776888
    [namelookup_time] => 13.131767
    [connect_time] => 13.145277
    [pretransfer_time] => 13.276856
    [size_upload] => 0
    [size_download] => 313296
    [speed_download] => 15841
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 13.4731
    [redirect_time] => 0
)
 
 
 

In this example , not only the download speed is slow, but also the DNS lookup time. DNS lookup took 13.1 seconds. The total time gets even longer.

Notice how the speed_download is calculated, size_download / (total_time). It includes the DNS lookup time. The actual transfer speed should be still 45KB like the first example. Or you can calculate by this formula: size_download / (totoal_time - starttransfer_time) .

I'm not sure which one is the problem, the curl or the server network speed, so I use the fread to download a file from remote host for example a youtube video.

Here is the function

 
    function download_to_browser(){
        $url = $this->url;
        $path_parts = pathinfo($url);
 
        $ext = $path_parts['extension'];
        $filename = $path_parts['filename'];
 
        header("Content-type: application/mp4");
 
        l()->debugtitle("downloading url: " . $url);
 
        try
        {
            $handle = fopen($url, "rb");
            if($handle === FALSE) {
                l()->debugtitle("fopen returns false, now exit");
                l()->dumptofile();
                exit;
            }
            perf()->perf_stub("start read from url");
            for(;;) {    
                $strread = fread($handle,400096);
                l()->debugtitle($i . "th read. content : ". "length: " . strlen($strread) );
                echo $strread;
                if($strread == FALSE) break;
            }
            perf()->perf_stub("end read from url");
 
            fclose($handle);
 
        }
        catch(Exception $e)
        {
             trigger_error("file_get_contents_chunked::" . $e->getMessage(),E_USER_NOTICE);
             return false;
        }
 
        l()->dumptofile();
        perf()->perf_dumptofile();
        exit;
    }
 

This is what I get. The file size is 19.4M, The total time used is 536.2s. The average download speed is 37.04KB/s.

And here is how the loop execute

 
Array
(
    [0] => <h2>before send </br></h2>
    [1] => <h2>downloading url: http://r5---sn-i3b7kn7k.googlevideo.com/videoplayback?nh=IgpwcjAxLmhrZzA4KgkxMjcuMC4wLjE&mime=video%2Fmp4&pl=24&ipbits=0&expire=1483989118&id=o-ABZ4J7btHL8AzED4BQgnuw3z79GpuOUoOK-b9NycSYci&mt=1483967475&ip=117.18.8.1&mm=31&mn=sn-i3b7kn7k&lmt=1472451587496230&signature=9541D0B15CDF3D05BE8A5820EC563E434EC4F3C9.ACA472CE2B7223CEAFDF452EF30F02171D098E9F&upn=IH1FmvQSVrM&sparams=dur%2Cei%2Cid%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cnh%2Cpl%2Cratebypass%2Csource%2Cupn%2Cexpire&key=yt6&ratebypass=yes&source=youtube&dur=731.846&mv=u&ei=HoxzWJaRDIqP4gLRyL24BQ&ms=au&itag=22</h2>
    [2] => <h2>th read. content : length: 597</h2>
    [3] => <h2>th read. content : length: 460</h2>
    [4] => <h2>th read. content : length: 460</h2>
    [5] => <h2>th read. content : length: 460</h2>
    [6] => <h2>th read. content : length: 460</h2>
    [7] => <h2>th read. content : length: 460</h2>
    [8] => <h2>th read. content : length: 460</h2>
    ...
    ...
    [18464] => <h2>th read. content : length: 1000</h2>
    [18465] => <h2>th read. content : length: 1000</h2>
    [18466] => <h2>th read. content : length: 1000</h2>
    [18467] => <h2>th read. content : length: 1000</h2>
    [18468] => <h2>th read. content : length: 2000</h2>
    [18469] => <h2>th read. content : length: 2000</h2>
    [18470] => <h2>th read. content : length: 2000</h2>
    [18471] => <h2>th read. content : length: 1000</h2>
    [18472] => <h2>th read. content : length: 1000</h2>
    [18473] => <h2>th read. content : length: 1000</h2>
    [18474] => <h2>th read. content : length: 851</h2>
    [18475] => <h2>th read. content : length: 0</h2>
)
 

The loop executed for 18475 times, it only read thousands or even hundreds of bytes in each loop. Every fread call cost about 30 milliseconds on average.

So, if a call of fread that read 1kb of data and cost 30 milliseconds, is this normal?.

This is another example in another server

 
Array
(
 
    [2] => <h2>th read. content : length: 9361</h2>
    [3] => <h2>th read. content : length: 7604</h2>
    [4] => <h2>th read. content : length: 1436</h2>
    [5] => <h2>th read. content : length: 1436</h2>
    [6] => <h2>th read. content : length: 1436</h2>
    [7] => <h2>th read. content : length: 1436</h2>
    [8] => <h2>th read. content : length: 1436</h2>
    [9] => <h2>th read. content : length: 1436</h2>
    [10] => <h2>th read. content : length: 1436</h2>
    [11] => <h2>th read. content : length: 1436</h2>
    [12] => <h2>th read. content : length: 1436</h2>
    [13] => <h2>th read. content : length: 1436</h2>
    [14] => <h2>th read. content : length: 1436</h2>
    [15] => <h2>th read. content : length: 1436</h2>
    [16] => <h2>th read. content : length: 5744</h2>
    [17] => <h2>th read. content : length: 8192</h2>
    [18] => <h2>th read. content : length: 7604</h2>
    [19] => <h2>th read. content : length: 1436</h2>
    [20] => <h2>th read. content : length: 8192</h2>
    [21] => <h2>th read. content : length: 7604</h2>
    [22] => <h2>th read. content : length: 1436</h2>
    [23] => <h2>th read. content : length: 1436</h2>
    [24] => <h2>th read. content : length: 8192</h2>
    [25] => <h2>th read. content : length: 8192</h2>
    [26] => <h2>th read. content : length: 2284</h2>
    [27] => <h2>th read. content : length: 8192</h2>
    [28] => <h2>th read. content : length: 8192</h2>
    [29] => <h2>th read. content : length: 8192</h2>
    [30] => <h2>th read. content : length: 4144</h2>
    [31] => <h2>th read. content : length: 8192</h2>
    [32] => <h2>th read. content : length: 4732</h2>
    [33] => <h2>th read. content : length: 1436</h2>
    [34] => <h2>th read. content : length: 8192</h2>
    [35] => <h2>th read. content : length: 1860</h2>
    [36] => <h2>th read. content : length: 2872</h2>
    [37] => <h2>th read. content : length: 8192</h2>
    [38] => <h2>th read. content : length: 7604</h2>
    [39] => <h2>th read. content : length: 8192</h2>
    [40] => <h2>th read. content : length: 8192</h2>
    [41] => <h2>th read. content : length: 8192</h2>
    [42] => <h2>th read. content : length: 5580</h2>
    [43] => <h2>th read. content : length: 1436</h2>
    [44] => <h2>th read. content : length: 1436</h2>
    [45] => <h2>th read. content : length: 1436</h2>
    [46] => <h2>th read. content : length: 1436</h2>
    [47] => <h2>th read. content : length: 1436</h2>
    [48] => <h2>th read. content : length: 1436</h2>
    [49] => <h2>th read. content : length: 1436</h2>
    [50] => <h2>th read. content : length: 1436</h2>
    [51] => <h2>th read. content : length: 1436</h2>
    [52] => <h2>th read. content : length: 1436</h2>
    [53] => <h2>th read. content : length: 2872</h2>
    [54] => <h2>th read. content : length: 1436</h2>
    [55] => <h2>th read. content : length: 1436</h2>
    [56] => <h2>th read. content : length: 4308</h2>
    [57] => <h2>th read. content : length: 8192</h2>
    [58] => <h2>th read. content : length: 8192</h2>
    [59] => <h2>th read. content : length: 8192</h2>
    [60] => <h2>th read. content : length: 1272</h2>
    [61] => <h2>th read. content : length: 8192</h2>
    [62] => <h2>th read. content : length: 8192</h2>
    [63] => <h2>th read. content : length: 5156</h2>
    [64] => <h2>th read. content : length: 8192</h2>
    [65] => <h2>th read. content : length: 3296</h2>
    [66] => <h2>th read. content : length: 4308</h2>
    [67] => <h2>th read. content : length: 8192</h2>
    [68] => <h2>th read. content : length: 6168</h2>
    [69] => <h2>th read. content : length: 8192</h2>
    [70] => <h2>th read. content : length: 8192</h2>
    [71] => <h2>th read. content : length: 848</h2>
    [72] => <h2>th read. content : length: 7180</h2>
    [73] => <h2>th read. content : length: 8192</h2>
    [74] => <h2>th read. content : length: 8192</h2>
    [75] => <h2>th read. content : length: 8192</h2>
    [76] => <h2>th read. content : length: 8192</h2>
    [77] => <h2>th read. content : length: 8192</h2>
    [78] => <h2>th read. content : length: 8192</h2>
    [79] => <h2>th read. content : length: 2544</h2>
    [80] => <h2>th read. content : length: 8192</h2>
    [81] => <h2>th read. content : length: 8192</h2>
    [82] => <h2>th read. content : length: 8192</h2>
    [83] => <h2>th read. content : length: 8192</h2>
    [84] => <h2>th read. content : length: 8192</h2>
    [85] => <h2>th read. content : length: 8192</h2>
    [86] => <h2>th read. content : length: 8192</h2>
    [87] => <h2>th read. content : length: 1532</h2>
    [88] => <h2>th read. content : length: 8192</h2>
    [89] => <h2>th read. content : length: 8192</h2>
    [90] => <h2>th read. content : length: 8192</h2>
    [91] => <h2>th read. content : length: 8192</h2>
    [92] => <h2>th read. content : length: 8192</h2>
    [93] => <h2>th read. content : length: 6428</h2>
    [94] => <h2>th read. content : length: 4308</h2>
    [95] => <h2>th read. content : length: 8192</h2>
    [96] => <h2>th read. content : length: 7604</h2>
    [97] => <h2>th read. content : length: 1436</h2>
    [98] => <h2>th read. content : length: 8192</h2>
    [99] => <h2>th read. content : length: 8192</h2>
    [100] => <h2>th read. content : length: 3720</h2>
    [101] => <h2>th read. content : length: 5744</h2>
    [102] => <h2>th read. content : length: 8192</h2>
    [103] => <h2>th read. content : length: 8192</h2>
    [104] => <h2>th read. content : length: 8192</h2>
    [105] => <h2>th read. content : length: 8192</h2>
    [106] => <h2>th read. content : length: 8192</h2>
    [107] => <h2>th read. content : length: 8192</h2>
    [108] => <h2>th read. content : length: 1108</h2>
    [109] => <h2>th read. content : length: 8192</h2>
    [110] => <h2>th read. content : length: 8192</h2>
    [111] => <h2>th read. content : length: 8192</h2>
    [112] => <h2>th read. content : length: 8192</h2>
    [113] => <h2>th read. content : length: 8192</h2>
    [114] => <h2>th read. content : length: 8192</h2>
    [115] => <h2>th read. content : length: 8192</h2>
    [116] => <h2>th read. content : length: 8192</h2>
    [117] => <h2>th read. content : length: 8192</h2>
    [118] => <h2>th read. content : length: 6688</h2>
    [119] => <h2>th read. content : length: 1436</h2>
    [120] => <h2>th read. content : length: 2872</h2>
    [121] => <h2>th read. content : length: 1436</h2>
    [122] => <h2>th read. content : length: 1436</h2>
    [123] => <h2>th read. content : length: 1436</h2>
    [124] => <h2>th read. content : length: 1436</h2>
    [125] => <h2>th read. content : length: 2872</h2>
    [126] => <h2>th read. content : length: 1436</h2>
    [127] => <h2>th read. content : length: 2872</h2>
    [128] => <h2>th read. content : length: 1436</h2>
    [129] => <h2>th read. content : length: 2872</h2>
    [130] => <h2>th read. content : length: 1436</h2>
    [131] => <h2>th read. content : length: 1436</h2>
    [132] => <h2>th read. content : length: 2872</h2>
    [133] => <h2>th read. content : length: 1436</h2>
    [134] => <h2>th read. content : length: 2872</h2>
    [135] => <h2>th read. content : length: 1436</h2>
    [136] => <h2>th read. content : length: 2872</h2>
    [137] => <h2>th read. content : length: 1436</h2>
    [138] => <h2>th read. content : length: 1436</h2>
    [139] => <h2>th read. content : length: 1436</h2>
    [140] => <h2>th read. content : length: 4308</h2>
    [141] => <h2>th read. content : length: 2872</h2>
    [142] => <h2>th read. content : length: 1436</h2>
    [143] => <h2>th read. content : length: 8192</h2>
    [144] => <h2>th read. content : length: 424</h2>
    [145] => <h2>th read. content : length: 2872</h2>
    [146] => <h2>th read. content : length: 8192</h2>
    [147] => <h2>th read. content : length: 8192</h2>
    [148] => <h2>th read. content : length: 8192</h2>
    [149] => <h2>th read. content : length: 8192</h2>
    [150] => <h2>th read. content : length: 8192</h2>
    [151] => <h2>th read. content : length: 8192</h2>
    [152] => <h2>th read. content : length: 8192</h2>
    [153] => <h2>th read. content : length: 8192</h2>
    [154] => <h2>th read. content : length: 1956</h2>
    [155] => <h2>th read. content : length: 8192</h2>
    [156] => <h2>th read. content : length: 424</h2>
    [157] => <h2>th read. content : length: 2872</h2>
    [158] => <h2>th read. content : length: 8192</h2>
    [159] => <h2>th read. content : length: 3296</h2>
    [160] => <h2>th read. content : length: 8192</h2>
    [161] => <h2>th read. content : length: 8192</h2>
    [162] => <h2>th read. content : length: 8192</h2>
    [163] => <h2>th read. content : length: 8192</h2>
    [164] => <h2>th read. content : length: 8192</h2>
    [165] => <h2>th read. content : length: 8192</h2>
    [166] => <h2>th read. content : length: 8192</h2>
    [167] => <h2>th read. content : length: 8192</h2>
    [168] => <h2>th read. content : length: 8192</h2>
    [169] => <h2>th read. content : length: 8192</h2>
    [170] => <h2>th read. content : length: 8192</h2>
    [171] => <h2>th read. content : length: 8192</h2>
    [172] => <h2>th read. content : length: 8192</h2>
    [173] => <h2>th read. content : length: 8192</h2>
    [174] => <h2>th read. content : length: 8192</h2>
    [175] => <h2>th read. content : length: 8192</h2>
    [176] => <h2>th read. content : length: 8192</h2>
    [177] => <h2>th read. content : length: 8192</h2>
    [178] => <h2>th read. content : length: 8192</h2>
    [179] => <h2>th read. content : length: 8056</h2>
    [180] => <h2>th read. content : length: 8192</h2>
    [181] => <h2>th read. content : length: 3296</h2>
    [182] => <h2>th read. content : length: 1436</h2>
    [183] => <h2>th read. content : length: 8192</h2>
    [184] => <h2>th read. content : length: 7604</h2>
    [185] => <h2>th read. content : length: 8192</h2>
    [186] => <h2>th read. content : length: 4732</h2>
    [187] => <h2>th read. content : length: 5744</h2>
    [188] => <h2>th read. content : length: 8192</h2>
    [189] => <h2>th read. content : length: 8192</h2>
    [190] => <h2>th read. content : length: 8192</h2>
    [191] => <h2>th read. content : length: 8192</h2>
    [192] => <h2>th read. content : length: 8192</h2>
    [193] => <h2>th read. content : length: 8192</h2>
    [194] => <h2>th read. content : length: 8192</h2>
    [195] => <h2>th read. content : length: 2968</h2>
    [196] => <h2>th read. content : length: 8192</h2>
    [197] => <h2>th read. content : length: 8192</h2>
    [198] => <h2>th read. content : length: 8028</h2>
    [199] => <h2>th read. content : length: 1436</h2>
    [200] => <h2>th read. content : length: 8192</h2>
    [201] => <h2>th read. content : length: 8192</h2>
    [202] => <h2>th read. content : length: 8192</h2>
    [203] => <h2>th read. content : length: 8192</h2>
    [204] => <h2>th read. content : length: 8192</h2>
    [205] => <h2>th read. content : length: 8192</h2>
    [206] => <h2>th read. content : length: 8192</h2>
    [207] => <h2>th read. content : length: 8192</h2>
    [208] => <h2>th read. content : length: 8192</h2>
    [209] => <h2>th read. content : length: 8192</h2>
    [210] => <h2>th read. content : length: 8192</h2>
    [211] => <h2>th read. content : length: 8192</h2>
    [212] => <h2>th read. content : length: 8192</h2>
    [213] => <h2>th read. content : length: 8192</h2>
    [214] => <h2>th read. content : length: 8192</h2>
    [215] => <h2>th read. content : length: 8192</h2>
    [216] => <h2>th read. content : length: 8192</h2>
    [217] => <h2>th read. content : length: 8192</h2>
    [218] => <h2>th read. content : length: 8192</h2>
    [219] => <h2>th read. content : length: 8192</h2>
    [220] => <h2>th read. content : length: 8192</h2>
    [221] => <h2>th read. content : length: 8192</h2>
    [222] => <h2>th read. content : length: 8192</h2>
    [223] => <h2>th read. content : length: 8192</h2>
    [224] => <h2>th read. content : length: 8192</h2>
    [225] => <h2>th read. content : length: 8192</h2>
    [226] => <h2>th read. content : length: 8192</h2>
    [227] => <h2>th read. content : length: 8192</h2>
    [228] => <h2>th read. content : length: 8192</h2>
    [229] => <h2>th read. content : length: 8192</h2>
    [230] => <h2>th read. content : length: 8192</h2>
    [231] => <h2>th read. content : length: 8192</h2>
    [232] => <h2>th read. content : length: 8192</h2>
    [233] => <h2>th read. content : length: 8192</h2>
    [234] => <h2>th read. content : length: 8192</h2>
    [235] => <h2>th read. content : length: 8192</h2>
    [236] => <h2>th read. content : length: 8192</h2>
    [237] => <h2>th read. content : length: 8192</h2>
    [238] => <h2>th read. content : length: 8192</h2>
    [239] => <h2>th read. content : length: 8192</h2>
    [240] => <h2>th read. content : length: 8192</h2>
    [241] => <h2>th read. content : length: 8192</h2>
    [242] => <h2>th read. content : length: 8192</h2>
    [243] => <h2>th read. content : length: 8192</h2>
    [244] => <h2>th read. content : length: 4296</h2>
    [245] => <h2>th read. content : length: 1436</h2>
    [246] => <h2>th read. content : length: 1436</h2>
    [247] => <h2>th read. content : length: 1436</h2>
    [248] => <h2>th read. content : length: 1436</h2>
    [249] => <h2>th read. content : length: 1436</h2>
    [250] => <h2>th read. content : length: 1436</h2>
    [251] => <h2>th read. content : length: 1436</h2>
    [252] => <h2>th read. content : length: 1436</h2>
    [253] => <h2>th read. content : length: 1436</h2>
    [254] => <h2>th read. content : length: 1436</h2>
    [255] => <h2>th read. content : length: 1436</h2>
    [256] => <h2>th read. content : length: 1436</h2>
    [257] => <h2>th read. content : length: 1436</h2>
    [258] => <h2>th read. content : length: 8192</h2>
    [259] => <h2>th read. content : length: 8192</h2>
    [260] => <h2>th read. content : length: 5156</h2>
    [261] => <h2>th read. content : length: 1436</h2>
    [262] => <h2>th read. content : length: 4308</h2>
    [263] => <h2>th read. content : length: 2872</h2>
    [264] => <h2>th read. content : length: 8192</h2>
    [265] => <h2>th read. content : length: 7604</h2>
    [266] => <h2>th read. content : length: 2872</h2>
    [267] => <h2>th read. content : length: 8192</h2>
    [268] => <h2>th read. content : length: 8192</h2>
    [269] => <h2>th read. content : length: 8192</h2>
    [270] => <h2>th read. content : length: 7016</h2>
    [271] => <h2>th read. content : length: 2872</h2>
    [272] => <h2>th read. content : length: 8192</h2>
    [273] => <h2>th read. content : length: 8192</h2>
    [274] => <h2>th read. content : length: 2284</h2>
    [275] => <h2>th read. content : length: 1436</h2>
    [276] => <h2>th read. content : length: 8192</h2>
    [277] => <h2>th read. content : length: 8192</h2>
    [278] => <h2>th read. content : length: 8028</h2>
    [279] => <h2>th read. content : length: 8192</h2>
    [280] => <h2>th read. content : length: 8192</h2>
    [281] => <h2>th read. content : length: 8192</h2>
    [282] => <h2>th read. content : length: 8192</h2>
    [283] => <h2>th read. content : length: 8192</h2>
    [284] => <h2>th read. content : length: 8192</h2>
    [285] => <h2>th read. content : length: 2544</h2>
    [286] => <h2>th read. content : length: 1436</h2>
    [287] => <h2>th read. content : length: 8192</h2>
    [288] => <h2>th read. content : length: 8192</h2>
    [289] => <h2>th read. content : length: 6592</h2>
    [290] => <h2>th read. content : length: 8192</h2>
    [291] => <h2>th read. content : length: 8192</h2>
    [292] => <h2>th read. content : length: 5156</h2>
    [293] => <h2>th read. content : length: 8192</h2>
    [294] => <h2>th read. content : length: 8192</h2>
    [295] => <h2>th read. content : length: 8192</h2>
    [296] => <h2>th read. content : length: 8192</h2>
    [297] => <h2>th read. content : length: 8192</h2>
    [298] => <h2>th read. content : length: 8192</h2>
    [299] => <h2>th read. content : length: 8192</h2>
    [300] => <h2>th read. content : length: 8192</h2>
    [301] => <h2>th read. content : length: 8192</h2>
    [302] => <h2>th read. content : length: 8192</h2>
    [303] => <h2>th read. content : length: 8192</h2>
    [304] => <h2>th read. content : length: 8192</h2>
    [305] => <h2>th read. content : length: 8192</h2>
    [306] => <h2>th read. content : length: 8192</h2>
    [307] => <h2>th read. content : length: 8192</h2>
    [308] => <h2>th read. content : length: 8192</h2>
    [309] => <h2>th read. content : length: 8192</h2>
    [310] => <h2>th read. content : length: 8192</h2>
    [311] => <h2>th read. content : length: 8192</h2>
    [312] => <h2>th read. content : length: 8192</h2>
    [313] => <h2>th read. content : length: 8192</h2>
    [314] => <h2>th read. content : length: 8192</h2>
    [315] => <h2>th read. content : length: 8192</h2>
    [316] => <h2>th read. content : length: 8192</h2>
    [317] => <h2>th read. content : length: 8192</h2>
    [318] => <h2>th read. content : length: 8192</h2>
    [319] => <h2>th read. content : length: 3844</h2>
    [320] => <h2>th read. content : length: 8192</h2>
    [321] => <h2>th read. content : length: 8192</h2>
    [322] => <h2>th read. content : length: 8192</h2>
    [323] => <h2>th read. content : length: 8192</h2>
    [324] => <h2>th read. content : length: 8192</h2>
    [325] => <h2>th read. content : length: 8192</h2>
    [326] => <h2>th read. content : length: 5416</h2>
    [327] => <h2>th read. content : length: 8192</h2>
    [328] => <h2>th read. content : length: 3296</h2>
    [329] => <h2>th read. content : length: 7180</h2>
    [330] => <h2>th read. content : length: 2872</h2>
    [331] => <h2>th read. content : length: 8192</h2>
    [332] => <h2>th read. content : length: 1860</h2>
    [333] => <h2>th read. content : length: 2872</h2>
    [334] => <h2>th read. content : length: 1436</h2>
    [335] => <h2>th read. content : length: 8192</h2>
    [336] => <h2>th read. content : length: 8192</h2>
    [337] => <h2>th read. content : length: 8192</h2>
    [338] => <h2>th read. content : length: 8192</h2>
    [339] => <h2>th read. content : length: 8192</h2>
    [340] => <h2>th read. content : length: 8192</h2>
    [341] => <h2>th read. content : length: 8192</h2>
    [342] => <h2>th read. content : length: 96</h2>
    [343] => <h2>th read. content : length: 1436</h2>
    [344] => <h2>th read. content : length: 1436</h2>
    [345] => <h2>th read. content : length: 1436</h2>
    [346] => <h2>th read. content : length: 1436</h2>
    [347] => <h2>th read. content : length: 1436</h2>
    [348] => <h2>th read. content : length: 1436</h2>
    [349] => <h2>th read. content : length: 1436</h2>
    [350] => <h2>th read. content : length: 1436</h2>
    [351] => <h2>th read. content : length: 1436</h2>
    [352] => <h2>th read. content : length: 1436</h2>
    [353] => <h2>th read. content : length: 1436</h2>
    [354] => <h2>th read. content : length: 8192</h2>
    [355] => <h2>th read. content : length: 8192</h2>
    [356] => <h2>th read. content : length: 8192</h2>
    [357] => <h2>th read. content : length: 1272</h2>
    [358] => <h2>th read. content : length: 2872</h2>
    [359] => <h2>th read. content : length: 1436</h2>
    [360] => <h2>th read. content : length: 1436</h2>
    [361] => <h2>th read. content : length: 1436</h2>
    [362] => <h2>th read. content : length: 1436</h2>
    [363] => <h2>th read. content : length: 1436</h2>
    [364] => <h2>th read. content : length: 2872</h2>
    [365] => <h2>th read. content : length: 1436</h2>
    [366] => <h2>th read. content : length: 1436</h2>
    [367] => <h2>th read. content : length: 1436</h2>
    [368] => <h2>th read. content : length: 8192</h2>
    [369] => <h2>th read. content : length: 1860</h2>
    [370] => <h2>th read. content : length: 8192</h2>
    [371] => <h2>th read. content : length: 8192</h2>
    [372] => <h2>th read. content : length: 5156</h2>
    [373] => <h2>th read. content : length: 8192</h2>
    [374] => <h2>th read. content : length: 8192</h2>
    [375] => <h2>th read. content : length: 8028</h2>
    [376] => <h2>th read. content : length: 8192</h2>
    [377] => <h2>th read. content : length: 8192</h2>
    [378] => <h2>th read. content : length: 8028</h2>
    [379] => <h2>th read. content : length: 2872</h2>
    [380] => <h2>th read. content : length: 2872</h2>
    [381] => <h2>th read. content : length: 8192</h2>
    [382] => <h2>th read. content : length: 4732</h2>
    [383] => <h2>th read. content : length: 8192</h2>
    [384] => <h2>th read. content : length: 8192</h2>
    [385] => <h2>th read. content : length: 8192</h2>
    [386] => <h2>th read. content : length: 8192</h2>
    [387] => <h2>th read. content : length: 8192</h2>
    [388] => <h2>th read. content : length: 8192</h2>
    [389] => <h2>th read. content : length: 8192</h2>
    [390] => <h2>th read. content : length: 8192</h2>
    [391] => <h2>th read. content : length: 8192</h2>
    [392] => <h2>th read. content : length: 8192</h2>
    [393] => <h2>th read. content : length: 8192</h2>
    [394] => <h2>th read. content : length: 8192</h2>
    [395] => <h2>th read. content : length: 8192</h2>
    [396] => <h2>th read. content : length: 8192</h2>
    [397] => <h2>th read. content : length: 8192</h2>
    [398] => <h2>th read. content : length: 8192</h2>
    [399] => <h2>th read. content : length: 8192</h2>
    [400] => <h2>th read. content : length: 8192</h2>
    [401] => <h2>th read. content : length: 8192</h2>
    [402] => <h2>th read. content : length: 8192</h2>
    [403] => <h2>th read. content : length: 8192</h2>
    [404] => <h2>th read. content : length: 8192</h2>
    [405] => <h2>th read. content : length: 8192</h2>
    [406] => <h2>th read. content : length: 8192</h2>
    [407] => <h2>th read. content : length: 8192</h2>
    [408] => <h2>th read. content : length: 8192</h2>
    [409] => <h2>th read. content : length: 8192</h2>
    [410] => <h2>th read. content : length: 8192</h2>
    [411] => <h2>th read. content : length: 8192</h2>
    [412] => <h2>th read. content : length: 3680</h2>
    [413] => <h2>th read. content : length: 1436</h2>
    [414] => <h2>th read. content : length: 1436</h2>
    [415] => <h2>th read. content : length: 1436</h2>
    [416] => <h2>th read. content : length: 1436</h2>
    [417] => <h2>th read. content : length: 8192</h2>
    [418] => <h2>th read. content : length: 8192</h2>
    [419] => <h2>th read. content : length: 8192</h2>
    [420] => <h2>th read. content : length: 8192</h2>
    [421] => <h2>th read. content : length: 260</h2>
    [422] => <h2>th read. content : length: 1436</h2>
    [423] => <h2>th read. content : length: 8192</h2>
    [424] => <h2>th read. content : length: 4732</h2>
    [425] => <h2>th read. content : length: 5744</h2>
    [426] => <h2>th read. content : length: 1436</h2>
    [427] => <h2>th read. content : length: 7180</h2>
    [428] => <h2>th read. content : length: 1436</h2>
    [429] => <h2>th read. content : length: 8192</h2>
    [430] => <h2>th read. content : length: 8192</h2>
    [431] => <h2>th read. content : length: 8192</h2>
    [432] => <h2>th read. content : length: 8192</h2>
    [433] => <h2>th read. content : length: 8192</h2>
    [434] => <h2>th read. content : length: 8192</h2>
    [435] => <h2>th read. content : length: 8192</h2>
    [436] => <h2>th read. content : length: 8192</h2>
    [437] => <h2>th read. content : length: 8192</h2>
    [438] => <h2>th read. content : length: 8192</h2>
    [439] => <h2>th read. content : length: 8192</h2>
    [440] => <h2>th read. content : length: 8192</h2>
    [441] => <h2>th read. content : length: 780</h2>
    [442] => <h2>th read. content : length: 4308</h2>
    [443] => <h2>th read. content : length: 7814</h2>
    [444] => <h2>th read. content : length: 0</h2>
)
 
 

This example the speed is 137.63KB/s.

The difference is one fread can read several thousands of bytes, or as high as 8192 bytes.

The conclusion here is the average bytes read in one fread call determine the transfer speed.

Another strange thing in the first example is the bytes read in one fread usually is 1000 or 2000, this looks really not so randomized, it looks like controlled by someone or configurations, it's unlikely reflect the network conditions.

Another thing that confuse me is that at the same condition, using fread can achieve 800~900 KB/s(download video from youtube), but if using curl, the speed is only 45KB (retrieve a Google SERP page).

Some website will limit the download speed

Some website has some kind of defensive mechanism, that will limit your download speed if it detected that the abuse.

For example, download youtube viedo from Youtube.com. Google has the open API let you find out the URL of the video.

For the first download, the speed can go up to he capacity of your bandwidth. But after several downloads the speed will be limited at 30~40 kb/s.