curl получить удаленный файл и загрузить силу одновременно

Я пытаюсь получить удаленный файл и принудительно загрузить его пользователю одновременно. Я не могу вставить код, код слишком длинный. но функция curl работает, но проблема заключается в том, что она не выставляет ничего, пока не получит удаленный файл, а затем принудительно загрузит его пользователю.

Я использую это, чтобы сказать curl, чтобы вернуть обратный вызов

curl_setopt($ch, CURLOPT_READFUNCTION, 'readCallback'); 

теперь в моей функции readCallback я делаю это:

 function readCallback($curl, $stream, $maxRead){ $read = fgets($stream, $maxRead); echo $read; return $read; } 

но он не возвращает ничего, что просто ждет, пока не будет извлечен удаленный файл.

Related of "curl получить удаленный файл и загрузить силу одновременно"

Попробуйте это, он будет использовать завиток, чтобы получить общий размер файла, а затем загрузить частичные фрагменты файла, проксимирующие его для пользователя, так как нет никакого ожидания завихрения для его загрузки в первую очередь, я тестировал это с помощью avi, mp4, mp3 и exe, надеюсь, что это поможет:

 <?php $file = 'http://example.com/somefile.mp3'; download($file,2000); /* Set Headers Get total size of file Then loop through the total size incrementing a chunck size */ function download($file,$chunks){ set_time_limit(0); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-disposition: attachment; filename='.basename($file)); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Expires: 0'); header('Pragma: public'); $size = get_size($file); header('Content-Length: '.$size); $i = 0; while($i<=$size){ //Output the chunk get_chunk($file,(($i==0)?$i:$i+1),((($i+$chunks)>$size)?$size:$i+$chunks)); $i = ($i+$chunks); } } //Callback function for CURLOPT_WRITEFUNCTION, This is what prints the chunk function chunk($ch, $str) { print($str); return strlen($str); } //Function to get a range of bytes from the remote file function get_chunk($file,$start,$end){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $file); curl_setopt($ch, CURLOPT_RANGE, $start.'-'.$end); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'chunk'); $result = curl_exec($ch); curl_close($ch); } //Get total size of file function get_size($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_NOBODY, true); curl_exec($ch); $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); return intval($size); } ?> в <?php $file = 'http://example.com/somefile.mp3'; download($file,2000); /* Set Headers Get total size of file Then loop through the total size incrementing a chunck size */ function download($file,$chunks){ set_time_limit(0); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-disposition: attachment; filename='.basename($file)); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Expires: 0'); header('Pragma: public'); $size = get_size($file); header('Content-Length: '.$size); $i = 0; while($i<=$size){ //Output the chunk get_chunk($file,(($i==0)?$i:$i+1),((($i+$chunks)>$size)?$size:$i+$chunks)); $i = ($i+$chunks); } } //Callback function for CURLOPT_WRITEFUNCTION, This is what prints the chunk function chunk($ch, $str) { print($str); return strlen($str); } //Function to get a range of bytes from the remote file function get_chunk($file,$start,$end){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $file); curl_setopt($ch, CURLOPT_RANGE, $start.'-'.$end); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'chunk'); $result = curl_exec($ch); curl_close($ch); } //Get total size of file function get_size($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_NOBODY, true); curl_exec($ch); $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); return intval($size); } ?> 

Попробуйте использовать завиток:

 $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_HTTPGET, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $result = curl_exec($ch); $msg = new HttpMessage($result); return $msg->getBody(); 

Тогда возвращаемое значение является содержимым запрашиваемого файла, который вы можете вывести. Поэтому нет необходимости в обратном вызове. Документы HTTPMessage .