简体   繁体   中英

PHP Guzzle Pool requests with proxy per request

i'm trying to set Pool requests to multiple urls, my only problem is that i want to set a new proxy in each request, cant figure the right way to do it, tried with Guzzle docs without luck.

my code:

$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;

The code is working perfect, the only missing part is the proxy change every request.

i tried to set

yield new Request('POST', $u->url, ['proxy' => $proxy], data...)

but that was just going without proxy at all..

any suggestions / help will be amazing..

Vlad.

GuzzleHttp\Psr7\Request doesn't take GuzzleHttp\RequestOptions like its GuzzleHttp\Client takes so when yielding the Request and passing 'proxy' option to it, Request has not effect.

You'll need to do something like this

$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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM