PHP中的cURL下载进度
我对cURL很陌生,所以我已经为这个问题苦苦挣扎了好几个小时。我正在尝试使用cURL在iframe中下载网站的源代码,并在加载时显示其中已加载了多少。到目前为止,我已经成功下载了源代码,但未显示加载进度。您能解释一下如何显示下载进度吗?如果没有cURL,我将逐字节读取文件,然后将下载的字节总数除以文件的总大小。由于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);
回答:
您需要的是
<?phpob_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();
?>
以上是 PHP中的cURL下载进度 的全部内容, 来源链接: utcz.com/qa/412977.html