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/examples/progress.php b/examples/progress.php new file mode 100644 index 0000000..5d2b04b --- /dev/null +++ b/examples/progress.php @@ -0,0 +1,15 @@ +progress(function($client, $download_size, $downloaded, $upload_size, $uploaded) { + if ($download_size === 0) { + return; + } + + $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'); diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 4d58b73..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; @@ -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)) {