2

With below code

    curl_setopt($ch, CURLOPT_NOPROGRESS, 0);
    curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress_upload');

progress_upload function will be triggered many times in a second but how can we set an interval to decrease it?

3
  • Have you tried to slow it down by including a usleep() in the callback function? Note that CURLOPT_PROGRESSFUNCTION is deprecated. Commented Oct 10, 2024 at 15:15
  • How delay can be helpful? I want to run function for example every 5 seconds. Commented Oct 10, 2024 at 16:51
  • I haven't tested this, but I assume that the call back function won't be called again until it has finished. Adding a delay to it will therefore slow it down. Commented Oct 10, 2024 at 17:03

2 Answers 2

2

You can also use a tiny trick in your body function like below

function progress_upload()
{
    
    //contain time of next run
    static $next ;

    $INTERVAL = 5; // unit is second

    $now = time();

    if ( $now > $next ){

      //function body here
      //
      //
      //

      $next = $now + $INTERVAL;

    }

}

Sign up to request clarification or add additional context in comments.

2 Comments

This is also a good idea. I would be happier with this if it said: static $next = 0;. If you replace $next = $now + $INTERVAL; by $next += $INTERVAL; you use $now only once, so you can simply replace it by time(). Simpler is better.
With this i can send message on specific progress percent!
1

I believe you want to set the BUFFERSIZE (in bytes) so that it will download in larger chunks:

curl_setopt($ch, CURLOPT_BUFFERSIZE, 2048);

Note: Someone mentioned that CURLOPT_PROGRESSFUNCTION is deprecated, I can see that how it works changes after 5.5.0 - but nowhere can I see that this feature will be removed.

1 Comment

For de deprication message see: CURLOPT_PROGRESSFUNCTION explained. In the PHP manual see: CURLOPT_XFERINFOFUNCTION. Using the old one is, at the moment, not a disaster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.