卷曲比流式传输文件时还慢?如何加快卷曲速度


CURL slower than fread while streaming file? how to speed up CURL

我正在尝试制作一个脚本来下载文件的一部分。只需使用 CURL 和 fread 进行测试,我意识到流式传输过程中的 CURL 比 fread 慢。为什么?如何加快流式传输文件的卷曲速度?我不喜欢使用Fread ,Fopen,因为我在流媒体过程中需要有限的时间。

这是我的示例代码。

$start = microtime(true);
$f = fopen('http://news.softpedia.com/images/news2/Debian-Turns-15-2.jpeg','r');
$response = fread($f, 3); echo $response.'<br>';
$response = fread($f, 3); echo $response.'<br>';
$response = fread($f, 3); echo $response.'<br>';
$response = fread($f, 3); echo $response.'<br>';
$response = fread($f, 3); echo $response.'<br>';
$stop = round(microtime(true) - $start, 5);
echo "{$stop}s";
exit();

fread/fopen 只需 1.1s

$start = microtime(true);
$curl = curl_init('http://news.softpedia.com/images/news2/Debian-Turns-15-2.jpeg');
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_RANGE, "0-2");
$response = curl_exec($curl);echo $response.'<br>';
curl_setopt($curl, CURLOPT_RANGE, "3-5");
$response = curl_exec($curl);echo $response.'<br>';
curl_setopt($curl, CURLOPT_RANGE, "6-8");
$response = curl_exec($curl);echo $response.'<br>';
curl_setopt($curl, CURLOPT_RANGE, "9-11");
$response = curl_exec($curl);echo $response.'<br>';
curl_setopt($curl, CURLOPT_RANGE, "12-14");
$response = curl_exec($curl);echo $response.'<br>';
curl_close($curl);
$stop = round(microtime(true) - $start, 5);
echo "{$stop}s";
exit();

卷曲花了大约 2.5 秒。如果我采取更多步骤下载文件的更多部分。卷曲会更慢。

为什么卷曲更慢?它是什么解决方案?

它总是较慢,因为您添加了额外的 HTTP 调用往返。每个curl_exec都是一个 HTTP 请求。

您的问题是关于具有许多部分请求或有关同一请求的增量缓冲读取。

fopen/fread 实现触发单个 HTTP 请求并多次逐段读取它。

另一方面,curl 实现会触发许多 HTTP 请求,每个请求一个请求(请参阅部分范围请求)。所以我们在比较苹果和橙子

公平地说,fopen/fread 看起来像这样

$start = microtime(true);
$f = fopen('http://news.softpedia.com/images/news2/Debian-Turns-15-2.jpeg','r');
$response = fread($f, 3); echo $response.'<br>';
fclose($f)
$f = fopen('http://news.softpedia.com/images/news2/Debian-Turns-15-2.jpeg','r');
fseek($f, 3);
$response = fread($f, 3); echo $response.'<br>';
fclose($f)
$f = fopen('http://news.softpedia.com/images/news2/Debian-Turns-15-2.jpeg','r');
fseek($f, 6);
$response = fread($f, 3); echo $response.'<br>';
fclose($f)
$f = fopen('http://news.softpedia.com/images/news2/Debian-Turns-15-2.jpeg','r');
fseek($f, 9);
$response = fread($f, 3); echo $response.'<br>';
fclose($f)
$f = fopen('http://news.softpedia.com/images/news2/Debian-Turns-15-2.jpeg','r');
fseek($f, 12);
$response = fread($f, 3); echo $response.'<br>';
fclose($f)
$stop = round(microtime(true) - $start, 5);
echo "{$stop}s";
exit();

更新:我已经重写了我的答案

您可以使用curl_getinfo来查看需要多长时间的时间。

速度缓慢可能是由于 curl 库的 DNS 查找造成的。您是否尝试过使用 IP 地址而不是域作为请求 URL?

编辑 :或者,您可以将CURLOPT_DNS_USE_GLOBAL_CACHE设置为 true 并将CURLOPT_DNS_CACHE_TIMEOUT设置为更长的值(例如 1 小时) - 默认情况下为 2 分钟。

来源:http://php.net/manual/en/function.curl-setopt.php

尝试在

第一次curl_exec之前设置Keep-Alive标头,例如 300 秒,如下所示:

$headers = array("Keep-Alive: 300");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

您可以尝试以下解决方案:

curl_setopt($curl, CURLOPT_RANGE, "0-2, 3-5, 6-8, 9-11, 12-14");
$response = curl_exec($curl);echo $response.'<br>';
curl_close($curl);
$stop = round(microtime(true) - $start, 5);
echo "{$stop}s";
exit();