From abe5bee54b184897e71f582230835acd5096e10d Mon Sep 17 00:00:00 2001 From: mohsen Date: Fri, 27 Feb 2015 17:59:07 +0330 Subject: [PATCH 1/4] 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)) { From 55214cddb5cbd2dcb29a95dd270bd6d1476ef66d Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Tue, 3 Mar 2015 00:59:59 +0700 Subject: [PATCH 2/4] Simplify progress example --- examples/progress.php | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/examples/progress.php b/examples/progress.php index 27e6aa3..8fe42d7 100644 --- a/examples/progress.php +++ b/examples/progress.php @@ -3,29 +3,13 @@ require '../src/Curl/Curl.php'; use \Curl\Curl; -$last_progress = 0; -$last_progress_time = 0; - $curl = new Curl(); $curl->progress(function ($client, $download_size, $downloaded, $upload_size, $uploaded){ - - global $last_progress; - global $last_progress_time; - - if($download_size == 0) + 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; - } + echo ' ' . $progress . '%' . "\r"; }); -$curl->download('http://php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz'); +$curl->download('https://php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz'); From 874bc8c7b0fb9dd3623c90411ca06720c995905b Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Tue, 3 Mar 2015 03:12:04 +0700 Subject: [PATCH 3/4] Add additional progress bar example --- examples/progress.php | 6 +++--- examples/progress_advanced.php | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 examples/progress_advanced.php diff --git a/examples/progress.php b/examples/progress.php index 8fe42d7..5d2b04b 100644 --- a/examples/progress.php +++ b/examples/progress.php @@ -4,12 +4,12 @@ require '../src/Curl/Curl.php'; use \Curl\Curl; $curl = new Curl(); -$curl->progress(function ($client, $download_size, $downloaded, $upload_size, $uploaded){ +$curl->progress(function($client, $download_size, $downloaded, $upload_size, $uploaded) { if ($download_size === 0) { return; } - $progress = floor( $downloaded * 100 / $download_size ); - echo ' ' . $progress . '%' . "\r"; + $percent = floor( $downloaded * 100 / $download_size ); + echo ' ' . $percent . '%' . "\r"; }); $curl->download('https://php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz'); diff --git a/examples/progress_advanced.php b/examples/progress_advanced.php new file mode 100644 index 0000000..148d51a --- /dev/null +++ b/examples/progress_advanced.php @@ -0,0 +1,24 @@ +progress(function($client, $download_size, $downloaded, $upload_size, $uploaded) { + if ($download_size === 0) { + return; + } + + // Display a progress bar: xxx% [=======> ] + $percent = (int)floor( $downloaded * 100 / $download_size ); + $percentage = sprintf('%3d%%', $percent); + $arrow_length = 40; + $arrow_tail_length = max(1, floor(($percent / 100 * $arrow_length) - 3)); + $space_length = max(0, $arrow_length - $arrow_tail_length - 3); + $arrow = '[' . str_repeat('=', $arrow_tail_length) . '>' . str_repeat(' ', $space_length) . ']'; + echo ' ' . $percentage . ' ' . $arrow . "\r"; +}); +$curl->complete(function($instance) { + echo "\n" . 'download complete' . "\n"; +}); +$curl->download('https://php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz'); From 6a0645e36f151a22914a5d4dc2eebc474dc4a1a0 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Tue, 3 Mar 2015 03:15:01 +0700 Subject: [PATCH 4/4] Bump minor version --- composer.json | 2 +- src/Curl/Curl.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 5bf263e..ca8668c 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "name": "Zach Borboa" } ], - "version": "3.3.3", + "version": "3.4.3", "require": { "php": ">=5.3", "ext-curl": "*" diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index db839ac..9c79cf2 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -4,7 +4,7 @@ namespace Curl; class Curl { - const VERSION = '3.3.3'; + const VERSION = '3.4.3'; const DEFAULT_TIMEOUT = 30; public $curl;