guzzle / guzzle

Guzzle, an extensible PHP HTTP client

Home Page:https://docs.guzzlephp.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't catch ConnectException when using pool

singleseeker opened this issue · comments

Description

"guzzlehttp/guzzle": "^7.0.1",

I found that it's no way to catch the ConnectException when we're using Pool.

If connectException happens, it though out directly with the message.

failed: GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;

// initiate http client
$client = new Client([
    'connect_timeout' => 10,
    'timeout'         => 10.00,
    'http_errors'     => true,
    'headers' => [
        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
        'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    ]
]);
// global storage where all results will be added to:
$results = [];

// First we define our main scraping loop:
function scrape($urls, $callback, $errback)
{
    // create 10 Request objects:
    $requests = array_map(function ($url) {
        return new Request('GET', $url);
    }, $urls);
    global $client;
    $pool = new Pool($client, $requests, [
        'concurrency' => 5,
        'fulfilled' => $callback,
        'rejected' => $errback,
    ]);
    $pool->promise()->wait();
}
commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.

@singleseeker did you resolved this behaviour?