cURL下载PHP的进度

时间:2012-12-19 17:59:24

标签: php curl

我对cURL很新,所以我几个小时都在努力解决这个问题。我正在尝试使用cURL在iframe中下载网站的源代码,并在加载时显示已加载了多少内容。到目前为止,我已经成功下载了源代码而没有显示加载进度。你能解释一下如何显示下载进度吗?如果没有cURL,我会逐字节读取文件,并将下载的字节总数除以文件的总大小。如何在cURL中完成这项工作,因为它从整体上读取了源代码? (至少我认为这是唯一的方法,不确定)这是我到目前为止所得到的:

/* Download source */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $adress);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$html = curl_exec($ch);
curl_close($ch); 

3 个答案:

答案 0 :(得分:25)

您需要的是

<?php
ob_start();

echo "<pre>";
echo "Loading ...";

ob_flush();
flush();

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://stackoverflow.com");
//curl_setopt($ch, CURLOPT_BUFFERSIZE,128);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress');
curl_setopt($ch, CURLOPT_NOPROGRESS, false); // needed to make progress function work
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$html = curl_exec($ch);
curl_close($ch);


function progress($resource,$download_size, $downloaded, $upload_size, $uploaded)
{
    if($download_size > 0)
         echo $downloaded / $download_size  * 100;
    ob_flush();
    flush();
    sleep(1); // just to see effect
}

echo "Done";
ob_flush();
flush();

?>

答案 1 :(得分:7)

这就是回调在C中的表现:

typedef int (*curl_progress_callback)(void *clientp,
                                      double dltotal,
                                      double dlnow,
                                      double ultotal,
                                      double ulnow);

可能在PHP中应该看起来像

curl_progress_callback($clientp, $dltotal, $dlnow, $ultotal, $ulnow)

因此,假设您有page.html在iframe中加载.php文件。

在您的php脚本中,您将需要以下功能:

curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, 'curl_progress_callback');    
curl_setopt($curl, CURLOPT_BUFFERSIZE,64000);    
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

应该产生类似于以下的输出:

0
0.1
0.2
0.2
0.3
0.4
...

然后在iframe页面上,您将有一个进度条

<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%;
}

javascript

var progressElement = document.getElementById('progress')

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

您可以输出JavaScript并让它为您更新进度条,例如:

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

您可能会对更多example code

感兴趣

答案 2 :(得分:1)

要在类中使用回调,您必须这样做:

curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array($this, 'progress'));

或者使用静态函数,如下所示:

curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array('self', 'progress'));

...给回调函数做任何你需要的事情:

private static function progress($resource, $downloadSize, $downloaded, $uploadSize, $uploaded)
{
    // emit the progress
    Cache::put('download_status', [
        'resource' => $resource,
        'download_size' => $downloadSize,
        'downloaded' => $downloaded,
        'upload_size' => $uploadSize,
        'uploaded' => $uploaded
    ], 10);
}