You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+98-20Lines changed: 98 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,6 +35,7 @@ mess with most of the low-level details.
35
35
*[Methods](#methods)
36
36
*[Promises](#promises)
37
37
*[Blocking](#blocking)
38
+
*[Streaming](#streaming)
38
39
*[submit()](#submit)
39
40
*[send()](#send)
40
41
*[withOptions()](#withoptions)
@@ -53,7 +54,6 @@ mess with most of the low-level details.
53
54
*[SOCKS proxy](#socks-proxy)
54
55
*[UNIX domain sockets](#unix-domain-sockets)
55
56
*[Options](#options)
56
-
*[Streaming](#streaming)
57
57
*[Install](#install)
58
58
*[License](#license)
59
59
@@ -129,6 +129,18 @@ $browser->get($url)->then(
129
129
130
130
If this looks strange to you, you can also use the more traditional [blocking API](#blocking).
131
131
132
+
Keep in mind that resolving the Promise with the full response message means the
133
+
whole response body has to be kept in memory.
134
+
This is easy to get started and works reasonably well for smaller responses
135
+
(such as common HTML pages or RESTful or JSON API requests).
136
+
137
+
You may also want to look into the [streaming API](#streaming):
138
+
139
+
* If you're dealing with lots of concurrent requests (100+) or
140
+
* If you want to process individual data chunks as they happen (without having to wait for the full response body) or
141
+
* If you're expecting a big response body size (1 MiB or more, for example when downloading binary files) or
142
+
* If you're unsure about the response body size (better be safe than sorry when accessing arbitrary remote HTTP endpoints and the response body size is unknown in advance).
143
+
132
144
#### Blocking
133
145
134
146
As stated above, this library provides you a powerful, async API by default.
Please refer to [clue/block-react](https://github.com/clue/php-block-react#readme) for more details.
169
181
182
+
Keep in mind the above remark about buffering the whole response message in memory.
183
+
As an alternative, you may also see the following chapter for the
184
+
[streaming API](#streaming).
185
+
186
+
#### Streaming
187
+
188
+
All of the above examples assume you want to store the whole response body in memory.
189
+
This is easy to get started and works reasonably well for smaller responses.
190
+
191
+
However, there are several situations where it's usually a better idea to use a
192
+
streaming approach, where only small chunks have to be kept in memory:
193
+
194
+
* If you're dealing with lots of concurrent requests (100+) or
195
+
* If you want to process individual data chunks as they happen (without having to wait for the full response body) or
196
+
* If you're expecting a big response body size (1 MiB or more, for example when downloading binary files) or
197
+
* If you're unsure about the response body size (better be safe than sorry when accessing arbitrary remote HTTP endpoints and the response body size is unknown in advance).
198
+
199
+
The streaming API uses the same HTTP message API, but does not buffer the response
200
+
message body in memory.
201
+
It only processes the response body in small chunks as data is received and
202
+
forwards this data through [React's Stream API](https://github.com/reactphp/stream).
203
+
This works for (any number of) responses of arbitrary sizes.
204
+
205
+
This resolves with a normal [`ResponseInterface`](#responseinterface), which
206
+
can be used access the response message parameters as usual.
207
+
You can access the message body as usual, however it now
The `submit($url, array $fields, $headers = array(), $method = 'POST')` method can be used to submit an array of field values similar to submitting a form (`application/x-www-form-urlencoded`).
@@ -433,31 +528,14 @@ can be controlled via the following API (and their defaults):
433
528
$newBrowser = $browser->withOptions(array(
434
529
'followRedirects' => true,
435
530
'maxRedirects' => 10,
436
-
'obeySuccessCode' => true
531
+
'obeySuccessCode' => true,
532
+
'streaming' => false,
437
533
));
438
534
```
439
535
440
536
Notice that the [`Browser`](#browser) is an immutable object, i.e. the `withOptions()` method
441
537
actually returns a *new*[`Browser`](#browser) instance with the options applied.
442
538
443
-
### Streaming
444
-
445
-
Note: This API is subject to change.
446
-
447
-
The [`Sender`](#sender) emits a `progress` event array on its `Promise` that can be used
448
-
to intercept the underlying outgoing request stream (`React\HttpClient\Request` in the `requestStream` key)
449
-
and the incoming response stream (`React\HttpClient\Response` in the `responseStream` key).
450
-
451
-
```php
452
-
$client->get('http://www.google.com/')->then($handler, null, function ($event) {
453
-
if (isset($event['responseStream'])) {
454
-
/* @var $stream React\HttpClient\Response */
455
-
$stream = $event['responseStream'];
456
-
$stream->on('data', function ($data) { });
457
-
}
458
-
});
459
-
```
460
-
461
539
## Install
462
540
463
541
The recommended way to install this library is [through Composer](http://getcomposer.org).
0 commit comments