gpt4 book ai didi

php - php curl 进度可以返回百分比吗?

转载 作者:行者123 更新时间:2023-12-02 04:52:48 25 4
gpt4 key购买 nike

我使用此代码通过 php curl 返回百分比下载数据

<?php 
$url = 'http://exemple.com/';
$path = 'index.html';
$fp = fopen($path, 'w');

$ch=curl_init() or die("ERROR|<b>Error:</b> cURL Error");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_FILE, $fp);

//####################################################//
// This is required to curl give us some progress
// if this is not set to false the progress function never
// gets called
curl_setopt($ch, CURLOPT_NOPROGRESS, false);

// Set up the callback
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'callback');

// Big buffer less progress info/callbacks
// Small buffer more progress info/callbacks
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
//####################################################//

curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

进度回调函数,

function callback($download_size, $downloaded, $upload_size, $uploaded)
{
$percent = $downloaded/$download_size;
// Do something with $percent
echo sprintf('%.2f', $percent*100);
echo '%<br/>';
echo str_repeat(' ', 8192);
flush();
}

我想,我在进度回调函数中犯了错误,我尝试了接收位的方法,但仍然无法使用这里的方法。

bps = time elapsed / size received;
Remaining Time = (Total size - size received) * bps

但我仍然在结果中得到这个:

0.00%
0.00%
0.00%
0.00%
0.00%
0.00%
0.00%
0.00%
0.00%
0.00%
0.00%

为什么这行不通,还有其他方法吗?

最佳答案

function handle() {
$this->db->prep("
update
`links`
set
`status`='toDownload',
`processStartDate` = now()
where
`id` = ?
limit
1
;
")->bind([$this->link->id]);
$ch = curl_init();
$fp = fopen(__DIR__ . "/test.data", "w+");
curl_setopt($ch, CURLOPT_URL, $this->link->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
$t = new DateTime();
$db = $this->db;
$linkId = $this->link->id;
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function ($resource,$download_size, $downloaded, $upload_size, $uploaded) use ($t, $db, $linkId) {
if ($download_size > 0 && ($t < new DateTime())) {
$p = round(($downloaded / $download_size) * 100, 2);
echo "download_size: $download_size, downloaded: $downloaded, p: $p%, t: ".$t->format("Y-m-d H:i:s")."\r\n";
$t->modify("+1 second");
$db->prep("update links set `progress` =? where `id`=? limit 1")->bind([
$p,
$linkId
]);
}
});
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$this->db->prep("update `links` set `progress`='100', `status`='done' where `id` = ? limit 1")->bind([
$this->link->id
]);
}

关于php - php curl 进度可以返回百分比吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26278330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com