cURL download progress in PHP

后端 未结 3 1912
我在风中等你
我在风中等你 2020-11-27 05:48

I\'m pretty new to cURL so I\'ve been struggling with this one for hours. I\'m trying to download the source of a website in an iframe using cURL and while it\'s loading to

3条回答
  •  渐次进展
    2020-11-27 06:25

    To use the callback inside a class, you must 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:

    private 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);
    }
    

提交回复
热议问题