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