From cab99aaee5b2a220e0f2aff0cce4ae57ca321383 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Wed, 28 Sep 2022 14:22:30 -0700 Subject: [PATCH] Add MultiCurl example for retrying with a new unique proxy (#729) --- .../multi_curl_get_with_new_random_proxy.php | 2 +- ..._curl_get_with_new_random_unique_proxy.php | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 examples/multi_curl_get_with_new_random_unique_proxy.php diff --git a/examples/multi_curl_get_with_new_random_proxy.php b/examples/multi_curl_get_with_new_random_proxy.php index 58bca60..1e02463 100644 --- a/examples/multi_curl_get_with_new_random_proxy.php +++ b/examples/multi_curl_get_with_new_random_proxy.php @@ -31,7 +31,7 @@ $multi_curl->addGet('https://httpbin.org/ip'); $multi_curl->complete(function ($instance) { echo - 'curl id ' . $instance->id . ':' . "\n" . + 'curl id ' . $instance->id . ' completed:' . "\n" . '- ip: ' . $instance->response->origin . "\n" . '- proxy: ' . $instance->getOpt(CURLOPT_PROXY) . "\n" . '- url: ' . $instance->effectiveUrl . '' . "\n" . diff --git a/examples/multi_curl_get_with_new_random_unique_proxy.php b/examples/multi_curl_get_with_new_random_unique_proxy.php new file mode 100644 index 0000000..9d30a54 --- /dev/null +++ b/examples/multi_curl_get_with_new_random_unique_proxy.php @@ -0,0 +1,49 @@ +setProxyType(CURLPROXY_SOCKS5); +$multi_curl->beforeSend(function ($instance) use ($proxies, &$current_proxies) { + if (!count($current_proxies)) { + $current_proxies = $proxies; + } + + // Use random proxy that hasn't been used yet. + $rand_key = ArrayUtil::arrayRandomIndex($current_proxies); + $new_random_proxy = $current_proxies[$rand_key]; + $instance->setProxy($new_random_proxy); + + // Remove proxy from list of current proxies. + unset($current_proxies[$rand_key]); + + // Re-index list of current proxies. + $current_proxies = array_values($current_proxies); + + echo 'about to make request ' . $instance->id . ' using proxy "' . $new_random_proxy . '".' . "\n"; +}); + +$multi_curl->addGet('https://httpbin.org/ip'); +$multi_curl->addGet('https://httpbin.org/ip'); +$multi_curl->addGet('https://httpbin.org/ip'); +$multi_curl->addGet('https://httpbin.org/ip'); + +$multi_curl->complete(function ($instance) { + echo + 'curl id ' . $instance->id . ' completed:' . "\n" . + '- ip: ' . $instance->response->origin . "\n" . + '- proxy: ' . $instance->getOpt(CURLOPT_PROXY) . "\n" . + '- url: ' . $instance->effectiveUrl . '' . "\n" . + ''; +}); + +$multi_curl->start();