@@ -433,6 +433,34 @@ When the HTTP status code of the response is in the 300-599 range (i.e. 3xx,
433
433
// instead the original response content (even if it's an error message)
434
434
$content = $response->getContent(false);
435
435
436
+ While responses are lazy, their destructor will always wait for headers to come
437
+ back. This means that the following request *will * complete; and if e.g. a 404
438
+ is returned, an exception will be thrown::
439
+
440
+ // because the returned value is not assigned to a variable, the destructor
441
+ // of the returned response will be called immediately and will throw if the
442
+ // status code is in the 300-599 range
443
+ $client->request('POST', 'https://...');
444
+
445
+ This in turn means that unassigned responses will fallback to synchronous requests.
446
+ If you want to make these requests concurrent, you can store their corresponding
447
+ responses in an array::
448
+
449
+ $responses[] = $client->request('POST', 'https://.../path1');
450
+ $responses[] = $client->request('POST', 'https://.../path2');
451
+ // ...
452
+
453
+ // This line will trigger the destructor of all responses stored in the array;
454
+ // they will complete concurrently and an exception will be thrown in case a
455
+ // status code in the 300-599 range is returned
456
+ unset($responses);
457
+
458
+ This behavior provided at destruction-time is part of the fail-safe design of the
459
+ component. No errors will be unnoticed: if you don't write the code to handle
460
+ errors, exceptions will notify you when needed. On the other hand, if you write
461
+ the error-handling code, you will opt-out from these fallback mechanisms as the
462
+ destructor won't have anything remaining to do.
463
+
436
464
Concurrent Requests
437
465
-------------------
438
466
@@ -567,7 +595,16 @@ catching ``TransportExceptionInterface`` in the foreach loop::
567
595
568
596
foreach ($client->stream($responses) as $response => $chunk) {
569
597
try {
570
- if ($chunk->isLast()) {
598
+ if ($chunk->isTimeout()) {
599
+ // ... decide what to do when a timeout occurs
600
+ // if you want to stop a response that timed out, don't miss
601
+ // calling $response->cancel() or the destructor of the response
602
+ // will try to complete it one more time
603
+ } elseif ($chunk->isFirst()) {
604
+ // if you want to check the status code, you must do it when the
605
+ // first chunk arrived, using $response->getStatusCode();
606
+ // not doing so might trigger an HttpExceptionInterface
607
+ } elseif ($chunk->isLast()) {
571
608
// ... do something with $response
572
609
}
573
610
} catch (TransportExceptionInterface $e) {
0 commit comments