Skip to content

Commit 3217fb4

Browse files
committed
Merge branch '4.4'
* 4.4: [HttpClient] add section about interop + performance
2 parents 3a3c1e3 + 2e3a3fd commit 3217fb4

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
@@ -38,8 +38,18 @@ low-level HTTP client that makes requests, like the following ``GET`` request::
3838
$content = $response->toArray();
3939
// $content = ['id' => 521583, 'name' => 'symfony-docs', ...]
4040

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

4454
This component supports both the native PHP streams and cURL to make the HTTP
4555
requests. Although both are interchangeable and provide the same features,
@@ -63,7 +73,7 @@ not configurable and cURL will be used automatically if the cURL PHP extension
6373
is installed and enabled. Otherwise, the native PHP streams will be used.
6474

6575
HTTP/2 Support
66-
--------------
76+
~~~~~~~~~~~~~~
6777

6878
When requesting an ``https`` URL, HTTP/2 is enabled by default if libcurl >= 7.36
6979
is used. To force HTTP/2 for ``http`` URLs, you need to enable it explicitly via
@@ -604,14 +614,55 @@ regular expression applied to relative URLs::
604614
'https://api\.github\.com/'
605615
);
606616

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

610-
This component uses and implements abstractions defined by the
611-
``symfony/http-client-contracts``. It also implements the `PSR-18`_ (HTTP Client)
612-
specifications via the :class:`Symfony\\Component\\HttpClient\\Psr18Client`
613-
class, which is an adapter to turn a Symfony ``HttpClientInterface`` into a
614-
PSR-18 ``ClientInterface``.
663+
This component implements the `PSR-18`_ (HTTP Client) specifications via the
664+
:class:`Symfony\\Component\\HttpClient\\Psr18Client` class, which is an adapter
665+
to turn a Symfony ``HttpClientInterface`` into a PSR-18 ``ClientInterface``.
615666

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

@@ -760,3 +811,4 @@ However, using ``MockResponse`` allows simulating chunked responses and timeouts
760811
.. _`cURL PHP extension`: https://php.net/curl
761812
.. _`PSR-17`: https://www.php-fig.org/psr/psr-17/
762813
.. _`PSR-18`: https://www.php-fig.org/psr/psr-18/
814+
.. _`Symfony Contracts`: https://github.com/symfony/contracts

0 commit comments

Comments
 (0)