PHP Curl progress bar (callback returning percentage)

独自空忆成欢 提交于 2019-12-10 09:33:10

问题


I have implemented the curl progress bar using

curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, 'callback');

curl_setopt($curl, CURLOPT_BUFFERSIZE,64000);

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

and a callback function.

problem is, the script is outputting the percentage on my html everytime like this :

0
0.1
0.2
0.2
0.3
0.4
..
..
..
1
1.1

How do i combine this with CSS to show a changing progress bar ?


回答1:


Suppose you have a progress bar HTML:

<div id="progress-bar">
    <div id="progress">0%</div>
</div>

CSS:

#progress-bar {
    width: 200px;
    padding: 2px;
    border: 2px solid #aaa;
    background: #fff;
}

#progress {
    background: #000;
    color: #fff;
    overflow: hidden;
    white-space: nowrap;
    padding: 5px 0;
    text-indent: 5px;
    width: 0%;
}

And JavaScript:

var progressElement = document.getElementById('progress')

function updateProgress(percentage) {
    progressElement.style.width = percentage + '%';
    progressElement.innerHTML = percentage + '%';
}

You can have it output JavaScript and have it update the progress bar for you, for example:

<script>updateProgress(0);</script>
<script>updateProgress(0.1);</script>
<script>updateProgress(0.2);</script>
..
..

Note that you can't put each update in separate script block, because the browser will try to read the full script before executing and the progress bar will not work.



来源:https://stackoverflow.com/questions/4705915/php-curl-progress-bar-callback-returning-percentage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!