Merge pull request #149 from zachborboa/master

Add progress callback
This commit is contained in:
Zach Borboa
2015-03-03 03:26:06 +07:00
4 changed files with 47 additions and 2 deletions
+1 -1
View File
@@ -9,7 +9,7 @@
"name": "Zach Borboa"
}
],
"version": "3.3.3",
"version": "3.4.3",
"require": {
"php": ">=5.3",
"ext-curl": "*"
+15
View File
@@ -0,0 +1,15 @@
<?php
require '../src/Curl/Curl.php';
use \Curl\Curl;
$curl = new Curl();
$curl->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');
+24
View File
@@ -0,0 +1,24 @@
<?php
require '../src/Curl/Curl.php';
use \Curl\Curl;
$curl = new Curl();
$curl->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');
+7 -1
View File
@@ -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)) {