本文介绍了在我的CURL CURLOPT_PROGRESSFUNCTION回调中,dltotal始终为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CURL进度回调通过CURLOPT_PROGRESSFUNCTION选项工作,它成功地调用我的成员函数在PHP中。 dlnow 变量返回正确的收到值,但 dltotal 始终返回0.我在此处缺少什么?

I've got a CURL progress callback working via the CURLOPT_PROGRESSFUNCTION option which is successfully calling my member function in PHP. The dlnow variable is returning a correct received value, but dltotal always returns 0. What am I missing here?

class MyClass {

    function getFile(){

    ...

      $fp = fopen ($file, 'w+');
      $curl = curl_init();
      curl_setopt($curl,CURLOPT_URL,$signed['signed_url']);
      curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
      curl_setopt($curl, CURLOPT_FILE, $fp); // write curl response to file
      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($curl, CURLOPT_NOPROGRESS, 0);
      curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, array($this, 'curl_progress_callback'));

    }

    function curl_progress_callback($dltotal, $dlnow, $ultotal, $ulnow){
        echo $dltotal; //Reports correct value
        echo $dlnow;   //Always returns 0
    }

}

>编辑:这篇文章已经更新了来自@Sabuj Hassan的信息。最初我认为我收到一个正确的dltotal,但不正确的dlnow,但我有一个额外的不必要的参数在我的回调函数。

推荐答案

注意:对于大于7.32.0的libcurl

NOTE: for libcurl older than 7.32.0

回调函数需要具有以下格式

the callback function need has the following format

curl_progress_callback($resource,$dltotal, $dlnow, $ultotal, $ulnow)


b $ b

其中较新的curl二进制不需要资源

where as newer curl binary does not require the resource

curl_progress_callback($dltotal, $dlnow, $ultotal, $ulnow)

使用

curl -V

这篇关于在我的CURL CURLOPT_PROGRESSFUNCTION回调中,dltotal始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 12:16