Answer the question
In order to leave comments, you need to log in
How to get last used proxy in Guzzle?
There is a list of proxies, I check them for operability through Guzzle.
How can I find out which proxy was used in the request and how long it took to complete the request?
$client = new Client(['timeout' => 3,
'on_stats' => function (TransferStats $stats) {
// тут можно получить время выполнения запроса
}
]);
$proxies = [
'202.147.195.132:8080',
'193.123.227.220:59394',
'103.47.67.158:8080',
'203.112.76.193:8080',
'36.92.107.194:8080',
];
array_walk($proxies, function (&$item) {
$item = trim($item);
});
$result = [];
$data = [];
foreach ($proxies as $proxy) {
$data[] = [
'proxy' => $proxy,
'request' => new Request('GET', 'https://example.com', [
'proxy' => $proxy,
'connect_timeout' => 3,
])
];
}
$requests = function () use ($data) {
foreach ($data as $item) {
yield $item['request'];
}
};
$pool = new Pool($client, $requests(), [
'concurrency' => 100,
// Тут через индекс узнаём какой прокси использовался. Уверен, что должен быть более удобный способ.
'fulfilled' => function (Response $response, $index) use ($data, &$result) {
$result[] = $data[$index]['proxy'];
}
]);
$promise = $pool->promise();
$promise->wait();
Answer the question
In order to leave comments, you need to log in
Guzzle has another way of passing tasks - through promises.
$requests = function () use ($data, $client) {
foreach ($data as $item) {
yield function () use ($client, $item) {
return $client->sendAsync($item['request'])
->then(function (Response $response) use ($item) {
echo $item['proxy'] . ' -> ' . $response->getStatusCode() . PHP_EOL;
});
};
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question