2013-10-17 26 views
4

我正在嘗試製作一個腳本來下載文件的一部分。 只需使用CURL和fread進行測試,我知道流傳輸過程中的CURL比fread慢。 爲什麼?如何加快curl流文件? 我不喜歡使用fread,因爲我需要有限的時間在流媒體過程中使用。流文件時CURL比fread慢?如何加快CURL

這是我的示例代碼。

$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秒。 如果我採取更多步驟來下載更多的文件的一部分。 捲曲會花更慢。

爲什麼捲曲比較慢?它有什麼解決方案?

+0

我已經更新了我的回答如下,它好像你正在比較一個請求與許多不太公平的請求範圍 –

回答

0

嘗試以這樣的方式之前,首先curl_exec設置Keep-Alive頭,實施例300秒:

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

我不是更好。我不認爲這是因爲保持活力的聯繫。 – TomSawyer

1

您可以使用curl_getinfo,看看有什麼正在長。

緩慢可能是由於捲曲庫的DNS查找。 您是否嘗試過使用IP地址而不是域來請求網址?

編輯:或者,你可以設置CURLOPT_DNS_USE_GLOBAL_CACHE真正並設置CURLOPT_DNS_CACHE_TIMEOUT到長的值(例如1小時) - 這是2分鐘默認。

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

+0

不,dns沒關係,這個東西是fopen打開文件一次,而curl,我認爲它很多時候打開文件來獲取文件的一小部分 – TomSawyer

3

它始終是慢,因爲你添加的HTTP調用額外的往返。每個curl_exec都是一個HTTP請求。

2

你的問題是關於有很多部分請求或關於相同請求的增量緩衝讀取。

fopen/fread實現會觸發單個HTTP請求並多次單獨讀取它。

另一方面curl實現觸發許多HTTP請求,每個請求一個請求(see partial range requests)。 所以我們比較蘋果和桔子

是公平則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(); 

更新:我已經重寫我的答案