@@ -8,6 +8,9 @@ Aplus Framework HTTP (HyperText Transfer Protocol) Client Library.
88
99- `Installation `_
1010- `Usage `_
11+ - `Request `_
12+ - `Client `_
13+ - `Response `_
1114- `Conclusion `_
1215
1316Installation
@@ -41,6 +44,278 @@ The HTTP Client library is very simple and powerful which can be used as follows
4144 echo $response->getStatus();
4245 echo $response->getBody();
4346
47+ Request
48+ -------
49+
50+ To perform the hypertext transfer it is necessary to send a request message.
51+
52+ The HTTP client needs objects of the Request class to connect to a URL address.
53+
54+ The object can be instantiated by passing the URL in the constructor:
55+
56+ .. code-block :: php
57+
58+ use Framework\HTTP\Client\Request;
59+
60+ $request = new Request('http://domain.tld');
61+
62+ Request URL
63+ ###########
64+
65+ The URL can be changed using the ``setUrl `` method:
66+
67+ .. code-block :: php
68+
69+ $request->setUrl('http://domain.tld');
70+
71+ Note that when the URL is changed, the Host header will be as well.
72+
73+ Request Protocol
74+ ################
75+
76+ With the Request object instantiated, it is possible to set the desired HTTP
77+ protocol, through a string or a constant of the Protocol class:
78+
79+ .. code-block :: php
80+
81+ use Framework\HTTP\Protocol;
82+
83+ $request->setProtocol('HTTP/2');
84+ $request->setProtocol(Protocol::HTTP_2);
85+
86+ Request Method
87+ ##############
88+
89+ By default, the request method is ``GET ``. And, it can be changed through the
90+ ``setMethod `` method, passing a string or a constant from the Method class:
91+
92+ .. code-block :: php
93+
94+ use Framework\HTTP\Method;
95+
96+ $request->setMethod('post');
97+ $request->setMethod(Method::POST);
98+
99+ Request Headers
100+ ###############
101+
102+ Headers can be passed via the header set methods.
103+
104+ Below we see an example using string and a constant of the Header class:
105+
106+ .. code-block :: php
107+
108+ use Framework\HTTP\Header;
109+
110+ $request->setHeader('Content-Type', 'application/json');
111+ $request->setHeader(Header::CONTENT_TYPE, 'application/json');
112+
113+ To set the Content-Type it is possible to use a method for this:
114+
115+ .. code-block :: php
116+
117+ $request->setContentType('application/json');
118+
119+ JSON
120+ """"
121+
122+ When the request has the Content-Type as ``application/json `` and the body is a
123+ JSON string, it is possible to set the header and the body at once using the
124+ ``setJson `` method:
125+
126+ .. code-block :: php
127+
128+ $request->setJson($data);
129+
130+ Authorization
131+ """""""""""""
132+
133+ When working with APIs it is very common that a username and password (or token)
134+ is required to perform authorization.
135+
136+ To set Authorization as ``Basic ``, just use the ``setBasicAuth `` method:
137+
138+ .. code-block :: php
139+
140+ $username = 'johndoe';
141+ $password = 'secr3t';
142+ $request->setBasicAuth($username, $password);
143+
144+ User-Agent
145+ """"""""""
146+
147+ The default User-Agent can be set by calling the ``setUserAgent `` method and it
148+ is also possible to pass a name to it:
149+
150+ .. code-block :: php
151+
152+ $request->setUserAgent();
153+ $request->setUserAgent('Aplus HTTP Client');
154+
155+ Cookies
156+ """""""
157+
158+ Cookies can be set by the ``setCookie `` method:
159+
160+ .. code-block :: php
161+
162+ use Framework\HTTP\Cookie;
163+
164+ $cookie = new Cookie('session_id', 'abc123');
165+ $request->setCookie($cookie);
166+
167+ Post Forms
168+ ##########
169+
170+ To send data to a form you can set an array with the fields and values using the
171+ ``setPost `` method:
172+
173+ .. code-block :: php
174+
175+ $request->setPost([
176+ 'name' => 'John Doe',
177+ 178+ ]);
179+
180+ Request with Upload
181+ ###################
182+
183+ You can upload files with the ``setFiles `` method.
184+
185+ In it, you set the name of the array keys as field names and the values can be
186+ the path to a file, an instance of **CURLFile ** or **CURLStringFile **:
187+
188+ .. code-block :: php
189+
190+ $request->setFiles([
191+ 'invoices' => [
192+ __DIR__ . '/foo/invoice-10001.pdf',
193+ __DIR__ . '/foo/invoice-10002.pdf',
194+ ],
195+ 'foo' => new CURLStringFile('foo', 'foo.txt', 'text/plain')
196+ ]);
197+
198+ Request to Download
199+ ###################
200+
201+ When making requests to download files, define a callback in the
202+ ``setDownloadFunction `` method, with the first parameter receiving the data
203+ chunk:
204+
205+ .. code-block :: php
206+
207+ $request->setDownloadFunction(function (string $data) {
208+ file_put_contents(__DIR__ . '/video.mp4', $data, FILE_APPEND);
209+ });
210+
211+ Note that when this function is set the response body will be set to an
212+ empty string.
213+
214+ Client
215+ ------
216+
217+ The HTTP client is capable of performing synchronous and asynchronous requests.
218+
219+ Let's see how to instantiate it:
220+
221+ .. code-block :: php
222+
223+ use Framework\HTTP\Client\Client;
224+
225+ $client = new Client();
226+
227+ A request can be made by passing a Request instance in the ``run `` method, which
228+ will return a Response:
229+
230+ .. code-block :: php
231+
232+ $response = $client->run($request);
233+
234+ To perform asynchronous requests use the ``runMulti `` method, passing an array
235+ with request identifiers as keys and Requests as values.
236+
237+ The ``runMulti `` method will return a **Generator ** with the request id in the
238+ key and a Response instance as a value.
239+
240+ Responses will be delivered as requests are finalized:
241+
242+ .. code-block :: php
243+
244+ $requests = [
245+ 1 => new Request('https://domain.tld/foo'),
246+ 2 => new Request('https://domain.tld/bar'),
247+ ];
248+
249+ foreach($client->runMulti($requests) as $id => $response) {
250+ echo "Request $id responded:";
251+ echo '<pre >' . htmlentities((string) $response) . '</pre >';
252+ }
253+
254+ Response
255+ --------
256+
257+ After running a request in the client, it will return an instance of the
258+ Response class.
259+
260+ Response Protocol
261+ #################
262+
263+ With it it is possible to obtain the protocol:
264+
265+ .. code-block :: php
266+
267+ $protocol = $response->getProtocol();
268+
269+ Response Status
270+ ###############
271+
272+ Also, you can get the response status:
273+
274+ .. code-block :: php
275+
276+ $response->getStatusCode();
277+ $response->getStatusReason();
278+ $response->getStatus();
279+
280+ Response Headers
281+ ################
282+
283+ It is also possible to get all headers at once:
284+
285+ .. code-block :: php
286+
287+ $headers = $response->getHeaders();
288+
289+ Or, get the headers individually:
290+
291+ .. code-block :: php
292+
293+ use Framework\HTTP\Header;
294+
295+ $response->getHeader('Content-Type');
296+ $response->getHeader(Header::CONTENT_TYPE);
297+
298+ Response Body
299+ #############
300+
301+ The message body, when set, can be obtained with the ``getBody `` method:
302+
303+ .. code-block :: php
304+
305+ $body = $response->getBody();
306+
307+ JSON Response
308+ #############
309+
310+ Also, you can check if the response content type is JSON and get the JSON data
311+ as an object or array in PHP:
312+
313+ .. code-block :: php
314+
315+ if ($response->isJson()) {
316+ $data = $response->getJson();
317+ }
318+
44319 Conclusion
45320----------
46321
@@ -52,5 +327,5 @@ The more you use it, the more you will learn.
52327.. note ::
53328 Did you find something wrong?
54329 Be sure to let us know about it with an
55- `issue <https://gitlab.com/aplus-framework/libraries/http-client/issues >`_.
330+ `issue <https://gitlab.com/aplus-framework/libraries/http-client/-/ issues >`_.
56331 Thank you!
0 commit comments