Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nikocraft's avatar

how to call CURLOPT_PROGRESSFUNCTION from controller

I am using curl inside Controller to download a file. I would like to display a progress while file is downloading. I've read about that here: http://stackoverflow.com/questions/13958303/curl-download-progress-in-php

these two lines are needed:

curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress');
curl_setopt($ch, CURLOPT_NOPROGRESS, false);

I've placed them inside a function in my controller but I am getting this error: curl_exec(): Cannot call the CURLOPT_PROGRESSFUNCTION

I am suspecting that it's not working because it can't call it like progress() it should call it like $this->progress(). Is that correct? How do I specify this if my assumption is correct, if not what am I doing wrong?

0 likes
2 replies
NXTLevel's avatar

Hey Guys!

got the same problem! PHP sample on webspace works fine, but in Laravel 5.4 can't get it to work!

stwilson's avatar

To use the callback inside a class, you need to do it like this:

curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array($this, 'progress'));

or if using static functions, like this:

curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array('self', 'progress'));

... to a callback function to do whatever you need:

public static function progress($resource, $downloadSize, $downloaded, $uploadSize, $uploaded)
{
    // emit the progress
    Cache::put('download_status', [
        'resource' => $resource,
        'download_size' => $downloadSize,
        'downloaded' => $downloaded,
        'upload_size' => $uploadSize,
        'uploaded' => $uploaded
    ], 10);

}

If you need to pass additional arguments, you can use an anonymous function like so (it's ugly):

curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function ($resource, $downloadSize, $downloaded, $uploadSize, $uploaded) use ($additionalArgument) {
    $this->progress($resource, $downloadSize, $downloaded, $uploadSize, $uploaded, $additionalArgument);
});

Please or to participate in this conversation.