CURL Download Progress in PHP not working?

I am new to PHP and trying to add a progress bar to an existing PHP script using the following method:

$ch=curl_init() or die("ERROR|<b>Error:</b> cURL Error"); curl_setopt($ch, CURLOPT_URL, $c); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_FILE, $fp); //####################################################// // This is required to curl give us some progress // if this is not set to false the progress function never // gets called curl_setopt($ch, CURLOPT_NOPROGRESS, false); // Set up the callback curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'callback'); // Big buffer less progress info/callbacks // Small buffer more progress info/callbacks curl_setopt($ch, CURLOPT_BUFFERSIZE, 128); //####################################################// curl_exec($ch); curl_close($ch); fclose($fp); 

Callback function:

  function callback($download_size, $downloaded, $upload_size, $uploaded) { $percent=$downloaded/$download_size; // Do something with $percent echo "$percent"; } 

Now I literally copied this example from a PHP site, but this does not work? My PHP version is 5.2.11, Pls. what could be wrong?

Edit: I call this php script from another script.

Info: I am stuck with 5.2.X branch as my web host says cPanel does not yet support 5.3.x branch, any solutions for this

+4
source share
2 answers

It seems that CURLOPT_PROGRESSFUNCTION does not exist 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 php5.3 and one for php6 branch.

edit: With php 5.2.x you can set 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); 
+8
source

As for the last comment, this code requires 5.3 because the second parameter stream_context_create () was added in 5.3. However, replacing this line with the following works in 5.2:

 $ctx = stream_context_create(); stream_context_set_params($ctx, array("notification" => "stream_notification_callback")); 

And in related news in the stream_notification_callback () documentation in the PHP manual there is an example that fully utilizes / creates a progress bar, so check it out.

http://php.net/stream_notification_callback

+2
source

All Articles