Search code examples
phpphp-curl

Make interval for PROGRESSFUNCTION in PHP CURL?


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?


Solution

  • 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;
    
        }
    
    }