@@ -622,15 +622,15 @@ regular expression applied to relative URLs::
622
622
Interoperability
623
623
----------------
624
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.
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.
630
630
631
631
If you are writing or maintaining a library that makes HTTP requests, you can
632
632
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) .
634
634
635
635
Symfony Contracts
636
636
~~~~~~~~~~~~~~~~~
@@ -662,12 +662,14 @@ the PSR-18 abstraction, which provides none related to the transport itself.
662
662
Another major feature covered by the Symfony Contracts is async/multiplexing,
663
663
as described in the previous sections.
664
664
665
- PSR-18
666
- ~~~~~~
665
+ PSR-18 and PSR-17
666
+ ~~~~~~~~~~~~~~~~~
667
667
668
668
This component implements the `PSR-18 `_ (HTTP Client) specifications via the
669
669
:class: `Symfony\\ Component\\ HttpClient\\ Psr18Client ` class, which is an adapter
670
670
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.
671
673
672
674
To use it, you need the ``psr/http-client `` package and a `PSR-17 `_ implementation:
673
675
@@ -682,18 +684,72 @@ To use it, you need the ``psr/http-client`` package and a `PSR-17`_ implementati
682
684
683
685
Now you can make HTTP requests with the PSR-18 client as follows::
684
686
685
- use Nyholm\Psr7\Factory\Psr17Factory;
686
687
use Symfony\Component\HttpClient\Psr18Client;
687
688
688
- $psr17Factory = new Psr17Factory();
689
- $psr18Client = new Psr18Client();
689
+ $client = new Psr18Client();
690
690
691
691
$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);
694
694
695
695
$content = json_decode($response->getBody()->getContents(), true);
696
696
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
+
697
753
Symfony Framework Integration
698
754
-----------------------------
699
755
@@ -816,4 +872,5 @@ However, using ``MockResponse`` allows simulating chunked responses and timeouts
816
872
.. _`cURL PHP extension` : https://php.net/curl
817
873
.. _`PSR-17` : https://www.php-fig.org/psr/psr-17/
818
874
.. _`PSR-18` : https://www.php-fig.org/psr/psr-18/
875
+ .. _`HTTPlug` : https://github.com/php-http/httplug/#readme
819
876
.. _`Symfony Contracts` : https://github.com/symfony/contracts
0 commit comments