Curl Download Progress in PHP

cURL download progress in PHP

What you need is

<?php
ob_start();

echo "<pre>";
echo "Loading ...";

ob_flush();
flush();

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://stackoverflow.com");
//curl_setopt($ch, CURLOPT_BUFFERSIZE,128);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress');
curl_setopt($ch, CURLOPT_NOPROGRESS, false); // needed to make progress function work
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$html = curl_exec($ch);
curl_close($ch);

function progress($resource,$download_size, $downloaded, $upload_size, $uploaded)
{
if($download_size > 0)
echo $downloaded / $download_size * 100;
ob_flush();
flush();
sleep(1); // just to see effect
}

echo "Done";
ob_flush();
flush();

?>

PHP/Curl progress while downloading

change the php str_replace to

str_replace(['#', ' '], '', $b);

Then you get only the percentage, without the preceding blanks, which you can just insert to the container without editing.

Example:
https://3v4l.org/OGWqU

php cURL progress function size limit

CURLOPT_PROGRESSFUNCTION function argument type are double, so you are probably casting these value to int somewhere in your code. and probably since your os is 32bit based architecture you are overflowing the int size limit. I would suggest you revise your code to avoid casting to int big double numbers.

cURL Download Progress in PHP not working?

There doesn't seem to be a CURLOPT_PROGRESSFUNCTION before php 5.3.

Take a look at http://cvs.php.net/viewvc.cgi/php-src/ext/curl/interface.c?view=log and you will find two entries - [DOC] MFH: #41712, implement progress callback. One for the php5.3 and one for the php6 branch.

edit: With php 5.2.x you should be able to set a stream_notification_callback

function foo() {
$args = func_get_args();
echo join(', ', $args), "\n";
}

$ctx = stream_context_create(null, array('notification' =>'foo'));
$fpIn = fopen('http://php.net/', 'rb', false, $ctx);
file_put_contents('localfile.txt', $fpIn);

Curl progress function in PHP

Declare your function progress outside of download:

function download($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress');
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$html = curl_exec($ch);
curl_close($ch);
}

function progress($resource, $download_size, $downloaded, $upload_size, $uploaded)
{
if ($download_size > 0)
$progress = round($downloaded / $download_size * 100);
$progress = array('progress' => $progress);
$path = "temp/";
$destination = $path."11.json";
$file = fopen($destination, "w+");
fwrite($file, json_encode($progress, JSON_UNESCAPED_UNICODE));
fclose($file);
}


Related Topics



Leave a reply



Submit