Files
php-curl-class/examples/progress_advanced.php
T
Zach Borboa acf8e9f6a0 Include curl error code constant in curl error message (Curl::curlErrorMessage)
Before:
    Timeout was reached: Operation timed out after 30001 milliseconds with 1453867 out of 3000000 bytes received

After:
    Timeout was reached (CURLE_OPERATION_TIMEOUTED): Operation timed out after 30001 milliseconds with 1453867 out of 3000000 bytes received
2022-09-30 07:52:27 -07:00

37 lines
1.1 KiB
PHP

<?php
require __DIR__ . '/../vendor/autoload.php';
// Note: The server needs to send a content-length header for progress to work.
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% [=======> ]
$progress_size = 40;
$fraction_downloaded = $downloaded / $download_size;
$dots = round($fraction_downloaded * $progress_size);
printf('%3.0f%% [', $fraction_downloaded * 100);
$i = 0;
for (; $i < $dots - 1; $i++) {
echo '=';
}
echo '>';
for (; $i < $progress_size - 1; $i++) {
echo ' ';
}
echo ']' . "\r";
});
$curl->complete(function ($instance) {
if ($instance->error) {
echo "\n" . 'Download error: ' . $instance->errorMessage . "\n";
} else {
echo "\n" . 'Download complete' . "\n";
}
});
$curl->download('https://www.php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz');