@@ -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
-------------
@@ -1186,17 +1161,42 @@ To use it, you need the ``psr/http-client`` package and a `PSR-17`_ implementati
1186
1161
# any already installed implementations from common vendors:
1187
1162
# composer require php-http/discovery
1188
1163
1189
- Now you can make HTTP requests with the PSR-18 client as follows::
1164
+ Now you can make HTTP requests with the PSR-18 client as follows:
1165
+
1166
+ .. configuration-block ::
1167
+
1168
+ .. code-block :: php-symfony
1169
+
1170
+ use Psr\Http\Client\ClientInterface;
1171
+
1172
+ class Symfony
1173
+ {
1174
+ private $client;
1175
+
1176
+ public function __construct(ClientInterface $client)
1177
+ {
1178
+ $this->client = $client;
1179
+ }
1180
+
1181
+ public function getAvailableVersions(): array
1182
+ {
1183
+ $request = $this->client->createRequest('GET', 'https://symfony.com/versions.json');
1184
+ $response = $this->client->sendRequest($request);
1185
+
1186
+ return json_decode($response->getBody()->getContents(), true);
1187
+ }
1188
+ }
1189
+
1190
+ .. code-block :: php-standalone
1190
1191
1191
- use Symfony\Component\HttpClient\Psr18Client;
1192
+ use Symfony\Component\HttpClient\Psr18Client;
1192
1193
1193
- $client = new Psr18Client();
1194
+ $client = new Psr18Client();
1194
1195
1195
- $url = 'https://symfony.com/versions.json';
1196
- $request = $client->createRequest('GET', $url);
1197
- $response = $client->sendRequest($request);
1196
+ $request = $client->createRequest('GET', 'https://symfony.com/versions.json');
1197
+ $response = $client->sendRequest($request);
1198
1198
1199
- $content = json_decode($response->getBody()->getContents(), true);
1199
+ $content = json_decode($response->getBody()->getContents(), true);
1200
1200
1201
1201
HTTPlug
1202
1202
~~~~~~~
0 commit comments