mdref.json config
[mdref/mdref-http] / http / Client / enqueue.md
1 # http\Client http\Client::enqueue(http\Client\Request $request[, callable $cb])
2
3 Add another http\Client\Request to the request queue.
4 If the optional callback $cb returns true, the request will be automatically dequeued.
5
6 > ***Note:***
7 > The http\Client\Response object resulting from the request is always stored
8 > internally to be retrieved at a later time, __even__ when $cb is used.
9 >
10 > If you are about to send a lot of requests and do __not__ need the response
11 > after executing the callback, you can use http\Client::getResponse() within
12 > the callback to keep the memory usage level as low as possible.
13
14 See http\Client::dequeue() and http\Client::send().
15
16
17 ## Params:
18
19 * http\Client\Request $request
20 The request to enqueue.
21 * Optional callable $cb as function(\http\Response $response) : ?bool
22 A callback to automatically call when the request has finished.
23
24 ## Returns:
25
26 * http\Client, self.
27
28 ## Throws:
29
30 * http\Exception\InvalidArgumentException
31 * http\Exception\BadMethodCallException
32 * http\Exception\RuntimeException
33
34 ## Example:
35
36 (new http\Client)->enqueue(new http\Client\Request("GET", "http://php.net"),
37 function(http\Client\Response $res) {
38 printf("%s returned %d\n", $res->getTransferInfo("effective_url"), $res->getResponseCode());
39 return true; // dequeue
40 })->send();
41
42 Yields:
43
44 http://php.net/ returned 200