PHP cURL实时代理(流文件) [英] PHP cURL Realtime proxy (stream file)
本文介绍了PHP cURL实时代理(流文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我有一个如下脚本:
Currently I have a script like the following:
<?php
$filename = "http://someurl.com/file.ext";
header('Content-Type: application/octet-stream');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$filename);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
$data=curl_exec($ch);
curl_close($ch);
echo $data;
?>
问题在于服务器仅在下载整个文件后才发送响应.我想使它像流"一样工作,在下载文件时发送大块数据作为响应.
The problem is that the server just send the response after download the whole file. I want to make it work like a "stream", sending chunks of data as response while the file is downloaded.
用PHP和cURL可以实现吗?
Is that possible to achieve with PHP and cURL?
推荐答案
有可能.您可以使用curl选项CURLOPT_WRITEFUNCTION
指定一个回调,您将在其中接收大块数据,以便在curl下载文件时将其直接发送给客户端.
It's possible. You can use the curl option CURLOPT_WRITEFUNCTION
to specify a callback where you'll receive chunks of data so you can send them directly to the client as curl downloads the file.
<?php
$filename = "http://someurl.com/file.ext";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$filename);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($curl, $data) {
echo $data;
return strlen($data);
});
curl_exec($ch);
curl_close($ch);
这篇关于PHP cURL实时代理(流文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文