From abe5bee54b184897e71f582230835acd5096e10d Mon Sep 17 00:00:00 2001 From: mohsen Date: Fri, 27 Feb 2015 17:59:07 +0330 Subject: [PATCH] Add progress callback --- examples/progress.php | 31 +++++++++++++++++++++++++++++++ src/Curl/Curl.php | 6 ++++++ 2 files changed, 37 insertions(+) create mode 100644 examples/progress.php diff --git a/examples/progress.php b/examples/progress.php new file mode 100644 index 0000000..27e6aa3 --- /dev/null +++ b/examples/progress.php @@ -0,0 +1,31 @@ +progress(function ($client, $download_size, $downloaded, $upload_size, $uploaded){ + + global $last_progress; + global $last_progress_time; + + if($download_size == 0) + return; + + $now = time(); + + $progress = floor( $downloaded * 100 / $download_size ); + + // print progress every one second + if(($now - $last_progress_time) > 1 && $last_progress != $progress) + { + echo $progress . "%\n"; + + $last_progress = $progress; + $last_progress_time = $now; + } +}); +$curl->download('http://php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz'); diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 4d58b73..db839ac 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -140,6 +140,12 @@ class Curl $this->complete_function = $callback; } + public function progress($callback) + { + $this->setOpt(CURLOPT_PROGRESSFUNCTION, $callback); + $this->setOpt(CURLOPT_NOPROGRESS, false); + } + public function delete($url, $data = array()) { if (is_array($url)) {