Skip to content

Commit 2a80f01

Browse files
committed
minor symfony#13937 [HttpClient] HTTP client is enabled by default when installed (wouterj)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] HTTP client is enabled by default when installed @nicolas-grekas explained that the component is enabled by default in the Symfony framework. This also improves some wordings for the interopability interfaces, to document all available autowiring aliases. Commits ------- ca6f95f HTTP client is enabled by default when installed
2 parents 82536e7 + ca6f95f commit 2a80f01

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

http_client.rst

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ supports synchronous and asynchronous operations. You can install it with:
2323
Basic Usage
2424
-----------
2525

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`:
2830

2931
.. configuration-block::
3032

@@ -77,38 +79,11 @@ low-level HTTP client that makes requests, like the following ``GET`` request:
7779
$content = $response->toArray();
7880
// $content = ['id' => 521583, 'name' => 'symfony-docs', ...]
7981
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::
10283

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.
11287

11388
Configuration
11489
-------------
@@ -1202,17 +1177,42 @@ To use it, you need the ``psr/http-client`` package and a `PSR-17`_ implementati
12021177
# any already installed implementations from common vendors:
12031178
# composer require php-http/discovery
12041179
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
12061207
1207-
use Symfony\Component\HttpClient\Psr18Client;
1208+
use Symfony\Component\HttpClient\Psr18Client;
12081209
1209-
$client = new Psr18Client();
1210+
$client = new Psr18Client();
12101211
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);
12141214
1215-
$content = json_decode($response->getBody()->getContents(), true);
1215+
$content = json_decode($response->getBody()->getContents(), true);
12161216
12171217
.. versionadded:: 4.4
12181218

0 commit comments

Comments
 (0)