PHP中的cURL下载进度 [英] cURL download progress in PHP

查看:25
本文介绍了PHP中的cURL下载进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

/* 下载源码 */$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);

解决方案

你需要的是

";echo "加载中...";ob_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);//需要使进度功能工作curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);$html = curl_exec($ch);curl_close($ch);函数进度($resource,$download_size,$downloaded,$upload_size,$uploaded){如果($download_size > 0)echo $downloaded/$download_size * 100;ob_flush();冲洗();睡眠(1);//只看效果}echo "完成";ob_flush();冲洗();?>

I'm pretty new to cURL so I've been struggling with this one for hours. I'm trying to download the source of a website in an iframe using cURL and while it's loading to show how much of it is loaded. So far I have successfully downloaded the source without showing the loading progress. Can you explain how to show the download progress? Without cURL I would read the file byte by byte and divide the total amount of downloaded bytes with the total size of the file. How can this be done in cURL since it reads the source as a whole? (at least I think that this is the only way, not sure) Here's what I've got so far:

/* 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); 

解决方案

What you need is

<?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();

?>

这篇关于PHP中的cURL下载进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆