Why is php's file_get_contents slow?

Test code

<?php
function sendGetByCurl($url, $time)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, $time);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $time);
    $return = curl_exec($ch);
    curl_close($ch);
    return $return;
}

$url = "http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIrQKRNquic8GwsU951TC7PDCFzIew3RFwTOFoNx8u1fln0FOzHv04YBoEqXPTHHfyU0Xa1qoFULCw/132";

$start1 = microtime(true);
$data1 = file_get_contents($url);
file_put_contents("1.jpg", $data1);
$end1 = microtime(true);
$span1 = $end1 - $start1;
echo $span1 . PHP_EOL;

$start2 = microtime(true);
$data2 = sendGetByCurl($url, 3);
file_put_contents("2.jpg", $data2);
$end2 = microtime(true);
$span2 = $end2 - $start2;
echo $span2 . PHP_EOL;

exit;

Test results

16.397258043289
0.18160080909729

it"s strange that the picture can be accessed by browser or downloaded by wget very quickly.

Php
Sep.16,2021

this is a bug, of file_get_contents that has been fixed in the latest php version. Search in google, there are very detailed reasons for explanation.


this is really interesting.
this is a similar bug problem mentioned by others:

this is a more reliable statement found:

to sum up, this problem did exist before 5.6.14, but it can be solved by manually adding 'header'= >' Connection: close\ r\ n' . It will not be needed after 5.6.14, but as for why php7 still occurs, it should be Wechat's pot and did not respond correctly to the request.

Menu