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

使用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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配