dougui1977 2015-09-16 20:48
浏览 791
已采纳

使用CURL multi时报警[curl_multi_remove_handle()]

I am using the following php function to download bulk remote images in parallel. it is fast enough to fetch remote images to my server.

function save($urls){
    $save_to=__DIR__.'/thumb/';

    $conn = array();
    $fp = array();
    $mh = curl_multi_init();
    foreach ($urls as $i => $url) {
        $g=$save_to.basename($url);
      if(!is_file($g)){
            $conn[$i]=curl_init($url);
            $fp[$i]=fopen ($g, "wb");
            curl_setopt ($conn[$i], CURLOPT_FILE, $fp[$i]);
            curl_setopt ($conn[$i], CURLOPT_HEADER ,0);
            curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,60);
            curl_setopt ($conn[$i], CURLOPT_MAXCONNECTS, 10);
            curl_multi_add_handle ($mh,$conn[$i]);
        }
    }
   $active = null;
    do {
        curl_multi_exec($mh,$active);
    }
    while ($active > 0);
    foreach ($urls as $i => $url) {
        curl_multi_remove_handle($mh,$conn[$i]);
        curl_close($conn[$i]);
        fclose ($fp[$i]);
    }
    curl_multi_close($mh);
}

the problem here is that i am getting the following Warnings and notices:

Notice: Undefined offset: 0 in C:\wamp\www\test\images.php on line 103

Warning: curl_multi_remove_handle() expects parameter 2 to be resource, null given in C:\wamp\www\test\images.php on line 103

Notice: Undefined offset: 0 in C:\wamp\www\test\images.php on line 104

Warning: curl_close() expects parameter 1 to be resource, null given in C:\wamp\www\test\images.php on line 104

Notice: Undefined offset: 0 in C:\wamp\www\test\images.php on line 105

those alarms are appearing for each image fetch.

any idea on how to eliminate such alarm causes?

Thanks you

EDIT: i have followed lafor suggestion and changed the last foreach to:

foreach ($conn  as $i => $url) {
... 
}

the number of alarms got decreased but still getting the same alarms at the following two lines:

curl_multi_remove_handle($mh,$conn[$i]);
curl_close($conn[$i]);
  • 写回答

1条回答 默认 最新

  • dongpigui8898 2015-09-16 21:21
    关注

    In that last foreach you seem to assume that both $urls and $conn arrays have same indexes, which is not always the case, because $conn[$i] is only set if file for the $i-th URL doesn't already exist. Iterate over $conn instead of $urls.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究