Skip to content

Commit ef8aad4

Browse files
committed
Merge pull request php-curl-class#149 from zachborboa/master
Add progress callback
2 parents f9d06fb + 6a0645e commit ef8aad4

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"name": "Zach Borboa"
1010
}
1111
],
12-
"version": "3.3.3",
12+
"version": "3.4.3",
1313
"require": {
1414
"php": ">=5.3",
1515
"ext-curl": "*"

examples/progress.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
require '../src/Curl/Curl.php';
3+
4+
use \Curl\Curl;
5+
6+
$curl = new Curl();
7+
$curl->progress(function($client, $download_size, $downloaded, $upload_size, $uploaded) {
8+
if ($download_size === 0) {
9+
return;
10+
}
11+
12+
$percent = floor( $downloaded * 100 / $download_size );
13+
echo ' ' . $percent . '%' . "\r";
14+
});
15+
$curl->download('https://php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz');

examples/progress_advanced.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
require '../src/Curl/Curl.php';
3+
4+
use \Curl\Curl;
5+
6+
$curl = new Curl();
7+
$curl->progress(function($client, $download_size, $downloaded, $upload_size, $uploaded) {
8+
if ($download_size === 0) {
9+
return;
10+
}
11+
12+
// Display a progress bar: xxx% [=======> ]
13+
$percent = (int)floor( $downloaded * 100 / $download_size );
14+
$percentage = sprintf('%3d%%', $percent);
15+
$arrow_length = 40;
16+
$arrow_tail_length = max(1, floor(($percent / 100 * $arrow_length) - 3));
17+
$space_length = max(0, $arrow_length - $arrow_tail_length - 3);
18+
$arrow = '[' . str_repeat('=', $arrow_tail_length) . '>' . str_repeat(' ', $space_length) . ']';
19+
echo ' ' . $percentage . ' ' . $arrow . "\r";
20+
});
21+
$curl->complete(function($instance) {
22+
echo "\n" . 'download complete' . "\n";
23+
});
24+
$curl->download('https://php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz');

src/Curl/Curl.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Curl
66
{
7-
const VERSION = '3.3.3';
7+
const VERSION = '3.4.3';
88
const DEFAULT_TIMEOUT = 30;
99

1010
public $curl;
@@ -140,6 +140,12 @@ public function complete($callback)
140140
$this->complete_function = $callback;
141141
}
142142

143+
public function progress($callback)
144+
{
145+
$this->setOpt(CURLOPT_PROGRESSFUNCTION, $callback);
146+
$this->setOpt(CURLOPT_NOPROGRESS, false);
147+
}
148+
143149
public function delete($url, $data = array())
144150
{
145151
if (is_array($url)) {

0 commit comments

Comments
 (0)