Skip to content

Commit c78d271

Browse files
committed
Fix php-curl-class#626: Add MultiCurl example for retrying with a new proxy
1 parent ea66e3b commit c78d271

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
require __DIR__ . '/../vendor/autoload.php';
3+
4+
use Curl\ArrayUtil;
5+
use Curl\MultiCurl;
6+
7+
$proxies = array(
8+
'someproxy.com:9999',
9+
'someproxy.com:80',
10+
'someproxy.com:443',
11+
);
12+
$max_retries = 3;
13+
14+
$multi_curl = new MultiCurl();
15+
$multi_curl->setProxyType(CURLPROXY_SOCKS5);
16+
$multi_curl->setProxies($proxies);
17+
18+
$multi_curl->setRetry(function ($instance) use ($proxies, $max_retries) {
19+
if ($instance->retries < $max_retries) {
20+
$new_random_proxy = ArrayUtil::arrayRandom($proxies);
21+
$instance->setProxy($new_random_proxy);
22+
return true;
23+
} else {
24+
return false;
25+
}
26+
});
27+
28+
$multi_curl->addGet('https://httpbin.org/ip');
29+
$multi_curl->addGet('https://httpbin.org/ip');
30+
$multi_curl->addGet('https://httpbin.org/ip');
31+
32+
$multi_curl->complete(function ($instance) {
33+
echo
34+
'curl id ' . $instance->id . ':' . "\n" .
35+
'- ip: ' . $instance->response->origin . "\n" .
36+
'- proxy: ' . $instance->getOpt(CURLOPT_PROXY) . "\n" .
37+
'- url: ' . $instance->effectiveUrl . '' . "\n" .
38+
'';
39+
});
40+
41+
$multi_curl->start();

0 commit comments

Comments
 (0)