我当前的代码开始异步下载,但是只有完成所有下载后,它才能获取下载文件的内容。我想为每个下载的文件分别运行一个功能,而无需等待其余操作完成。
$data[0] = "http://example.com";
$data[1] = "http://example2.com";
$curly = array();
$result = array();
$mh = curl_multi_init();
foreach ($data as $id => $d) {
$curly[$id] = curl_init();
$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
curl_setopt($curly[$id], CURLOPT_URL, $url);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $headers);
curl_multi_add_handle($mh, $curly[$id]);
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
curl_multi_close($mh);
print_r($result);