Skip to content

Commit 2bcd5ca

Browse files
committed
Merge branch '4.4'
* 4.4: [HttpClient] add words about HTTPlug
2 parents 3217fb4 + 0ad95cc commit 2bcd5ca

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
@@ -617,15 +617,15 @@ regular expression applied to relative URLs::
617617
Interoperability
618618
----------------
619619

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

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

630630
Symfony Contracts
631631
~~~~~~~~~~~~~~~~~
@@ -657,12 +657,14 @@ the PSR-18 abstraction, which provides none related to the transport itself.
657657
Another major feature covered by the Symfony Contracts is async/multiplexing,
658658
as described in the previous sections.
659659

660-
PSR-18
661-
~~~~~~
660+
PSR-18 and PSR-17
661+
~~~~~~~~~~~~~~~~~
662662

663663
This component implements the `PSR-18`_ (HTTP Client) specifications via the
664664
:class:`Symfony\\Component\\HttpClient\\Psr18Client` class, which is an adapter
665665
to turn a Symfony ``HttpClientInterface`` into a PSR-18 ``ClientInterface``.
666+
This class also implements the relevant methods of `PSR-17`_ to ease creating
667+
request objects.
666668

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

@@ -677,18 +679,72 @@ To use it, you need the ``psr/http-client`` package and a `PSR-17`_ implementati
677679
678680
Now you can make HTTP requests with the PSR-18 client as follows::
679681

680-
use Nyholm\Psr7\Factory\Psr17Factory;
681682
use Symfony\Component\HttpClient\Psr18Client;
682683

683-
$psr17Factory = new Psr17Factory();
684-
$psr18Client = new Psr18Client();
684+
$client = new Psr18Client();
685685

686686
$url = 'https://symfony.com/versions.json';
687-
$request = $psr17Factory->createRequest('GET', $url);
688-
$response = $psr18Client->sendRequest($request);
687+
$request = $client->createRequest('GET', $url);
688+
$response = $client->sendRequest($request);
689689

690690
$content = json_decode($response->getBody()->getContents(), true);
691691

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

@@ -811,4 +867,5 @@ However, using ``MockResponse`` allows simulating chunked responses and timeouts
811867
.. _`cURL PHP extension`: https://php.net/curl
812868
.. _`PSR-17`: https://www.php-fig.org/psr/psr-17/
813869
.. _`PSR-18`: https://www.php-fig.org/psr/psr-18/
870+
.. _`HTTPlug`: https://github.com/php-http/httplug/#readme
814871
.. _`Symfony Contracts`: https://github.com/symfony/contracts

0 commit comments

Comments
 (0)