Skip to content

Commit 2e3a3fd

Browse files
committed
Merge branch '4.3' into 4.4
* 4.3: [HttpClient] add section about interop + performance
2 parents d67e91a + 4ff6247 commit 2e3a3fd

File tree

1 file changed

+61
-9
lines changed

1 file changed

+61
-9
lines changed

components/http_client.rst

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,18 @@ low-level HTTP client that makes requests, like the following ``GET`` request::
4343
$content = $response->toArray();
4444
// $content = ['id' => 521583, 'name' => 'symfony-docs', ...]
4545

46+
Performance
47+
-----------
48+
49+
The component is built for maximum HTTP performance. By design, it is compatible
50+
with HTTP/2 and with doing concurrent asynchronous streamed and multiplexed
51+
requests/responses. Even when doing regular synchronous calls, this design
52+
allows keeping connections to remote hosts open between requests, improving
53+
performance by saving repetitive DNS resolution, SSL negotiation, etc.
54+
To leverage all these design benefits, the cURL extension is needed.
55+
4656
Enabling cURL Support
47-
---------------------
57+
~~~~~~~~~~~~~~~~~~~~~
4858

4959
This component supports both the native PHP streams and cURL to make the HTTP
5060
requests. Although both are interchangeable and provide the same features,
@@ -68,7 +78,7 @@ not configurable and cURL will be used automatically if the cURL PHP extension
6878
is installed and enabled. Otherwise, the native PHP streams will be used.
6979

7080
HTTP/2 Support
71-
--------------
81+
~~~~~~~~~~~~~~
7282

7383
When requesting an ``https`` URL, HTTP/2 is enabled by default if libcurl >= 7.36
7484
is used. To force HTTP/2 for ``http`` URLs, you need to enable it explicitly via
@@ -609,14 +619,55 @@ regular expression applied to relative URLs::
609619
'https://api\.github\.com/'
610620
);
611621

612-
PSR-18 Compatibility
613-
--------------------
622+
Interoperability
623+
----------------
624+
625+
The component is interoperable with two different abstractions for HTTP clients:
626+
`Symfony Contracts`_ and `PSR-18`_. If your application uses libraries that need
627+
any of them, the component is compatible with both. They also benefit from
628+
:ref:`autowiring aliases <service-autowiring-alias>` when the
629+
:ref:`framework bundle <framework-bundle-configuration>` is used.
630+
631+
If you are writing or maintaining a library that makes HTTP requests, you can
632+
decouple it from any specific HTTP client implementations by coding against
633+
either Symfony Contracts (recommended) or PSR-18.
634+
635+
Symfony Contracts
636+
~~~~~~~~~~~~~~~~~
637+
638+
The interfaces found in the ``symfony/http-client-contracts`` package define
639+
the primary abstractions implemented by the component. Its entry point is the
640+
:class:`Symfony\\Contracts\\HttpClient\\HttpClientInterface`. That's the
641+
interface you need to code against when a client is needed::
642+
643+
use Symfony\Contracts\HttpClient\HttpClientInterface;
644+
645+
class MyApiLayer
646+
{
647+
private $client;
648+
649+
public function __construct(HttpClientInterface $client)
650+
{
651+
$this->client = $client
652+
}
653+
654+
// [...]
655+
}
656+
657+
All request options mentioned above (e.g. timeout management) are also defined
658+
in the wordings of the interface, so that any compliant implementations (like
659+
this component) is guaranteed to provide them. That's a major difference with
660+
the PSR-18 abstraction, which provides none related to the transport itself.
661+
662+
Another major feature covered by the Symfony Contracts is async/multiplexing,
663+
as described in the previous sections.
664+
665+
PSR-18
666+
~~~~~~
614667

615-
This component uses and implements abstractions defined by the
616-
``symfony/http-client-contracts``. It also implements the `PSR-18`_ (HTTP Client)
617-
specifications via the :class:`Symfony\\Component\\HttpClient\\Psr18Client`
618-
class, which is an adapter to turn a Symfony ``HttpClientInterface`` into a
619-
PSR-18 ``ClientInterface``.
668+
This component implements the `PSR-18`_ (HTTP Client) specifications via the
669+
:class:`Symfony\\Component\\HttpClient\\Psr18Client` class, which is an adapter
670+
to turn a Symfony ``HttpClientInterface`` into a PSR-18 ``ClientInterface``.
620671

621672
To use it, you need the ``psr/http-client`` package and a `PSR-17`_ implementation:
622673

@@ -765,3 +816,4 @@ However, using ``MockResponse`` allows simulating chunked responses and timeouts
765816
.. _`cURL PHP extension`: https://php.net/curl
766817
.. _`PSR-17`: https://www.php-fig.org/psr/psr-17/
767818
.. _`PSR-18`: https://www.php-fig.org/psr/psr-18/
819+
.. _`Symfony Contracts`: https://github.com/symfony/contracts

0 commit comments

Comments
 (0)