class HttpProxyList
{
/**
* @param array[] список прокси серверов `[host: string, port: int]`
*/
private array $proxyList = [];
public function __construct(array $proxyList)
{
$this->proxyList = $proxyList;
}
protected function checkProxy(string $host, int $port): bool
{
$errorCode = $errorMessage = null;
try {
$handler = fsockopen($host, $port, $errorCode, $errorMessage, 3);
if ($handler !== false) {
fclose($handler);
return true;
}
} catch (Throwable $e) {
error_log("Proxy server '$host:$port' not available: $errorMessage [$errorCode]"):
}
return false;
}
public function getActiveProxy(): ?array
{
foreach ($this->proxyList as $key => $proxy) {
if ($this->checkProxy($proxy['host'], $proxy['port'])) {
return $proxy;
}
unset($this->proxyList[$key]);
}
return null;
}
}