Answer the question
In order to leave comments, you need to log in
How to use a random proxy for each request in Guzzle during asynchronous work?
Good afternoon. I am processing a list of sites asynchronously through Guzzle, for each site you need to use a random proxy from the list, the documentation has an example where one proxy is specified for the client and then used for all requests, is it possible to make each request run with a random proxy from the list ?
$client = new Client([
'timeout' => 10.0,
'proxy' => '51.77.66.35:10563'
]);
$requests = function () {
$handle = fopen("site.txt", "r");
if ($handle) {
while (($s = fgets($handle)) !== false) {
yield new Request('GET', trim($s));
}
fclose($handle);
}
};
$pool = new Pool($client, $requests(), [
'concurrency' => 25,
'fulfilled' => function ($response, $index) {
$body = $response->getBody();
// Обрабатываю $body
},
'rejected' => function ($reason, $index) {
// Обрабатываю ошибки
},
]);
// Инициализируем трансферы и создаём promise
$promise = $pool->promise();
// Ожидаем ответ promise // Принудительно завершаем пул запросов
$promise->wait();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question