preliminary 2.3.0 docs
[mdref/mdref-http] / http / Client.md
1 # class http\Client implements SplSubject, Countable
2
3 The HTTP client. See http\Client\Curl's [options](http/Client/Curl#Options:) which is the only driver currently supported.
4
5 ## Changelog:
6
7 Version | Change
8 --------|-------
9 2.3.0 | Deprecated methods:<br>http\Client::enablePipelining() and <br>http\Client::enableEvents().<br>Added Methods:<br>http\Client::configure(),<br>http\Client::getAvailableConfiguration() and<br>http\Client::getAvailableOptions().
10
11 ## Examples:
12
13 ### Sending a simple GET request:
14
15 <?php
16
17 $request = new http\Client\Request("GET",
18 "http://localhost",
19 ["User-Agent"=>"My Client/0.1"]
20 );
21 $request->setOptions(["timeout"=>1]);
22
23 $client = new http\Client;
24 $client->enqueue($request)->send();
25
26 // pop the last retrieved response
27 $response = $client->getResponse();
28 printf("%s returned '%s' (%d)\n",
29 $response->getTransferInfo("effective_url"),
30 $response->getInfo(),
31 $response->getResponseCode()
32 );
33 ?>
34
35 #### Yields:
36
37 http://localhost/ returned 'HTTP/1.1 200 OK' (200)
38
39
40 ### Submitting a standard form:
41
42 <?php
43
44 $request = new http\Client\Request("POST",
45 "http://localhost/post.php",
46 ["Content-Type" => "application/x-www-form-urlencoded"]
47 );
48 $request->getBody()->append(new http\QueryString([
49 "user" => "mike",
50 "name" => "Michael Wallner"
51 ]));
52
53 $client = new http\Client;
54 $client->setOptions(["ssl" => [
55 "version" => http\Client\Curl\SSL_VERSION_TLSv1
56 ]]);
57 $client->enqueue($request)->send();
58
59 // ask for the response for this specific request
60 $response = $client->getResponse($request);
61 printf("-> %s\n", $response->getInfo());
62
63 ?>
64
65 #### Yields:
66
67 -> HTTP/1.1 200 OK
68
69
70 ### Submitting a multipart form:
71
72 <?php
73
74 $request = new http\Client\Request("POST",
75 "http://localhost/post.php"
76 );
77
78 // http\Message\Body::addForm() will automatically add
79 // Content-Type: multipart/form-data to the request headers
80 $request->getBody()->addForm([
81 "user" => "mike",
82 "name" => "Michael Wallner"
83 ], [
84 [
85 "name" => "image",
86 "type" => "image/jpeg",
87 "file" => "image.jpg"
88 ]
89 ]);
90
91 $client = new http\Client;
92 $client->setOptions(["ssl" => [
93 "version" => http\Client\Curl\SSL_VERSION_TLSv1
94 ]]);
95 $client->enqueue($request)->send();
96
97 // ask for the response for this specific request
98 $response = $client->getResponse($request);
99 printf("-> %.2F kB\n @ %.2F Mbit",
100 .001 * $response->getTransferInfo("size_upload"),
101 .0000008 * $response->getTransferInfo("speed_upload")
102 );
103 ?>
104
105 #### Yields:
106
107 -> 15.98 kB @ 6.77 Mbit
108
109
110 ## Properties:
111
112 * private SplObjectStorage $observers = NULL
113 Attached observers.
114 * protected array $options = NULL
115 Set options.
116 * protected http\Message $history = NULL
117 Request/response history.
118 * public bool $recordHistory = false
119 Whether to record history in http\Client::$history.