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