B
B
Bur Ov2021-12-17 11:35:56
PHP
Bur Ov, 2021-12-17 11:35:56

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

1 answer(s)
B
Bur Ov, 2021-12-17
@burov0798

$proxy = open("./proxy..txt");

$requests = function ($data) use ($client, $proxy) { 
    foreach ($data as $u) {
        $p = trim($proxy[array_rand($proxy)]); 
        yield function() use ($client, $u, $p) {
            return $client->request(
                'GET',
                trim($u),
                [
                    'proxy' => $p
                ]
            );
        };
    }
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question