我编写了一个PHP脚本,它获取了一个远程文件并使用cURL将其提供给客户端。
它就像代理......
电话:
<?php
[...]
public function streamContent($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILETIME, true);
if($this->cookie) {
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie);
curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie);
}
if($this->follow) {
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
}
curl_setopt($curl, CURLOPT_HEADERFUNCTION, array($this, "streamHeader"));
curl_setopt($curl, CURLOPT_WRITEFUNCTION, array($this, "myProgressFunc"));
$data = curl_exec($curl);
curl_close($curl);
}
private function streamHeader($curl,$string)
{
$stringTrim = trim($string);
if(!empty ($stringTrim)) {
if(!preg_match('~chunk~i', $stringTrim)) header($stringTrim);
}
return strlen($string);
}
private function myProgressFunc($curl, $str)
{
echo $str;
return strlen($str);
}
[...]
$object->streamContent('http://url_to_distant_file');
?>
现在,我想知道客户端已经下载了多少字节(即使下载已停止,也会直播)
我想我可以在myProgressFunc()
上写一些内容来解决这个问题,但我不知道如何:/
有人知道如何实现这个目标吗?
谢谢!
编辑: 嗯... myProgressFunc()返回strlen(),所以它下载的是大小(以字节为单位),对吧?
答案 0 :(得分:1)
使用curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, "myProgressFunc")
代替WRITEFUNCTION
。见这里:cURL download progress in PHP