Home  >  Article  >  Backend Development  >  Curl download file displays real-time progress bar (with code)

Curl download file displays real-time progress bar (with code)

php中世界最好的语言
php中世界最好的语言Original
2018-05-17 13:51:125956browse

This time I will bring you a real-time progress bar display for Curl download files (with code). What are the precautions for Curl download files to display a real-time progress bar? The following is a practical case, let's take a look.

Preface

Recently, I have been tinkering with programming under the command line. Downloading files is always an arduous process. It would be much better if there was a progress bar. Got it! ! !

Let’s start with a progress bar expansion package, which is pretty goodhttps://github.com/dariuszp/cli-progress-bar (local download)

Rendering:

It’s still pretty good-looking!

What is the use of curl?

Use php, curl mainly captures data, of course we can use other methods to capture, For example, fsockopen, file_get_contents, etc. But it can only capture those pages that can be directly accessed. If you want to capture pages with page Access Control, or pages after logging in, it will be more difficult.

curl uses

curl as a very common download method for PHP, here is a simple way to use it;

// 初始化一个 curl
$ch = curl_init();
// 设置请求的 url
curl_setopt($ch, CURLOPT_URL, $url);
// 
curl_setopt($ch, CURLOPT_HEADER, 0);
// 不直接输出,而是通过 curl_exec 返回
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (false === ($stream = curl_exec($ch))) {
 throw new \Exception(curl_errno($ch));
}
curl_close($ch);
return $stream;

The above is a very simple example. If a file is large, the user will need to wait for a long time. At this time, we should add the effect of the progress bar:

class Request
{
 protected $bar;
 // 是否下载完成
 protected $downloaded = false;
 public function construct()
 {
 // 初始化一个进度条
 $this->bar = new CliProgressBar(100);
 $this->bar->display();
 $this->bar->setColorToRed();
 }
 
 function download($url)
 {
 $ch = curl_init();
 // 从配置文件中获取根路径
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
 // 开启进度条
 curl_setopt($ch, CURLOPT_NOPROGRESS, 0);
 // 进度条的触发函数
 curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress');
 // ps: 如果目标网页跳转,也跟着跳转
 // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 if (false === ($stream = curl_exec($ch))) {
  throw new \Exception(curl_errno($ch));
 }
 curl_close($ch);
 return $stream;
 }
 
 /**
 * 进度条下载.
 *
 * @param $ch
 * @param $countDownloadSize 总下载量
 * @param $currentDownloadSize 当前下载量
 * @param $countUploadSize 
 * @param $currentUploadSize
 */
 public function progress($ch, $countDownloadSize, $currentDownloadSize, $countUploadSize, $currentUploadSize)
 {
  // 等于 0 的时候,应该是预读资源不等于0的时候即开始下载
  // 这里的每一个判断都是坑,多试试就知道了
 if (0 === $countDownloadSize) {
  return false;
 }
 // 有时候会下载两次,第一次很小,应该是重定向下载
 if ($countDownloadSize > $currentDownloadSize) {
  $this->downloaded = false;
  // 继续显示进度条
 }
 // 已经下载完成还会再发三次请求
 elseif ($this->downloaded) {
  return false;
 }
 // 两边相等下载完成并不一定结束,
 elseif ($currentDownloadSize === $countDownloadSize) {
  return false;
 }
 
 // 开始计算
 $bar = $currentDownloadSize / $countDownloadSize * 100;
 $this->bar->progress($bar);
 }
}
(new Request)->download('http://www.shiguopeng.cn/database.sql');

Be sure to pay attention to the download callback There is a pitfall in judgment! ! !

There is another problem: if you jump to download and set curl to jump, the returned file will have problems.

I downloaded a zip file, which will cause the file header There is the content of the HTTP response header of the first request,

So you need to see what you needcurl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

I believe you have read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps to dynamically add XML data with PHP

CMSPRESS implements unlimited classification (with code)

The above is the detailed content of Curl download file displays real-time progress bar (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn