-
Notifications
You must be signed in to change notification settings - Fork 824
/
multi_curl_track_success_urls.php
126 lines (111 loc) · 3.21 KB
/
multi_curl_track_success_urls.php
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
require __DIR__ . '/../vendor/autoload.php';
use Curl\MultiCurl;
// List of pages to fetch.
$urls = [
'https://httpbin.org/status/503',
'https://httpbin.org/status/200',
'https://httpbin.org/status/401',
'https://httpbin.org/delay/3',
'https://httpbin.org/status/201',
'https://httpbin.org/delay/1',
'https://httpbin.org/status/500',
'https://httpbin.org/status/504',
];
$request_stats = [
'all' => [],
'successful' => [],
'errors' => [],
'completed' => [],
'not_completed' => [],
];
$multi_curl = new MultiCurl();
$multi_curl->setConcurrency(2);
foreach ($urls as $url) {
// Queue requests.
$request = $multi_curl->addGet($url);
// Track all requests queued.
$request_stats['all'][] = $request->url;
}
// Track successful requests.
$multi_curl->success(function ($instance) use (&$request_stats, $multi_curl) {
$request_stats['successful'][] = $instance->url;
// Optionally, stop additional requests based on some condition (e.g. stop
// after a number of successful requests).
if (count($request_stats['successful']) >= 3) {
$multi_curl->stop();
}
});
// Track requests that errored.
$multi_curl->error(function ($instance) use (&$request_stats) {
$request_stats['errors'][] = $instance->url;
});
// Track requests that completed.
$multi_curl->complete(function ($instance) use (&$request_stats) {
$request_stats['completed'][] = $instance->url;
});
$multi_curl->start();
// Determine urls not completed.
$request_stats['not_completed'] = array_diff($request_stats['all'], $request_stats['completed']);
// Display results.
var_dump($request_stats);
// $ php multi_curl_track_success_urls.php
// array(5) {
// ["all"]=>
// array(8) {
// [0]=>
// string(30) "https://httpbin.org/status/503"
// [1]=>
// string(30) "https://httpbin.org/status/200"
// [2]=>
// string(30) "https://httpbin.org/status/401"
// [3]=>
// string(27) "https://httpbin.org/delay/3"
// [4]=>
// string(30) "https://httpbin.org/status/201"
// [5]=>
// string(27) "https://httpbin.org/delay/1"
// [6]=>
// string(30) "https://httpbin.org/status/500"
// [7]=>
// string(30) "https://httpbin.org/status/504"
// }
// ["successful"]=>
// array(3) {
// [0]=>
// string(30) "https://httpbin.org/status/200"
// [1]=>
// string(27) "https://httpbin.org/delay/3"
// [2]=>
// string(30) "https://httpbin.org/status/201"
// }
// ["errors"]=>
// array(2) {
// [0]=>
// string(30) "https://httpbin.org/status/503"
// [1]=>
// string(30) "https://httpbin.org/status/401"
// }
// ["completed"]=>
// array(5) {
// [0]=>
// string(30) "https://httpbin.org/status/200"
// [1]=>
// string(30) "https://httpbin.org/status/503"
// [2]=>
// string(30) "https://httpbin.org/status/401"
// [3]=>
// string(27) "https://httpbin.org/delay/3"
// [4]=>
// string(30) "https://httpbin.org/status/201"
// }
// ["not_completed"]=>
// array(3) {
// [5]=>
// string(27) "https://httpbin.org/delay/1"
// [6]=>
// string(30) "https://httpbin.org/status/500"
// [7]=>
// string(30) "https://httpbin.org/status/504"
// }
// }