@@ -30,8 +30,8 @@ low-level HTTP client that makes requests, like the following ``GET`` request::
3030
3131 use Symfony\Component\HttpClient\HttpClient;
3232
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');
3535
3636 $statusCode = $response->getStatusCode();
3737 // $statusCode = 200
@@ -67,10 +67,10 @@ the transport explicitly, use the following classes to create the client::
6767 use Symfony\Component\HttpClient\NativeHttpClient;
6868
6969 // uses native PHP streams
70- $httpClient = new NativeHttpClient();
70+ $client = new NativeHttpClient();
7171
7272 // uses the cURL PHP extension
73- $httpClient = new CurlHttpClient();
73+ $client = new CurlHttpClient();
7474
7575When using this component in a full-stack Symfony application, this behavior is
7676not 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
8383is used. To force HTTP/2 for ``http `` URLs, you need to enable it explicitly via
8484the ``http_version `` option::
8585
86- $httpClient = HttpClient::create(['http_version' => '2.0']);
86+ $client = HttpClient::create(['http_version' => '2.0']);
8787
8888Support for HTTP/2 PUSH works out of the box when libcurl >= 7.61 is used with
8989PHP >= 7.2.17 / 7.3.4: pushed responses are put into a temporary cache and are
@@ -95,16 +95,16 @@ Making Requests
9595The client created with the ``HttpClient `` class provides a single ``request() ``
9696method to perform all kinds of HTTP requests::
9797
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://...');
101101 // ...
102102
103103Responses are always asynchronous, so that the call to the method returns
104104immediately instead of waiting to receive the response::
105105
106106 // 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');
108108
109109 // getting the response headers waits until they arrive
110110 $contentType = $response->getHeaders()['content-type'][0];
@@ -129,7 +129,7 @@ defined globally when creating the client (to apply it to all requests) and to
129129each request (which overrides any global authentication)::
130130
131131 // Use the same authentication for all requests
132- $httpClient = HttpClient::create([
132+ $client = HttpClient::create([
133133 // HTTP Basic authentication (there are multiple ways of configuring it)
134134 'auth_basic' => ['the-username'],
135135 'auth_basic' => ['the-username', 'the-password'],
@@ -144,7 +144,7 @@ each request (which overrides any global authentication)::
144144 'auth_ntlm' => 'the-username:the-password',
145145 ]);
146146
147- $response = $httpClient ->request('GET', 'https://...', [
147+ $response = $client ->request('GET', 'https://...', [
148148 // use a different HTTP Basic authentication only for this request
149149 'auth_basic' => ['the-username', 'the-password'],
150150
@@ -166,7 +166,7 @@ You can either append them manually to the requested URL, or define them as an
166166associative array via the ``query `` option, that will be merged with the URL::
167167
168168 // 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', [
170170 // these values are automatically encoded before including them in the URL
171171 'query' => [
172172 'token' => '...',
@@ -181,13 +181,13 @@ Use the ``headers`` option to define both the default headers added to all
181181requests and the specific headers for each request::
182182
183183 // this header is added to all requests made by this client
184- $httpClient = HttpClient::create(['headers' => [
184+ $client = HttpClient::create(['headers' => [
185185 'User-Agent' => 'My Fancy App',
186186 ]]);
187187
188188 // this header is only included in this request and overrides the value
189189 // of the same header if defined globally by the HTTP client
190- $response = $httpClient ->request('POST', 'https://...', [
190+ $response = $client ->request('POST', 'https://...', [
191191 'headers' => [
192192 'Content-Type' => 'text/plain',
193193 ],
@@ -200,7 +200,7 @@ This component provides several methods for uploading data using the ``body``
200200option. You can use regular strings, closures, iterables and resources and they'll be
201201processed automatically when making the requests::
202202
203- $response = $httpClient ->request('POST', 'https://...', [
203+ $response = $client ->request('POST', 'https://...', [
204204 // defining data using a regular string
205205 'body' => 'raw data',
206206
@@ -233,7 +233,7 @@ A generator or any ``Traversable`` can also be used instead of a closure.
233233 given content will be JSON-encoded automatically and the request will add the
234234 ``Content-Type: application/json `` automatically too::
235235
236- $response = $httpClient ->request('POST', 'https://...', [
236+ $response = $client ->request('POST', 'https://...', [
237237 'json' => ['param1' => 'value1', '...'],
238238 ]);
239239
@@ -276,7 +276,7 @@ making a request. Use the ``max_redirects`` setting to configure this behavior
276276(if the number of redirects is higher than the configured value, you'll get a
277277:class: `Symfony\\ Component\\ HttpClient\\ Exception\\ RedirectionException `)::
278278
279- $response = $httpClient ->request('GET', 'https://...', [
279+ $response = $client ->request('GET', 'https://...', [
280280 // 0 means to not follow any redirect
281281 'max_redirects' => 0,
282282 ]);
@@ -305,7 +305,7 @@ uploads/downloads as they complete. This callback is guaranteed to be called on
305305DNS resolution, on arrival of headers and on completion; additionally it is
306306called when new data is uploaded or downloaded and at least once per second::
307307
308- $response = $httpClient ->request('GET', 'https://...', [
308+ $response = $client ->request('GET', 'https://...', [
309309 'on_progress' => function (int $dlNow, int $dlSize, array $info): void {
310310 // $dlNow is the number of bytes downloaded so far
311311 // $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
330330:class: `Symfony\\ Contracts\\ HttpClient\\ ResponseInterface ` which provides the
331331following methods::
332332
333- $response = $httpClient ->request('GET', 'https://...');
333+ $response = $client ->request('GET', 'https://...');
334334
335335 // gets the HTTP status code of the response
336336 $statusCode = $response->getStatusCode();
@@ -369,7 +369,7 @@ Call the ``stream()`` method of the HTTP client to get *chunks* of the
369369response sequentially instead of waiting for the entire response::
370370
371371 $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, [
373373 // optional: if you don't want to buffer the response in memory
374374 'buffer' => false,
375375 ]);
@@ -382,7 +382,7 @@ response sequentially instead of waiting for the entire response::
382382 // get the response contents in chunk and save them in a file
383383 // response chunks implement Symfony\Contracts\HttpClient\ChunkInterface
384384 $fileHandler = fopen('/ubuntu.iso', 'w');
385- foreach ($httpClient ->stream($response) as $chunk) {
385+ foreach ($client ->stream($response) as $chunk) {
386386 fwrite($fileHandler, $chunk->getContent());
387387 }
388388
@@ -416,7 +416,7 @@ When the HTTP status code of the response is in the 300-599 range (i.e. 3xx,
416416``getHeaders() `` and ``getContent() `` methods throw an appropriate exception::
417417
418418 // 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');
420420
421421 // this code results in a Symfony\Component\HttpClient\Exception\ClientException
422422 // 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::
611611 'Authorization' => 'token '.$githubToken,
612612 ],
613613 ],
614+ // ...
614615 ]);
615616
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+
616620If the request URL is relative (because you use the ``base_uri `` option), the
617621scoping HTTP client can't make a match. That's why you can define a third
618622optional argument in its constructor which will be considered the default
619623regular expression applied to relative URLs::
620624
621625 // ...
622626
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+ ],
627633 ],
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
630636 'https://api\.github\.com/'
631637 );
632638
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+
633650Interoperability
634651----------------
635652
@@ -811,11 +828,11 @@ into any services by type-hinting a constructor argument with the
811828
812829 class SomeService
813830 {
814- private $httpClient ;
831+ private $client ;
815832
816- public function __construct(HttpClientInterface $httpClient )
833+ public function __construct(HttpClientInterface $client )
817834 {
818- $this->httpClient = $httpClient ;
835+ $this->client = $client ;
819836 }
820837 }
821838
0 commit comments