PHP Guzzle池请求,每个请求使用代理

PHP Guzzle池请求,每个请求使用代理,php,asynchronous,proxy,python-requests,guzzle,Php,Asynchronous,Proxy,Python Requests,Guzzle,我正在尝试将池请求设置为多个URL,我唯一的问题是,我想在每个请求中设置一个新的代理,无法找到正确的方法,尝试使用Guzzle文档时运气不佳 我的代码: $proxies = file('./proxies.txt'); $proxy = trim($proxies[array_rand($proxies)]); $this->headers['Content-Type'] = 'application/json'; $this->headers['User-Agent'] = '

我正在尝试将池请求设置为多个URL,我唯一的问题是,我想在每个请求中设置一个新的代理,无法找到正确的方法,尝试使用Guzzle文档时运气不佳

我的代码:

$proxies = file('./proxies.txt');
$proxy = trim($proxies[array_rand($proxies)]);

$this->headers['Content-Type'] = 'application/json';
$this->headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36';

$client = new Client();

$requests = function(array $data) {
    foreach ($data as $u) {
        yield new Request('POST', $u->url, $this->headers,
            json_encode([
                'text' => $u->s,
            ])
        );

    }
};

$pool = new Pool($client, $requests($data), [
    'concurrency' => 20,
    'fulfilled' => function(Response $response, $index) use ($data) {
        $data->result = json_decode((String)$response->getBody());
        $data->status = True;
        $data->index = $index;
    },
    'rejected' => function(RequestException $reason, $index) use ($data) {
        $data[$index]->index = $index;
        $data[$index]->rejected = $reason;
    }
]);

$promise = $pool->promise();
$promise->wait();

return $data;
代码工作得很完美,唯一缺少的部分是代理更改每个请求

我试着设置

yield new Request('POST', $u->url, ['proxy' => $proxy], data...)
但那完全是没有代理的

任何建议/帮助都将是惊人的


Vlad.

GuzzleHttp\Psr7\Request
不像它的
GuzzleHttp\RequestOptions
那样接受
GuzzleHttp\Client
请求,所以当产生
请求并向其传递'proxy'选项时,请求无效

你需要这样做

$requests = function ($data) use ($client, $proxy, $headers) {
    foreach ($data as $u) {
        yield function() use ($client, $u, $proxy, $headers) {
            return $client->request(
                'POST',
                $u->url,
                [
                    'proxy' => $proxy,
                    'headers' => $headers
                ]
            );
        };
    }
};

$pool = new Pool($client, $requests($data));

非常感谢你。