@@ -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-------------
@@ -1186,17 +1161,42 @@ To use it, you need the ``psr/http-client`` package and a `PSR-17`_ implementati
11861161 # any already installed implementations from common vendors:
11871162 # composer require php-http/discovery
11881163
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
11901191
1191- use Symfony\Component\HttpClient\Psr18Client;
1192+ use Symfony\Component\HttpClient\Psr18Client;
11921193
1193- $client = new Psr18Client();
1194+ $client = new Psr18Client();
11941195
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);
11981198
1199- $content = json_decode($response->getBody()->getContents(), true);
1199+ $content = json_decode($response->getBody()->getContents(), true);
12001200
12011201 HTTPlug
12021202~~~~~~~
0 commit comments