flush(); $dirname = "{$name}\\{$title}"; exec("mkdir \"{$dirname}\""); $i = 0; foreach ($pages as $file => $img) { echo '.' . ++$i . '.'; flush(); $file = html_entity_decode($file); // quickfix $img = html_entity_decode($img); $output = "{$dirname}\\{$file}"; // cUrl if ($n == 0) { $curlHandle = curl_multi_init(); } $curlList[$output] = Crawler::addHandle($curlHandle, $img); $n++; if ($n >= $size) { Crawler::execHandle($curlHandle); foreach ($curlList as $outfile => $curlEl) { // exec("mkdir \"".dirname($outfile)."\\\""); file_put_contents($outfile, curl_multi_getcontent($curlEl)); curl_multi_remove_handle($curlHandle, $curlEl); } curl_multi_close($curlHandle); $n = 0; $curlList = array(); } } echo "\n"; flush();
/** * Mendownload $size buah link sekaligus, lalu diproses oleh fungsi $function * @param integer $size jumlah thread yg diinginkan * @param array $pages_url array of full url * @param string $function nama fungsi yang dipanggil untuk memproses 1 halaman, * fungsinya punya 2 parameter (resource $fil, string $url) * @param array $params parameter tambahan ke fungsi */ public static function multiProcess($size, $pages_url, $function, $params = false) { $n = 0; $curlHandle = null; $curlList = array(); foreach ($pages_url as $aurl) { if ($n == 0) { $curlHandle = curl_multi_init(); } $curlList[$aurl] = Crawler::addHandle($curlHandle, $aurl); $n++; if ($n >= $size) { Crawler::execHandle($curlHandle); foreach ($curlList as $theurl => $curlEl) { $html = curl_multi_getcontent($curlEl); // Ada kemungkinan gagal retrieve if (trim($html)) { $fil = tmpfile(); fwrite($fil, $html); } else { $fil = $theurl; } if ($params) { call_user_func_array($function, array_merge(array($fil, $theurl), $params)); } else { $function($fil, $theurl); } curl_multi_remove_handle($curlHandle, $curlEl); } curl_multi_close($curlHandle); $n = 0; $curlList = array(); } } if ($curlList) { Crawler::execHandle($curlHandle); foreach ($curlList as $theurl => $curlEl) { $html = curl_multi_getcontent($curlEl); // Ada kemungkinan gagal retrieve if (trim($html)) { $fil = tmpfile(); fwrite($fil, $html); } else { $fil = $theurl; } if ($params) { call_user_func_array($function, array_merge(array($fil, $theurl), $params)); } else { $function($fil, $theurl); } curl_multi_remove_handle($curlHandle, $curlEl); } curl_multi_close($curlHandle); } }