Skip to content

Commit 6574668

Browse files
committed
Docs about batchSend() added
1 parent 32e60c3 commit 6574668

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

Client.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,20 @@ public function send($request)
215215

216216
/**
217217
* Performs multiple HTTP requests in parallel.
218+
* This method accepts an array of the [[Request]] objects and returns an array of the [[Response]] objects.
219+
* Keys of the response array correspond the ones from request array.
220+
*
221+
* ```php
222+
* $client = new Client();
223+
* $requests = [
224+
* 'news' => $client->get('http://domain.com/news'),
225+
* 'friends' => $client->get('http://domain.com/user/friends', ['userId' => 12]),
226+
* ];
227+
* $responses = $client->batchSend($requests);
228+
* var_dump($responses['news']->isOk);
229+
* var_dump($responses['friends']->isOk);
230+
* ```
231+
*
218232
* @param Request[] $requests requests to perform.
219233
* @return Response[] responses list.
220234
*/

Transport.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ abstract public function send($request);
2929
/**
3030
* Performs multiple HTTP requests.
3131
* Particular transport may benefit from this method, allowing sending requests in parallel.
32+
* This method accepts an array of the [[Request]] objects and returns an array of the [[Response]] objects.
33+
* Keys of the response array correspond the ones from request array.
3234
* @param Request[] $requests requests to perform.
3335
* @return Response[] responses list.
3436
*/

docs/guide/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Usage
1717
* [Transports](usage-transports.md)
1818
* [Request options](usage-request-options.md)
1919
* [Multi-part content](usage-multi-part-content.md)
20+
* [Batch request sending](usage-batch-request-sending.md)
2021
* [Logging and profiling](usage-logging.md)
2122
* [Setup Client instance](usage-setup-client-instance.md)
2223

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Batch request sending
2+
=====================
3+
4+
HTTP Client allows sending multiple requests at once using [[\yii\httpclient\Client::batchSend()]] method:
5+
6+
```php
7+
use yii\httpclient\Client;
8+
9+
$client = new Client();
10+
11+
$requests = [
12+
$client->get('http://domain.com/keep-alive'),
13+
$client->post('http://domain.com/notify', ['userId' => 12]),
14+
];
15+
$responses = $client->batchSend($requests);
16+
```
17+
18+
Particular [transport](usage-transports.md) may benefit from this method usage, allowing to increase the performance.
19+
Among the built-in transports, only [[\yii\httpclient\CurlTransport]] does this. It allows to send requests in parallel,
20+
which saves the program execution time.
21+
22+
> Note: only some particular transports allows processing requests at `batchSend()` in special way, which provides some
23+
benefit. By default transport just sends them one by one without any error or warning thrown. Make sure you have
24+
configured correct transport for the client, if you wish to achieve performance boost.
25+
26+
`batchSend()` method returns the array of the responses, which keys correspond the ones from array of requests.
27+
This allows you to process particular request response in easy way:
28+
29+
```php
30+
use yii\httpclient\Client;
31+
32+
$client = new Client();
33+
34+
$requests = [
35+
'news' => $client->get('http://domain.com/news'),
36+
'friends' => $client->get('http://domain.com/user/friends', ['userId' => 12]),
37+
'newComment' => $client->post('http://domain.com/user/comments', ['userId' => 12, 'content' => 'New comment']),
38+
];
39+
$responses = $client->batchSend($requests);
40+
41+
// result of `GET http://domain.com/news` :
42+
if ($responses['news']->isOk) {
43+
echo $responses['news']->content;
44+
}
45+
46+
// result of `GET http://domain.com/user/friends` :
47+
if ($responses['friends']->isOk) {
48+
echo $responses['friends']->content;
49+
}
50+
51+
// result of `POST http://domain.com/user/comments` :
52+
if ($responses['newComment']->isOk) {
53+
echo "Comment has been added successfully";
54+
}
55+
```

0 commit comments

Comments
 (0)