Im trying create a PHP script that uses proxy and asynchronous requests with Guzzle but Im not reaching the success.
On the Guzzle 7 docs, we can see this example below:
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
$client = new Client();
$requests = function ($total) {
$uri = 'http://127.0.0.1:8126/guzzle-server/perf';
for ($i = 0; $i < $total; $i++) {
yield new Request('GET', $uri);
}
};
$pool = new Pool($client, $requests(100), [
'concurrency' => 5,
'fulfilled' => function (Response $response, $index) {
// this is delivered each successful response
},
'rejected' => function (RequestException $reason, $index) {
// this is delivered each failed request
},
]);
// Initiate the transfers and create a promise
$promise = $pool->promise();
// Force the pool of requests to complete.
$promise->wait();
And with my low knowledge on Guzzle, I think that we can apply proxies only on Client
not on Request
class, so what I've to do to make it happens?
I've been created this example how it will works using simple cURL;
$mh = curl_multi_init();
$ch = [];
$headers = array(
'Host: example.com',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'DNT: 1',
'Connection: keep-alive'
);
$timeout = 7;
$chunked = array_chunk($proxies, 1000);
foreach($chunked as $proxies) {
foreach($proxies as $i => $proxy) {
$ch[$i] = curl_init();
curl_setopt($ch[$i], CURLOPT_AUTOREFERER, true);
curl_setopt($ch[$i], CURLOPT_COOKIESESSION, true);
curl_setopt($ch[$i], CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch[$i], CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch[$i], CURLOPT_HTTPPROXYTUNNEL, true);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYSTATUS, false);
curl_setopt($ch[$i], CURLOPT_PROXY_SSL_VERIFYPEER, false);
curl_setopt($ch[$i], CURLOPT_CONNECTTIMEOUT_MS, $timeout * 1000);
curl_setopt($ch[$i], CURLOPT_MAXREDIRS, -1);
curl_setopt($ch[$i], CURLOPT_SOCKS5_AUTH, CURLAUTH_NONE);
curl_setopt($ch[$i], CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch[$i], CURLOPT_PROXY_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
curl_setopt($ch[$i], CURLOPT_PROXY_SSL_VERIFYHOST, 0);
curl_setopt($ch[$i], CURLOPT_TIMEOUT_MS, $timeout * 1000);
curl_setopt($ch[$i], CURLOPT_ENCODING, 'gzip, deflate, br');
curl_setopt($ch[$i], CURLOPT_PROXY, $proxy);
curl_setopt($ch[$i], CURLOPT_URL, 'https://example.com');
curl_setopt($ch[$i], CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0');
curl_setopt($ch[$i], CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYHOST, 0);
curl_multi_add_handle($mh, $ch[$i]);
}
do {
curl_multi_exec($mh, $running);
curl_multi_select($mh);
} while ($running > 0);
foreach($ch as $key => $handle) {
$content = curl_multi_getcontent($handle);
if (strstr($content, 'Example')) {
file_put_contents('proxies.txt', $proxies[$key].PHP_EOL , FILE_APPEND | LOCK_EX);
}
curl_multi_remove_handle($mh, $ch[$key]);
}
curl_multi_close($mh);
}