@@ -30,8 +30,8 @@ low-level HTTP client that makes requests, like the following ``GET`` request::
30
30
31
31
use Symfony\Component\HttpClient\HttpClient;
32
32
33
- $httpClient = HttpClient::create();
34
- $response = $httpClient ->request('GET', 'https://api.github.com/repos/symfony/symfony-docs');
33
+ $client = HttpClient::create();
34
+ $response = $client ->request('GET', 'https://api.github.com/repos/symfony/symfony-docs');
35
35
36
36
$statusCode = $response->getStatusCode();
37
37
// $statusCode = 200
@@ -67,10 +67,10 @@ the transport explicitly, use the following classes to create the client::
67
67
use Symfony\Component\HttpClient\NativeHttpClient;
68
68
69
69
// uses native PHP streams
70
- $httpClient = new NativeHttpClient();
70
+ $client = new NativeHttpClient();
71
71
72
72
// uses the cURL PHP extension
73
- $httpClient = new CurlHttpClient();
73
+ $client = new CurlHttpClient();
74
74
75
75
When using this component in a full-stack Symfony application, this behavior is
76
76
not configurable and cURL will be used automatically if the cURL PHP extension
@@ -83,7 +83,7 @@ When requesting an ``https`` URL, HTTP/2 is enabled by default if libcurl >= 7.3
83
83
is used. To force HTTP/2 for ``http `` URLs, you need to enable it explicitly via
84
84
the ``http_version `` option::
85
85
86
- $httpClient = HttpClient::create(['http_version' => '2.0']);
86
+ $client = HttpClient::create(['http_version' => '2.0']);
87
87
88
88
Support for HTTP/2 PUSH works out of the box when libcurl >= 7.61 is used with
89
89
PHP >= 7.2.17 / 7.3.4: pushed responses are put into a temporary cache and are
@@ -95,16 +95,16 @@ Making Requests
95
95
The client created with the ``HttpClient `` class provides a single ``request() ``
96
96
method to perform all kinds of HTTP requests::
97
97
98
- $response = $httpClient ->request('GET', 'https://...');
99
- $response = $httpClient ->request('POST', 'https://...');
100
- $response = $httpClient ->request('PUT', 'https://...');
98
+ $response = $client ->request('GET', 'https://...');
99
+ $response = $client ->request('POST', 'https://...');
100
+ $response = $client ->request('PUT', 'https://...');
101
101
// ...
102
102
103
103
Responses are always asynchronous, so that the call to the method returns
104
104
immediately instead of waiting to receive the response::
105
105
106
106
// code execution continues immediately; it doesn't wait to receive the response
107
- $response = $httpClient ->request('GET', 'http://releases.ubuntu.com/18.04.2/ubuntu-18.04.2-desktop-amd64.iso');
107
+ $response = $client ->request('GET', 'http://releases.ubuntu.com/18.04.2/ubuntu-18.04.2-desktop-amd64.iso');
108
108
109
109
// getting the response headers waits until they arrive
110
110
$contentType = $response->getHeaders()['content-type'][0];
@@ -129,7 +129,7 @@ defined globally when creating the client (to apply it to all requests) and to
129
129
each request (which overrides any global authentication)::
130
130
131
131
// Use the same authentication for all requests
132
- $httpClient = HttpClient::create([
132
+ $client = HttpClient::create([
133
133
// HTTP Basic authentication (there are multiple ways of configuring it)
134
134
'auth_basic' => ['the-username'],
135
135
'auth_basic' => ['the-username', 'the-password'],
@@ -144,7 +144,7 @@ each request (which overrides any global authentication)::
144
144
'auth_ntlm' => 'the-username:the-password',
145
145
]);
146
146
147
- $response = $httpClient ->request('GET', 'https://...', [
147
+ $response = $client ->request('GET', 'https://...', [
148
148
// use a different HTTP Basic authentication only for this request
149
149
'auth_basic' => ['the-username', 'the-password'],
150
150
@@ -166,7 +166,7 @@ You can either append them manually to the requested URL, or define them as an
166
166
associative array via the ``query `` option, that will be merged with the URL::
167
167
168
168
// it makes an HTTP GET request to https://httpbin.org/get?token=...&name=...
169
- $response = $httpClient ->request('GET', 'https://httpbin.org/get', [
169
+ $response = $client ->request('GET', 'https://httpbin.org/get', [
170
170
// these values are automatically encoded before including them in the URL
171
171
'query' => [
172
172
'token' => '...',
@@ -181,13 +181,13 @@ Use the ``headers`` option to define both the default headers added to all
181
181
requests and the specific headers for each request::
182
182
183
183
// this header is added to all requests made by this client
184
- $httpClient = HttpClient::create(['headers' => [
184
+ $client = HttpClient::create(['headers' => [
185
185
'User-Agent' => 'My Fancy App',
186
186
]]);
187
187
188
188
// this header is only included in this request and overrides the value
189
189
// of the same header if defined globally by the HTTP client
190
- $response = $httpClient ->request('POST', 'https://...', [
190
+ $response = $client ->request('POST', 'https://...', [
191
191
'headers' => [
192
192
'Content-Type' => 'text/plain',
193
193
],
@@ -200,7 +200,7 @@ This component provides several methods for uploading data using the ``body``
200
200
option. You can use regular strings, closures, iterables and resources and they'll be
201
201
processed automatically when making the requests::
202
202
203
- $response = $httpClient ->request('POST', 'https://...', [
203
+ $response = $client ->request('POST', 'https://...', [
204
204
// defining data using a regular string
205
205
'body' => 'raw data',
206
206
@@ -233,7 +233,7 @@ A generator or any ``Traversable`` can also be used instead of a closure.
233
233
given content will be JSON-encoded automatically and the request will add the
234
234
``Content-Type: application/json `` automatically too::
235
235
236
- $response = $httpClient ->request('POST', 'https://...', [
236
+ $response = $client ->request('POST', 'https://...', [
237
237
'json' => ['param1' => 'value1', '...'],
238
238
]);
239
239
@@ -276,7 +276,7 @@ making a request. Use the ``max_redirects`` setting to configure this behavior
276
276
(if the number of redirects is higher than the configured value, you'll get a
277
277
:class: `Symfony\\ Component\\ HttpClient\\ Exception\\ RedirectionException `)::
278
278
279
- $response = $httpClient ->request('GET', 'https://...', [
279
+ $response = $client ->request('GET', 'https://...', [
280
280
// 0 means to not follow any redirect
281
281
'max_redirects' => 0,
282
282
]);
@@ -305,7 +305,7 @@ uploads/downloads as they complete. This callback is guaranteed to be called on
305
305
DNS resolution, on arrival of headers and on completion; additionally it is
306
306
called when new data is uploaded or downloaded and at least once per second::
307
307
308
- $response = $httpClient ->request('GET', 'https://...', [
308
+ $response = $client ->request('GET', 'https://...', [
309
309
'on_progress' => function (int $dlNow, int $dlSize, array $info): void {
310
310
// $dlNow is the number of bytes downloaded so far
311
311
// $dlSize is the total size to be downloaded or -1 if it is unknown
@@ -330,7 +330,7 @@ The response returned by all HTTP clients is an object of type
330
330
:class: `Symfony\\ Contracts\\ HttpClient\\ ResponseInterface ` which provides the
331
331
following methods::
332
332
333
- $response = $httpClient ->request('GET', 'https://...');
333
+ $response = $client ->request('GET', 'https://...');
334
334
335
335
// gets the HTTP status code of the response
336
336
$statusCode = $response->getStatusCode();
@@ -369,7 +369,7 @@ Call the ``stream()`` method of the HTTP client to get *chunks* of the
369
369
response sequentially instead of waiting for the entire response::
370
370
371
371
$url = 'https://releases.ubuntu.com/18.04.1/ubuntu-18.04.1-desktop-amd64.iso';
372
- $response = $httpClient ->request('GET', $url, [
372
+ $response = $client ->request('GET', $url, [
373
373
// optional: if you don't want to buffer the response in memory
374
374
'buffer' => false,
375
375
]);
@@ -382,7 +382,7 @@ response sequentially instead of waiting for the entire response::
382
382
// get the response contents in chunk and save them in a file
383
383
// response chunks implement Symfony\Contracts\HttpClient\ChunkInterface
384
384
$fileHandler = fopen('/ubuntu.iso', 'w');
385
- foreach ($httpClient ->stream($response) as $chunk) {
385
+ foreach ($client ->stream($response) as $chunk) {
386
386
fwrite($fileHandler, $chunk->getContent());
387
387
}
388
388
@@ -416,7 +416,7 @@ When the HTTP status code of the response is in the 300-599 range (i.e. 3xx,
416
416
``getHeaders() `` and ``getContent() `` methods throw an appropriate exception::
417
417
418
418
// the response of this request will be a 403 HTTP error
419
- $response = $httpClient ->request('GET', 'https://httpbin.org/status/403');
419
+ $response = $client ->request('GET', 'https://httpbin.org/status/403');
420
420
421
421
// this code results in a Symfony\Component\HttpClient\Exception\ClientException
422
422
// because it doesn't check the status code of the response
@@ -611,25 +611,42 @@ class to autoconfigure the HTTP client based on the requested URL::
611
611
'Authorization' => 'token '.$githubToken,
612
612
],
613
613
],
614
+ // ...
614
615
]);
615
616
617
+ You can define several scopes, so that each set of options is added only if a
618
+ requested URL matches one of the regular expressions provided as keys.
619
+
616
620
If the request URL is relative (because you use the ``base_uri `` option), the
617
621
scoping HTTP client can't make a match. That's why you can define a third
618
622
optional argument in its constructor which will be considered the default
619
623
regular expression applied to relative URLs::
620
624
621
625
// ...
622
626
623
- $httpClient = new ScopingHttpClient($client, [
624
- 'https://api\.github\.com/' => [
625
- 'base_uri' => 'https://api.github.com/',
626
- // ...
627
+ $client = new ScopingHttpClient($client,
628
+ [
629
+ 'https://api\.github\.com/' => [
630
+ 'base_uri' => 'https://api.github.com/',
631
+ // ...
632
+ ],
627
633
],
628
- ],
629
- // this is the regexp applied to all relative URLs
634
+ // this is the index in the previous array that defines
635
+ // the base URI that shoud be used to resolve relative URLs
630
636
'https://api\.github\.com/'
631
637
);
632
638
639
+ The above example can be reduced to a simpler call::
640
+
641
+ // ...
642
+
643
+ $client = ScopingHttpClient::forBaseUri($client, 'https://api.github.com/', [
644
+ // ...
645
+ ]);
646
+
647
+ This way, the provided options will be used only if the requested URL is relative
648
+ or if it matches the ``https://api.github.com/ `` base URI.
649
+
633
650
Interoperability
634
651
----------------
635
652
@@ -811,11 +828,11 @@ into any services by type-hinting a constructor argument with the
811
828
812
829
class SomeService
813
830
{
814
- private $httpClient ;
831
+ private $client ;
815
832
816
- public function __construct(HttpClientInterface $httpClient )
833
+ public function __construct(HttpClientInterface $client )
817
834
{
818
- $this->httpClient = $httpClient ;
835
+ $this->client = $client ;
819
836
}
820
837
}
821
838
0 commit comments