yiisoft/yii2-httpclient

How to catch exceptions in bulk request sending?

Coding-Yuan opened this issue · 1 comments

What steps will reproduce the problem?

In bulk request sending, when a request fails, the response cannot be obtained. When one request fails, how do other requests return correctly?

use yii\httpclient\Client;

$client = new Client();

$requests = [
    $client->get('http://domain.com/keep-alive'),
    $client->post('http://domain.com/notify', ['userId' => 12]),
];
$responses = $client->batchSend($requests);

What's expected?

In batch requests, not only some requests fail, but also other requests return correctly

What do you get instead?

If there is a request timeout in batch request sending, an exception is responded

Additional info

Q A
Yii version 2.0.35
Yii HTTP Client version ^2.0
PHP version 7.1.31
Operating system CentOS7.6

You should wrap the batchSend() call with a try catch and handle it on your side.

You can also configure the Client to report http errors with the following code

$client = new Client([
    'requestConfig' => [
        'options' => [
        'http' => [
                'ignore_errors' => false,
            ]
        ]
    ]
]);

These options are passed to the stream_context_create function of PHP core.