Skip to content

Commit 0ad95cc

Browse files
committed
minor #11820 [HttpClient] add words about HTTPlug (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] add words about HTTPlug Fixes #11809, #11724 and #11721 Some parts could be moved to branch 4.3. Commits ------- 790446d [HttpClient] add words about HTTPlug
2 parents 2e3a3fd + 790446d commit 0ad95cc

File tree

1 file changed

+70
-13
lines changed

1 file changed

+70
-13
lines changed

components/http_client.rst

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -622,15 +622,15 @@ regular expression applied to relative URLs::
622622
Interoperability
623623
----------------
624624

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.
625+
The component is interoperable with three different abstractions for HTTP
626+
clients: `Symfony Contracts`_, `PSR-18`_ and `HTTPlug`_ v1 and v2. If your
627+
application uses libraries that need any of them, the component is compatible
628+
with all of them. They also benefit from :ref:`autowiring aliases <service-autowiring-alias>`
629+
when the :ref:`framework bundle <framework-bundle-configuration>` is used.
630630

631631
If you are writing or maintaining a library that makes HTTP requests, you can
632632
decouple it from any specific HTTP client implementations by coding against
633-
either Symfony Contracts (recommended) or PSR-18.
633+
either Symfony Contracts (recommended) or PSR-18 (which superseded HTTPlug).
634634

635635
Symfony Contracts
636636
~~~~~~~~~~~~~~~~~
@@ -662,12 +662,14 @@ the PSR-18 abstraction, which provides none related to the transport itself.
662662
Another major feature covered by the Symfony Contracts is async/multiplexing,
663663
as described in the previous sections.
664664

665-
PSR-18
666-
~~~~~~
665+
PSR-18 and PSR-17
666+
~~~~~~~~~~~~~~~~~
667667

668668
This component implements the `PSR-18`_ (HTTP Client) specifications via the
669669
:class:`Symfony\\Component\\HttpClient\\Psr18Client` class, which is an adapter
670670
to turn a Symfony ``HttpClientInterface`` into a PSR-18 ``ClientInterface``.
671+
This class also implements the relevant methods of `PSR-17`_ to ease creating
672+
request objects.
671673

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

@@ -682,18 +684,72 @@ To use it, you need the ``psr/http-client`` package and a `PSR-17`_ implementati
682684
683685
Now you can make HTTP requests with the PSR-18 client as follows::
684686

685-
use Nyholm\Psr7\Factory\Psr17Factory;
686687
use Symfony\Component\HttpClient\Psr18Client;
687688

688-
$psr17Factory = new Psr17Factory();
689-
$psr18Client = new Psr18Client();
689+
$client = new Psr18Client();
690690

691691
$url = 'https://symfony.com/versions.json';
692-
$request = $psr17Factory->createRequest('GET', $url);
693-
$response = $psr18Client->sendRequest($request);
692+
$request = $client->createRequest('GET', $url);
693+
$response = $client->sendRequest($request);
694694

695695
$content = json_decode($response->getBody()->getContents(), true);
696696

697+
.. versionadded:: 4.4
698+
699+
The PSR-17 factory methods of ``Psr18Client`` were introduced in Symfony 4.4.
700+
701+
HTTPlug
702+
~~~~~~~
703+
704+
.. versionadded:: 4.4
705+
706+
Support for HTTPlug was introduced in Symfony 4.4.
707+
708+
The `HTTPlug`_ specification was published before PSR-18 and is superseded by
709+
it. As such, you should not use it in newly written code. Yet, many libraries
710+
still require v1 or v2 of it. The component is interoperable with them thanks to
711+
the ``HttplugClient`` adapter class. Similarly to ``Psr18Client`` implementing
712+
relevant parts of PSR-17, ``HttplugClient`` also implements the factory methods
713+
defined in the related ``php-http/message-factory`` package.
714+
715+
Internally, the implementation relies on the ``Psr18Client``, so that the
716+
``psr/http-client`` package is needed to use this class::
717+
718+
.. code-block:: terminal
719+
720+
# Let's suppose php-http/httplug is already required by the lib you want to use
721+
722+
# installs the PSR-18 ClientInterface
723+
$ composer require psr/http-client
724+
725+
# installs an efficient implementation of response and stream factories
726+
# with autowiring aliases provided by Symfony Flex
727+
$ composer require nyholm/psr7
728+
729+
Let's say you want to instantiate a class with the following constructor,
730+
that requires HTTPlug dependencies::
731+
732+
use Http\Client\HttpClient;
733+
use Http\Message\RequestFactory;
734+
use Http\Message\StreamFactory;
735+
736+
class SomeSdk
737+
{
738+
public function __construct(
739+
HttpClient $httpClient,
740+
RequestFactory $requestFactory,
741+
StreamFactory $streamFactory
742+
)
743+
// [...]
744+
}
745+
746+
Because ``HttplugClient`` implements the three interfaces, you can use it this way::
747+
748+
use Symfony\Component\HttpClient\HttplugClient;
749+
750+
$httpClient = new HttplugClient();
751+
$apiClient = new SomeSdk($httpClient, $httpClient, $httpClient);
752+
697753
Symfony Framework Integration
698754
-----------------------------
699755

@@ -816,4 +872,5 @@ However, using ``MockResponse`` allows simulating chunked responses and timeouts
816872
.. _`cURL PHP extension`: https://php.net/curl
817873
.. _`PSR-17`: https://www.php-fig.org/psr/psr-17/
818874
.. _`PSR-18`: https://www.php-fig.org/psr/psr-18/
875+
.. _`HTTPlug`: https://github.com/php-http/httplug/#readme
819876
.. _`Symfony Contracts`: https://github.com/symfony/contracts

0 commit comments

Comments
 (0)