我正在使用多线程 curl 非常快速地检查许多代理
这是我为您的需要准备的代码
$mc = curl_multi_init();
$i=1;
while ($i <= 450000) {
$thread_no = $i;
$c [$thread_no] = curl_init();
curl_setopt($c [$thread_no], CURLOPT_URL, 'http://www.url.de/id='.$i.'&land=be');
curl_setopt($c [$thread_no], CURLOPT_HEADER, 0);
curl_setopt($c [$thread_no], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c [$thread_no], CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($c [$thread_no], CURLOPT_TIMEOUT, 10);
curl_multi_add_handle($mc, $c [$thread_no]);
$i++;
}
do {
while (($execrun = curl_multi_exec($mc, $running)) == CURLM_CALL_MULTI_PERFORM) ;
if ($execrun != CURLM_OK) break;
while ($done = curl_multi_info_read($mc)) {
$html=curl_multi_getcontent($done['handle']);
mysql_query("INSERT INTO hb (content) VALUES('$html')");
curl_multi_remove_handle($mc, $done ['handle']);
}
} while ($running);
curl_multi_close($mc);
我的代理直接代码是:
$proxies = $proxyL;
$mc = curl_multi_init();
for ($thread_no = 0; $thread_no < count($proxies); $thread_no++) {
$c [$thread_no] = curl_init();
curl_setopt($c [$thread_no], CURLOPT_URL, SERVER_URL . '/checkProxy.php');
curl_setopt($c [$thread_no], CURLOPT_HEADER, 0);
curl_setopt($c [$thread_no], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c [$thread_no], CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($c [$thread_no], CURLOPT_TIMEOUT, 10);
curl_setopt($c [$thread_no], CURLOPT_PROXY, trim($proxies [$thread_no]));
curl_setopt($c [$thread_no], CURLOPT_PROXYTYPE, 0);
curl_multi_add_handle($mc, $c [$thread_no]);
}
do {
while (($execrun = curl_multi_exec($mc, $running)) == CURLM_CALL_MULTI_PERFORM) ;
if ($execrun != CURLM_OK) break;
while ($done = curl_multi_info_read($mc)) {
$info = curl_getinfo($done ['handle']);
if (curl_multi_getcontent($done['handle']) == 'ok') {
$proxy[] = $proxies [array_search($done['handle'], $c)];
}
curl_multi_remove_handle($mc, $done ['handle']);
}
} while ($running);
curl_multi_close($mc);