@@ -31,8 +31,8 @@ low-level HTTP client that makes requests, like the following ``GET`` request::
31
31
32
32
use Symfony\Component\HttpClient\HttpClient;
33
33
34
- $httpClient = HttpClient::create();
35
- $response = $httpClient ->request('GET', 'https://api.github.com/repos/symfony/symfony-docs');
34
+ $client = HttpClient::create();
35
+ $response = $client ->request('GET', 'https://api.github.com/repos/symfony/symfony-docs');
36
36
37
37
$statusCode = $response->getStatusCode();
38
38
// $statusCode = 200
@@ -68,10 +68,10 @@ the transport explicitly, use the following classes to create the client::
68
68
use Symfony\Component\HttpClient\NativeHttpClient;
69
69
70
70
// uses native PHP streams
71
- $httpClient = new NativeHttpClient();
71
+ $client = new NativeHttpClient();
72
72
73
73
// uses the cURL PHP extension
74
- $httpClient = new CurlHttpClient();
74
+ $client = new CurlHttpClient();
75
75
76
76
When using this component in a full-stack Symfony application, this behavior is
77
77
not configurable and cURL will be used automatically if the cURL PHP extension
@@ -84,7 +84,7 @@ When requesting an ``https`` URL, HTTP/2 is enabled by default if libcurl >= 7.3
84
84
is used. To force HTTP/2 for ``http `` URLs, you need to enable it explicitly via
85
85
the ``http_version `` option::
86
86
87
- $httpClient = HttpClient::create(['http_version' => '2.0']);
87
+ $client = HttpClient::create(['http_version' => '2.0']);
88
88
89
89
Support for HTTP/2 PUSH works out of the box when libcurl >= 7.61 is used with
90
90
PHP >= 7.2.17 / 7.3.4: pushed responses are put into a temporary cache and are
@@ -96,16 +96,16 @@ Making Requests
96
96
The client created with the ``HttpClient `` class provides a single ``request() ``
97
97
method to perform all kinds of HTTP requests::
98
98
99
- $response = $httpClient ->request('GET', 'https://...');
100
- $response = $httpClient ->request('POST', 'https://...');
101
- $response = $httpClient ->request('PUT', 'https://...');
99
+ $response = $client ->request('GET', 'https://...');
100
+ $response = $client ->request('POST', 'https://...');
101
+ $response = $client ->request('PUT', 'https://...');
102
102
// ...
103
103
104
104
Responses are always asynchronous, so that the call to the method returns
105
105
immediately instead of waiting to receive the response::
106
106
107
107
// code execution continues immediately; it doesn't wait to receive the response
108
- $response = $httpClient ->request('GET', 'http://releases.ubuntu.com/18.04.2/ubuntu-18.04.2-desktop-amd64.iso');
108
+ $response = $client ->request('GET', 'http://releases.ubuntu.com/18.04.2/ubuntu-18.04.2-desktop-amd64.iso');
109
109
110
110
// getting the response headers waits until they arrive
111
111
$contentType = $response->getHeaders()['content-type'][0];
@@ -130,7 +130,7 @@ defined globally when creating the client (to apply it to all requests) and to
130
130
each request (which overrides any global authentication)::
131
131
132
132
// Use the same authentication for all requests
133
- $httpClient = HttpClient::create([
133
+ $client = HttpClient::create([
134
134
// HTTP Basic authentication with only the username and not a password
135
135
'auth_basic' => ['the-username'],
136
136
@@ -141,7 +141,7 @@ each request (which overrides any global authentication)::
141
141
'auth_bearer' => 'the-bearer-token',
142
142
]);
143
143
144
- $response = $httpClient ->request('GET', 'https://...', [
144
+ $response = $client ->request('GET', 'https://...', [
145
145
// use a different HTTP Basic authentication only for this request
146
146
'auth_basic' => ['the-username', 'the-password'],
147
147
@@ -155,7 +155,7 @@ You can either append them manually to the requested URL, or define them as an
155
155
associative array via the ``query `` option, that will be merged with the URL::
156
156
157
157
// it makes an HTTP GET request to https://httpbin.org/get?token=...&name=...
158
- $response = $httpClient ->request('GET', 'https://httpbin.org/get', [
158
+ $response = $client ->request('GET', 'https://httpbin.org/get', [
159
159
// these values are automatically encoded before including them in the URL
160
160
'query' => [
161
161
'token' => '...',
@@ -170,13 +170,13 @@ Use the ``headers`` option to define both the default headers added to all
170
170
requests and the specific headers for each request::
171
171
172
172
// this header is added to all requests made by this client
173
- $httpClient = HttpClient::create(['headers' => [
173
+ $client = HttpClient::create(['headers' => [
174
174
'User-Agent' => 'My Fancy App',
175
175
]]);
176
176
177
177
// this header is only included in this request and overrides the value
178
178
// of the same header if defined globally by the HTTP client
179
- $response = $httpClient ->request('POST', 'https://...', [
179
+ $response = $client ->request('POST', 'https://...', [
180
180
'headers' => [
181
181
'Content-Type' => 'text/plain',
182
182
],
@@ -189,7 +189,7 @@ This component provides several methods for uploading data using the ``body``
189
189
option. You can use regular strings, closures, iterables and resources and they'll be
190
190
processed automatically when making the requests::
191
191
192
- $response = $httpClient ->request('POST', 'https://...', [
192
+ $response = $client ->request('POST', 'https://...', [
193
193
// defining data using a regular string
194
194
'body' => 'raw data',
195
195
@@ -222,7 +222,7 @@ A generator or any ``Traversable`` can also be used instead of a closure.
222
222
given content will be JSON-encoded automatically and the request will add the
223
223
``Content-Type: application/json `` automatically too::
224
224
225
- $response = $httpClient ->request('POST', 'https://...', [
225
+ $response = $client ->request('POST', 'https://...', [
226
226
'json' => ['param1' => 'value1', '...'],
227
227
]);
228
228
@@ -265,7 +265,7 @@ making a request. Use the ``max_redirects`` setting to configure this behavior
265
265
(if the number of redirects is higher than the configured value, you'll get a
266
266
:class: `Symfony\\ Component\\ HttpClient\\ Exception\\ RedirectionException `)::
267
267
268
- $response = $httpClient ->request('GET', 'https://...', [
268
+ $response = $client ->request('GET', 'https://...', [
269
269
// 0 means to not follow any redirect
270
270
'max_redirects' => 0,
271
271
]);
@@ -294,7 +294,7 @@ uploads/downloads as they complete. This callback is guaranteed to be called on
294
294
DNS resolution, on arrival of headers and on completion; additionally it is
295
295
called when new data is uploaded or downloaded and at least once per second::
296
296
297
- $response = $httpClient ->request('GET', 'https://...', [
297
+ $response = $client ->request('GET', 'https://...', [
298
298
'on_progress' => function (int $dlNow, int $dlSize, array $info): void {
299
299
// $dlNow is the number of bytes downloaded so far
300
300
// $dlSize is the total size to be downloaded or -1 if it is unknown
@@ -319,7 +319,7 @@ The response returned by all HTTP clients is an object of type
319
319
:class: `Symfony\\ Contracts\\ HttpClient\\ ResponseInterface ` which provides the
320
320
following methods::
321
321
322
- $response = $httpClient ->request('GET', 'https://...');
322
+ $response = $client ->request('GET', 'https://...');
323
323
324
324
// gets the HTTP status code of the response
325
325
$statusCode = $response->getStatusCode();
@@ -358,7 +358,7 @@ Call the ``stream()`` method of the HTTP client to get *chunks* of the
358
358
response sequentially instead of waiting for the entire response::
359
359
360
360
$url = 'https://releases.ubuntu.com/18.04.1/ubuntu-18.04.1-desktop-amd64.iso';
361
- $response = $httpClient ->request('GET', $url, [
361
+ $response = $client ->request('GET', $url, [
362
362
// optional: if you don't want to buffer the response in memory
363
363
'buffer' => false,
364
364
]);
@@ -371,7 +371,7 @@ response sequentially instead of waiting for the entire response::
371
371
// get the response contents in chunk and save them in a file
372
372
// response chunks implement Symfony\Contracts\HttpClient\ChunkInterface
373
373
$fileHandler = fopen('/ubuntu.iso', 'w');
374
- foreach ($httpClient ->stream($response) as $chunk) {
374
+ foreach ($client ->stream($response) as $chunk) {
375
375
fwrite($fileHandler, $chunk->getContent());
376
376
}
377
377
@@ -405,7 +405,7 @@ When the HTTP status code of the response is in the 300-599 range (i.e. 3xx,
405
405
``getHeaders() `` and ``getContent() `` methods throw an appropriate exception::
406
406
407
407
// the response of this request will be a 403 HTTP error
408
- $response = $httpClient ->request('GET', 'https://httpbin.org/status/403');
408
+ $response = $client ->request('GET', 'https://httpbin.org/status/403');
409
409
410
410
// this code results in a Symfony\Component\HttpClient\Exception\ClientException
411
411
// because it doesn't check the status code of the response
@@ -600,25 +600,42 @@ class to autoconfigure the HTTP client based on the requested URL::
600
600
'Authorization' => 'token '.$githubToken,
601
601
],
602
602
],
603
+ // ...
603
604
]);
604
605
606
+ You can define several scopes, so that each set of options is added only if a
607
+ requested URL matches one of the regular expressions provided as keys.
608
+
605
609
If the request URL is relative (because you use the ``base_uri `` option), the
606
610
scoping HTTP client can't make a match. That's why you can define a third
607
611
optional argument in its constructor which will be considered the default
608
612
regular expression applied to relative URLs::
609
613
610
614
// ...
611
615
612
- $httpClient = new ScopingHttpClient($client, [
613
- 'https://api\.github\.com/' => [
614
- 'base_uri' => 'https://api.github.com/',
615
- // ...
616
+ $client = new ScopingHttpClient($client,
617
+ [
618
+ 'https://api\.github\.com/' => [
619
+ 'base_uri' => 'https://api.github.com/',
620
+ // ...
621
+ ],
616
622
],
617
- ],
618
- // this is the regexp applied to all relative URLs
623
+ // this is the index in the previous array that defines
624
+ // the base URI that shoud be used to resolve relative URLs
619
625
'https://api\.github\.com/'
620
626
);
621
627
628
+ The above example can be reduced to a simpler call::
629
+
630
+ // ...
631
+
632
+ $client = ScopingHttpClient::forBaseUri($client, 'https://api.github.com/', [
633
+ // ...
634
+ ]);
635
+
636
+ This way, the provided options will be used only if the requested URL is relative
637
+ or if it matches the ``https://api.github.com/ `` base URI.
638
+
622
639
Interoperability
623
640
----------------
624
641
@@ -744,11 +761,11 @@ into any services by type-hinting a constructor argument with the
744
761
745
762
class SomeService
746
763
{
747
- private $httpClient ;
764
+ private $client ;
748
765
749
- public function __construct(HttpClientInterface $httpClient )
766
+ public function __construct(HttpClientInterface $client )
750
767
{
751
- $this->httpClient = $httpClient ;
768
+ $this->client = $client ;
752
769
}
753
770
}
754
771
0 commit comments