并发请求curl_multi_exec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 普通非代理
function curl_multi_request ($query_arr) {
$ch = curl_multi_init();
$count = count($query_arr);
$ch_arr = array();
for ($i = 0; $i < $count; $i++) {
$query_string = $query_arr[$i];
$ch_arr[$i] = curl_init($query_string);
curl_setopt($ch_arr[$i], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($ch, $ch_arr[$i]);
}
$running = null;
do {
curl_multi_exec($ch, $running);
} while ($running > 0);
for ($i = 0; $i < $count; $i++) {
$results[$i] = curl_multi_getcontent($ch_arr[$i]);
curl_multi_remove_handle($ch, $ch_arr[$i]);
}
curl_multi_close($ch);
return $results;
}

#通过代理

function curl_multi_request ($query_arr) {
$cookie_file = $_SERVER['DOCUMENT_ROOT'] . '/cache/cookie.txt';
$ch = curl_multi_init();
$count = count($query_arr);
$ch_arr = array();
for ($i = 0; $i < $count; $i++) {
$query_string = $query_arr[$i];
$ch_arr[$i] = curl_init($query_string);
$proxy = "111.111.111.111";
$proxyport = "8877";
curl_setopt( $ch_arr[$i],curlOPT_proxy,$proxy);
curl_setopt( $ch_arr[$i],curlOPT_proxyPORT,$proxyport);
curl_setopt( $ch_arr[$i], CURLOPT_URL, $query_string); // set url to post to
curl_setopt( $ch_arr[$i], CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:10.0) Gecko/20100101 Firefox/10.0");
curl_setopt( $ch_arr[$i], CURLOPT_HTTPHEADER, array('Expect:'));
// curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt( $ch_arr[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch_arr[$i], CURLOPT_REFERER, '');
curl_setopt( $ch_arr[$i], CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt( $ch_arr[$i], CURLOPT_FOLLOWLOCATION, true); // allow redirects
curl_setopt($ch_arr[$i], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($ch, $ch_arr[$i]);
}
$running = null;
do {
curl_multi_exec($ch, $running);
} while ($running > 0);
for ($i = 0; $i < $count; $i++) {
$results[$i] = curl_multi_getcontent($ch_arr[$i]);
curl_multi_remove_handle($ch, $ch_arr[$i]);
}
curl_multi_close($ch);
return $results;
}