本文整理汇总了PHP中Crawler::addHandle方法的典型用法代码示例。如果您正苦于以下问题:PHP Crawler::addHandle方法的具体用法?PHP Crawler::addHandle怎么用?PHP Crawler::addHandle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crawler
的用法示例。
在下文中一共展示了Crawler::addHandle方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: flush
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();
示例2: multiProcess
/**
* 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);
}
}