@@ -26,8 +26,8 @@ low-level HTTP client that makes requests, like the following ``GET`` request::
26
26
27
27
use Symfony\Component\HttpClient\HttpClient;
28
28
29
- $httpClient = HttpClient::create();
30
- $response = $httpClient ->request('GET', 'https://api.github.com/repos/symfony/symfony-docs');
29
+ $client = HttpClient::create();
30
+ $response = $client ->request('GET', 'https://api.github.com/repos/symfony/symfony-docs');
31
31
32
32
$statusCode = $response->getStatusCode();
33
33
// $statusCode = 200
@@ -63,10 +63,10 @@ the transport explicitly, use the following classes to create the client::
63
63
use Symfony\Component\HttpClient\NativeHttpClient;
64
64
65
65
// uses native PHP streams
66
- $httpClient = new NativeHttpClient();
66
+ $client = new NativeHttpClient();
67
67
68
68
// uses the cURL PHP extension
69
- $httpClient = new CurlHttpClient();
69
+ $client = new CurlHttpClient();
70
70
71
71
When using this component in a full-stack Symfony application, this behavior is
72
72
not configurable and cURL will be used automatically if the cURL PHP extension
@@ -79,7 +79,7 @@ When requesting an ``https`` URL, HTTP/2 is enabled by default if libcurl >= 7.3
79
79
is used. To force HTTP/2 for ``http `` URLs, you need to enable it explicitly via
80
80
the ``http_version `` option::
81
81
82
- $httpClient = HttpClient::create(['http_version' => '2.0']);
82
+ $client = HttpClient::create(['http_version' => '2.0']);
83
83
84
84
Support for HTTP/2 PUSH works out of the box when libcurl >= 7.61 is used with
85
85
PHP >= 7.2.17 / 7.3.4: pushed responses are put into a temporary cache and are
@@ -91,16 +91,16 @@ Making Requests
91
91
The client created with the ``HttpClient `` class provides a single ``request() ``
92
92
method to perform all kinds of HTTP requests::
93
93
94
- $response = $httpClient ->request('GET', 'https://...');
95
- $response = $httpClient ->request('POST', 'https://...');
96
- $response = $httpClient ->request('PUT', 'https://...');
94
+ $response = $client ->request('GET', 'https://...');
95
+ $response = $client ->request('POST', 'https://...');
96
+ $response = $client ->request('PUT', 'https://...');
97
97
// ...
98
98
99
99
Responses are always asynchronous, so that the call to the method returns
100
100
immediately instead of waiting to receive the response::
101
101
102
102
// code execution continues immediately; it doesn't wait to receive the response
103
- $response = $httpClient ->request('GET', 'http://releases.ubuntu.com/18.04.2/ubuntu-18.04.2-desktop-amd64.iso');
103
+ $response = $client ->request('GET', 'http://releases.ubuntu.com/18.04.2/ubuntu-18.04.2-desktop-amd64.iso');
104
104
105
105
// getting the response headers waits until they arrive
106
106
$contentType = $response->getHeaders()['content-type'][0];
@@ -125,7 +125,7 @@ defined globally when creating the client (to apply it to all requests) and to
125
125
each request (which overrides any global authentication)::
126
126
127
127
// Use the same authentication for all requests
128
- $httpClient = HttpClient::create([
128
+ $client = HttpClient::create([
129
129
// HTTP Basic authentication (there are multiple ways of configuring it)
130
130
'auth_basic' => ['the-username'],
131
131
'auth_basic' => ['the-username', 'the-password'],
@@ -140,7 +140,7 @@ each request (which overrides any global authentication)::
140
140
'auth_ntlm' => 'the-username:the-password',
141
141
]);
142
142
143
- $response = $httpClient ->request('GET', 'https://...', [
143
+ $response = $client ->request('GET', 'https://...', [
144
144
// use a different HTTP Basic authentication only for this request
145
145
'auth_basic' => ['the-username', 'the-password'],
146
146
@@ -158,7 +158,7 @@ You can either append them manually to the requested URL, or define them as an
158
158
associative array via the ``query `` option, that will be merged with the URL::
159
159
160
160
// it makes an HTTP GET request to https://httpbin.org/get?token=...&name=...
161
- $response = $httpClient ->request('GET', 'https://httpbin.org/get', [
161
+ $response = $client ->request('GET', 'https://httpbin.org/get', [
162
162
// these values are automatically encoded before including them in the URL
163
163
'query' => [
164
164
'token' => '...',
@@ -173,13 +173,13 @@ Use the ``headers`` option to define both the default headers added to all
173
173
requests and the specific headers for each request::
174
174
175
175
// this header is added to all requests made by this client
176
- $httpClient = HttpClient::create(['headers' => [
176
+ $client = HttpClient::create(['headers' => [
177
177
'User-Agent' => 'My Fancy App',
178
178
]]);
179
179
180
180
// this header is only included in this request and overrides the value
181
181
// of the same header if defined globally by the HTTP client
182
- $response = $httpClient ->request('POST', 'https://...', [
182
+ $response = $client ->request('POST', 'https://...', [
183
183
'headers' => [
184
184
'Content-Type' => 'text/plain',
185
185
],
@@ -192,7 +192,7 @@ This component provides several methods for uploading data using the ``body``
192
192
option. You can use regular strings, closures, iterables and resources and they'll be
193
193
processed automatically when making the requests::
194
194
195
- $response = $httpClient ->request('POST', 'https://...', [
195
+ $response = $client ->request('POST', 'https://...', [
196
196
// defining data using a regular string
197
197
'body' => 'raw data',
198
198
@@ -225,7 +225,7 @@ A generator or any ``Traversable`` can also be used instead of a closure.
225
225
given content will be JSON-encoded automatically and the request will add the
226
226
``Content-Type: application/json `` automatically too::
227
227
228
- $response = $httpClient ->request('POST', 'https://...', [
228
+ $response = $client ->request('POST', 'https://...', [
229
229
'json' => ['param1' => 'value1', '...'],
230
230
]);
231
231
@@ -268,7 +268,7 @@ making a request. Use the ``max_redirects`` setting to configure this behavior
268
268
(if the number of redirects is higher than the configured value, you'll get a
269
269
:class: `Symfony\\ Component\\ HttpClient\\ Exception\\ RedirectionException `)::
270
270
271
- $response = $httpClient ->request('GET', 'https://...', [
271
+ $response = $client ->request('GET', 'https://...', [
272
272
// 0 means to not follow any redirect
273
273
'max_redirects' => 0,
274
274
]);
@@ -297,7 +297,7 @@ uploads/downloads as they complete. This callback is guaranteed to be called on
297
297
DNS resolution, on arrival of headers and on completion; additionally it is
298
298
called when new data is uploaded or downloaded and at least once per second::
299
299
300
- $response = $httpClient ->request('GET', 'https://...', [
300
+ $response = $client ->request('GET', 'https://...', [
301
301
'on_progress' => function (int $dlNow, int $dlSize, array $info): void {
302
302
// $dlNow is the number of bytes downloaded so far
303
303
// $dlSize is the total size to be downloaded or -1 if it is unknown
@@ -322,7 +322,7 @@ The response returned by all HTTP clients is an object of type
322
322
:class: `Symfony\\ Contracts\\ HttpClient\\ ResponseInterface ` which provides the
323
323
following methods::
324
324
325
- $response = $httpClient ->request('GET', 'https://...');
325
+ $response = $client ->request('GET', 'https://...');
326
326
327
327
// gets the HTTP status code of the response
328
328
$statusCode = $response->getStatusCode();
@@ -361,7 +361,7 @@ Call the ``stream()`` method of the HTTP client to get *chunks* of the
361
361
response sequentially instead of waiting for the entire response::
362
362
363
363
$url = 'https://releases.ubuntu.com/18.04.1/ubuntu-18.04.1-desktop-amd64.iso';
364
- $response = $httpClient ->request('GET', $url, [
364
+ $response = $client ->request('GET', $url, [
365
365
// optional: if you don't want to buffer the response in memory
366
366
'buffer' => false,
367
367
]);
@@ -374,7 +374,7 @@ response sequentially instead of waiting for the entire response::
374
374
// get the response contents in chunk and save them in a file
375
375
// response chunks implement Symfony\Contracts\HttpClient\ChunkInterface
376
376
$fileHandler = fopen('/ubuntu.iso', 'w');
377
- foreach ($httpClient ->stream($response) as $chunk) {
377
+ foreach ($client ->stream($response) as $chunk) {
378
378
fwrite($fileHandler, $chunk->getContent());
379
379
}
380
380
@@ -408,7 +408,7 @@ When the HTTP status code of the response is in the 300-599 range (i.e. 3xx,
408
408
``getHeaders() `` and ``getContent() `` methods throw an appropriate exception::
409
409
410
410
// the response of this request will be a 403 HTTP error
411
- $response = $httpClient ->request('GET', 'https://httpbin.org/status/403');
411
+ $response = $client ->request('GET', 'https://httpbin.org/status/403');
412
412
413
413
// this code results in a Symfony\Component\HttpClient\Exception\ClientException
414
414
// because it doesn't check the status code of the response
@@ -603,25 +603,42 @@ class to autoconfigure the HTTP client based on the requested URL::
603
603
'Authorization' => 'token '.$githubToken,
604
604
],
605
605
],
606
+ // ...
606
607
]);
607
608
609
+ You can define several scopes, so that each set of options is added only if a
610
+ requested URL matches one of the regular expressions provided as keys.
611
+
608
612
If the request URL is relative (because you use the ``base_uri `` option), the
609
613
scoping HTTP client can't make a match. That's why you can define a third
610
614
optional argument in its constructor which will be considered the default
611
615
regular expression applied to relative URLs::
612
616
613
617
// ...
614
618
615
- $httpClient = new ScopingHttpClient($client, [
616
- 'https://api\.github\.com/' => [
617
- 'base_uri' => 'https://api.github.com/',
618
- // ...
619
+ $client = new ScopingHttpClient($client,
620
+ [
621
+ 'https://api\.github\.com/' => [
622
+ 'base_uri' => 'https://api.github.com/',
623
+ // ...
624
+ ],
619
625
],
620
- ],
621
- // this is the regexp applied to all relative URLs
626
+ // this is the index in the previous array that defines
627
+ // the base URI that shoud be used to resolve relative URLs
622
628
'https://api\.github\.com/'
623
629
);
624
630
631
+ The above example can be reduced to a simpler call::
632
+
633
+ // ...
634
+
635
+ $client = ScopingHttpClient::forBaseUri($client, 'https://api.github.com/', [
636
+ // ...
637
+ ]);
638
+
639
+ This way, the provided options will be used only if the requested URL is relative
640
+ or if it matches the ``https://api.github.com/ `` base URI.
641
+
625
642
Interoperability
626
643
----------------
627
644
@@ -795,11 +812,11 @@ into any services by type-hinting a constructor argument with the
795
812
796
813
class SomeService
797
814
{
798
- private $httpClient ;
815
+ private $client ;
799
816
800
- public function __construct(HttpClientInterface $httpClient )
817
+ public function __construct(HttpClientInterface $client )
801
818
{
802
- $this->httpClient = $httpClient ;
819
+ $this->client = $client ;
803
820
}
804
821
}
805
822
0 commit comments