@@ -19,8 +19,10 @@ supports synchronous and asynchronous operations. You can install it with:
1919 Basic Usage
2020-----------
2121
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 `:
2426
2527.. configuration-block ::
2628
@@ -73,38 +75,11 @@ low-level HTTP client that makes requests, like the following ``GET`` request:
7375 $content = $response->toArray();
7476 // $content = ['id' => 521583, 'name' => 'symfony-docs', ...]
7577
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 ::
9879
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.
10883
10984Configuration
11085-------------
@@ -1197,17 +1172,42 @@ To use it, you need the ``psr/http-client`` package and a `PSR-17`_ implementati
11971172 # any already installed implementations from common vendors:
11981173 # composer require php-http/discovery
11991174
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
12011202
1202- use Symfony\Component\HttpClient\Psr18Client;
1203+ use Symfony\Component\HttpClient\Psr18Client;
12031204
1204- $client = new Psr18Client();
1205+ $client = new Psr18Client();
12051206
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);
12091209
1210- $content = json_decode($response->getBody()->getContents(), true);
1210+ $content = json_decode($response->getBody()->getContents(), true);
12111211
12121212 HTTPlug
12131213~~~~~~~
0 commit comments