From ca6f95f9ce2a8b824353ae2f09322e9b49252ba5 Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Sat, 4 Jul 2020 13:51:28 +0200 Subject: [PATCH] HTTP client is enabled by default when installed This also improves some wordings for the interopability interfaces, to document all autowiring aliases. --- http_client.rst | 80 ++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/http_client.rst b/http_client.rst index a3ff12e0a1a..e3feb5a873b 100644 --- a/http_client.rst +++ b/http_client.rst @@ -23,8 +23,10 @@ supports synchronous and asynchronous operations. You can install it with: Basic Usage ----------- -Use the :class:`Symfony\\Component\\HttpClient\\HttpClient` class to create the -low-level HTTP client that makes requests, like the following ``GET`` request: +Use the :class:`Symfony\\Component\\HttpClient\\HttpClient` class to make +requests. In the Symfony framework, this class is available as the +``http_client`` service. This service will be :doc:`autowired ` +automatically when type-hinting for :class:`Symfony\\Component\\HttpClient\\HttpClientInterface`: .. configuration-block:: @@ -77,38 +79,11 @@ low-level HTTP client that makes requests, like the following ``GET`` request: $content = $response->toArray(); // $content = ['id' => 521583, 'name' => 'symfony-docs', ...] -In the Symfony framework, you have to enable the HTTP client integration in -order for the ``HttpClientInterface`` to be :doc:`autowired ` -automatically: - -.. configuration-block:: - - .. code-block:: yaml - - # config/packages/framework.yaml - framework: - http_client: true - - .. code-block:: xml - - - - +.. tip:: - - - - .. code-block:: php - - // config/packages/framework.php - $container->loadFromExtension('framework', [ - 'http_client' => true, - ]); + The HTTP client is interopable with many common HTTP client abstractions in + PHP. You can also use any of these abstractions to profit from autowirings. + See `Interoperability`_ for more information. Configuration ------------- @@ -1202,17 +1177,42 @@ To use it, you need the ``psr/http-client`` package and a `PSR-17`_ implementati # any already installed implementations from common vendors: # composer require php-http/discovery -Now you can make HTTP requests with the PSR-18 client as follows:: +Now you can make HTTP requests with the PSR-18 client as follows: + +.. configuration-block:: + + .. code-block:: php-symfony + + use Psr\Http\Client\ClientInterface; + + class Symfony + { + private $client; + + public function __construct(ClientInterface $client) + { + $this->client = $client; + } + + public function getAvailableVersions(): array + { + $request = $this->client->createRequest('GET', 'https://symfony.com/versions.json'); + $response = $this->client->sendRequest($request); + + return json_decode($response->getBody()->getContents(), true); + } + } + + .. code-block:: php-standalone - use Symfony\Component\HttpClient\Psr18Client; + use Symfony\Component\HttpClient\Psr18Client; - $client = new Psr18Client(); + $client = new Psr18Client(); - $url = 'https://symfony.com/versions.json'; - $request = $client->createRequest('GET', $url); - $response = $client->sendRequest($request); + $request = $client->createRequest('GET', 'https://symfony.com/versions.json'); + $response = $client->sendRequest($request); - $content = json_decode($response->getBody()->getContents(), true); + $content = json_decode($response->getBody()->getContents(), true); .. versionadded:: 4.4