Multi_Curl无法正常工作

时间:2014-08-26 07:30:19

标签: php curl

我已经举了一个使用php.net中的multi_curl的例子并遇到了问题。为什么此代码下载页面少于30次(通常为5到20次)?我试图改变超时,但这没有帮助。

$cmh   = curl_multi_init();
$tasks = array();

for ($i = 0; $i < 30; $i++) {

 $ch = curl_init('http://www.bloomberg.com');
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_HEADER, 1);

 $tasks[$i] = $ch;

 curl_multi_add_handle($cmh, $ch);
}

$active = null;
$result = array();

do {
 $mrc = curl_multi_exec($cmh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && ($mrc == CURLM_OK)) {
if (curl_multi_select($cmh) == -1) {
    usleep(100); 
}

do {
    $mrc = curl_multi_exec($cmh, $active);

    $info = curl_multi_info_read($cmh);

    if ($info['msg'] == CURLMSG_DONE) {
        $ch                  = $info['handle'];
        $url                 = array_search($ch, $tasks);
        $result[intval($ch)] = curl_multi_getcontent($ch);

        echo ($result[intval($ch)]) . '<br/>';

        curl_multi_remove_handle($cmh, $ch);
        curl_close($ch);
    }
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

}
curl_multi_close($cmh);

0 个答案:

没有答案