@@ -19,8 +19,10 @@ supports synchronous and asynchronous operations. You can install it with:
19
19
Basic Usage
20
20
-----------
21
21
22
- Use the :class: `Symfony\\ Component\\ HttpClient\\ HttpClient ` class to create the
23
- low-level HTTP client that makes requests, like the following ``GET `` request:
22
+ Use the :class: `Symfony\\ Component\\ HttpClient\\ HttpClient ` class to make
23
+ requests. In the Symfony framework, this class is available as the
24
+ ``http_client `` service. This service will be :doc: `autowired </service_container/autowiring >`
25
+ automatically when type-hinting for :class: `Symfony\\ Component\\ HttpClient\\ HttpClientInterface `:
24
26
25
27
.. configuration-block ::
26
28
@@ -73,38 +75,11 @@ low-level HTTP client that makes requests, like the following ``GET`` request:
73
75
$content = $response->toArray();
74
76
// $content = ['id' => 521583, 'name' => 'symfony-docs', ...]
75
77
76
- In the Symfony framework, you have to enable the HTTP client integration in
77
- order for the ``HttpClientInterface `` to be :doc: `autowired </service_container/autowiring >`
78
- automatically:
79
-
80
- .. configuration-block ::
81
-
82
- .. code-block :: yaml
83
-
84
- # config/packages/framework.yaml
85
- framework :
86
- http_client : true
87
-
88
- .. code-block :: xml
89
-
90
- <!-- config/packages/framework.xml -->
91
- <?xml version =" 1.0" encoding =" UTF-8" ?>
92
- <container xmlns =" http://symfony.com/schema/dic/services"
93
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
94
- xmlns : framework =" http://symfony.com/schema/dic/symfony"
95
- xsi : schemaLocation =" http://symfony.com/schema/dic/services
96
- https://symfony.com/schema/dic/services/services-1.0.xsd
97
- http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
78
+ .. tip ::
98
79
99
- <framework : config http-client =" true" />
100
- </container >
101
-
102
- .. code-block :: php
103
-
104
- // config/packages/framework.php
105
- $container->loadFromExtension('framework', [
106
- 'http_client' => true,
107
- ]);
80
+ The HTTP client is interopable with many common HTTP client abstractions in
81
+ PHP. You can also use any of these abstractions to profit from autowirings.
82
+ See `Interoperability `_ for more information.
108
83
109
84
Configuration
110
85
-------------
@@ -1197,17 +1172,42 @@ To use it, you need the ``psr/http-client`` package and a `PSR-17`_ implementati
1197
1172
# any already installed implementations from common vendors:
1198
1173
# composer require php-http/discovery
1199
1174
1200
- Now you can make HTTP requests with the PSR-18 client as follows::
1175
+ Now you can make HTTP requests with the PSR-18 client as follows:
1176
+
1177
+ .. configuration-block ::
1178
+
1179
+ .. code-block :: php-symfony
1180
+
1181
+ use Psr\Http\Client\ClientInterface;
1182
+
1183
+ class Symfony
1184
+ {
1185
+ private $client;
1186
+
1187
+ public function __construct(ClientInterface $client)
1188
+ {
1189
+ $this->client = $client;
1190
+ }
1191
+
1192
+ public function getAvailableVersions(): array
1193
+ {
1194
+ $request = $this->client->createRequest('GET', 'https://symfony.com/versions.json');
1195
+ $response = $this->client->sendRequest($request);
1196
+
1197
+ return json_decode($response->getBody()->getContents(), true);
1198
+ }
1199
+ }
1200
+
1201
+ .. code-block :: php-standalone
1201
1202
1202
- use Symfony\Component\HttpClient\Psr18Client;
1203
+ use Symfony\Component\HttpClient\Psr18Client;
1203
1204
1204
- $client = new Psr18Client();
1205
+ $client = new Psr18Client();
1205
1206
1206
- $url = 'https://symfony.com/versions.json';
1207
- $request = $client->createRequest('GET', $url);
1208
- $response = $client->sendRequest($request);
1207
+ $request = $client->createRequest('GET', 'https://symfony.com/versions.json');
1208
+ $response = $client->sendRequest($request);
1209
1209
1210
- $content = json_decode($response->getBody()->getContents(), true);
1210
+ $content = json_decode($response->getBody()->getContents(), true);
1211
1211
1212
1212
HTTPlug
1213
1213
~~~~~~~
0 commit comments