在我的CURL CURLOPT_PROGRESSFUNCTION回调中,dltotal始终为0

时间:2014-03-14 23:21:22

标签: php curl callback

我通过CURLOPT_PROGRESSFUNCTION选项进行了CURL进程回调,该选项在PHP中成功调用了我的成员函数。 dlnow 变量返回正确的接收值,但 dltotal 始终返回0.我在这里缺少什么?

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但我的回调函数中还有一个不必要的参数。

3 个答案:

答案 0 :(得分:4)

注意:对于早于7.32.0的libcurl

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

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

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

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

使用

检查你的卷曲版本
curl -V

答案 1 :(得分:1)

尝试删除课程:

curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, 'curl_progress_callback');

然后从callback中删除第一个参数。

function curl_progress_callback($dltotal, $dlnow, $ultotal, $ulnow){
   ...
}

更新:正在运行的代码:尝试从最终运行它。

function getFile(){
  $fp = fopen ("output.html", 'w+');
  $curl = curl_init();
  curl_setopt($curl,CURLOPT_URL,'http://www.mashable.com/');
  curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
  curl_setopt($curl, CURLOPT_FILE, $fp);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($curl, CURLOPT_NOPROGRESS, 0);
  curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, 'curl_progress_callback');

  $result = curl_exec($curl);
  fclose($fp);

}

function curl_progress_callback($dltotal, $dlnow, $ultotal, $ulnow){
    echo "$dltotal\n";
    echo "$dlnow\n";
}

getFile();

答案 2 :(得分:0)

问题似乎是我要求的网站没有发送http内容长度,所以dltotal总是空的。感谢@Subuj Hassan在我的curl_progress_callback中纠正了我的错误,这个错误有一个太多的值,让我最初认为问题是dlnow值。