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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀