This time, we bring you curl. Download file shows real-time progress bar (with code), Curl download file shows the real-time progress bar considerations, the following is the actual case, take a look.
Objective
Recently in the command line under the programming, download the file is always a difficult process, if there is a progress bar is much better!!!
The extension package of the previous progress bar, or a good https://github.com/dariuszp/cli-progress-bar (local download)
:
It's pretty good!
What's the use of curl?
With PHP, curl is mainly crawling data, of course, we can use other methods to crawl, such as fsockopen,file_get_contents. But you can only catch those pages that can be accessed directly, if you want to crawl pages with page access control, or to log in after the page is more difficult.
Curl uses
Curl as PHP a very common way to download, here simple to use;
Initialize a curl$ch = Curl_init ();//Set the requested Urlcurl_setopt ($ch, Curlopt_url, $url);//curl_setopt ($ch, Curlopt_header, 0);//No Direct output, but return curl_setopt via Curl_exec ($ch, Curlopt_returntransfer, 1); if (false = = = ($stream = curl_exec ($ch))) {throw new \e Xception (Curl_errno ($ch));} Curl_close ($ch); return $stream;
The above is a very simple example, if a file is very large, then the user will have to wait a long time, then we should add the effect of the progress bar:
Class request{protected $bar;//Whether the download completes protected $downloaded = false; public function construct () {//Initialize a progress bar $this-&G T;bar = new Cliprogressbar (100); $this->bar->display (); $this->bar->setcolortored (); } function Download ($url) {$ch = Curl_init ();//Gets the root path curl_setopt ($ch, Curlopt_url, $url) from the configuration file; curl_setopt ($ch, Curlo Pt_returntransfer, 0); Open progress bar curl_setopt ($ch, curlopt_noprogress, 0); The trigger function of the progress bar is curl_setopt ($ch, curlopt_progressfunction, ' progress '); PS: If the target page jumps, also follow the jump//curl_setopt ($ch, curlopt_followlocation, true); if (false = = = ($stream = curl_exec ($ch))) {throw new \exception (Curl_errno ($ch));} curl_close ($ch); return $stream; }/** * progress bar download. * * @param $ch * @param $countDownloadSize Total downloads * @param $currentDownloadSize current Downloads * @param $countUploadSize * @param $c Urrentuploadsize */Public Function Progress ($ch, $countDownloadSize, $currentDownloadSize, $countUploadSize, $ Currentuploadsize) {//equals 0, should be pre-read resources are not equal to 0 when the start download//Here's every judgment is a pit, more try to know if (0 = = $countDownloadSize) {return false;}//Sometimes downloaded two times, first small, should be redirected download if ($countDownloadSize > $currentDownloadS ize) {$this->downloaded = false; Continue to show progress bar}//has been downloaded to complete will also send three requests elseif ($this->downloaded) {return false;}//Both sides equal download completion does not necessarily end, ElseIf ($currentDownloa Dsize = = = $countDownloadSize) {return false;} Start calculation $bar = $currentDownloadSize/$countDownloadSize * 100; $this->bar->progress ($bar); }} (new Request)->download (' Http://www.shiguopeng.cn/database.sql ');
Please note download callback to judge where the pit!!!
There is also a problem: if the jump download, set up curl also follow the jump, the returned file will be a problem,
I downloaded a zip file that would cause the header of the file to have the contents of the first HTTP response header for this request,
So look at what you needcurl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
PHP dynamic Add XML Data steps in a detailed
Cmspress implementation of infinite class classification (with code)