|
| 1 | +# HTTPlug HTTP client abstraction |
| 2 | + |
| 3 | +HTTPlug is an abstraction for HTTP clients. There are two main use cases: |
| 4 | + |
| 5 | +1. Usage in a project/application |
| 6 | +2. Usage in a reusable package |
| 7 | + |
| 8 | +In both cases, the `Http\Client\HttpClient` provides a `sendRequest` method to send a PSR-7 `RequestInterface` |
| 9 | +and returns a PSR-7 `ResponseInterface`or throws an exception that implements `Http\Client\Exception`. |
| 10 | + |
| 11 | +There is also the `Http\Client\HttpAsyncClient` which provides the `sendAsyncRequest` method to send |
| 12 | +a request asynchronously and returns a `Http\Client\Promise`. |
| 13 | + |
| 14 | +The promise allows to specify handlers for a PSR-7 `ResponseInterface` |
| 15 | +or an exception that implements `Http\Client\Exception`. |
| 16 | + |
| 17 | + |
| 18 | +<p class="text-warning"> |
| 19 | + Contract for the `Http\Client\Promise` is temporary until |
| 20 | + [PSR is released](https://groups.google.com/forum/?fromgroups#!topic/php-fig/wzQWpLvNSjs). |
| 21 | + Once it is out, we will use this PSR in the main client and deprecate the old contract. |
| 22 | +</p> |
| 23 | + |
| 24 | + |
| 25 | +See the [tutorial](tutorial.md) for a concrete example. |
| 26 | + |
| 27 | + |
| 28 | +## HTTPlug implementations |
| 29 | + |
| 30 | +HTTPlug implementations typically are either HTTP clients of their own, or adapters wrapping existing clients |
| 31 | +like Guzzle 6. In the latter case, they will depend on the required client implementation, |
| 32 | +so you only need to require the adapter and not the actual client. |
| 33 | + |
| 34 | + |
| 35 | +There are two kind of implementations: |
| 36 | + |
| 37 | + - [php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation): |
| 38 | + the synchronous implementation that waits for the response / error before returning from the `sendRequest` method. |
| 39 | + - [php-http/async-client-implementation](https://packagist.org/providers/php-http/async-client-implementation): |
| 40 | + the asynchronous implementation that immediately returns a `Http\Client\Promise`, |
| 41 | + allowing to send several requests in parallel and handling responses later. |
| 42 | + |
| 43 | +Check links above for the full list of implementations. |
| 44 | + |
| 45 | + |
| 46 | +## Usage in an application |
| 47 | + |
| 48 | +When writing an application, you need to require a concrete |
| 49 | +[implementation](https://packagist.org/providers/php-http/client-implementation). |
| 50 | + |
| 51 | +See [virtual package](virtual-package.md) for more information on the topic of working with HTTPlug implementations. |
| 52 | + |
| 53 | + |
| 54 | +## Installation in a reusable package |
| 55 | + |
| 56 | +In many cases, packages are designed to be reused from the very beginning. |
| 57 | +For example, API clients are usually used in other packages/applications, not on their own. |
| 58 | + |
| 59 | +Reusable packages should **not rely on a concrete implementation** (like Guzzle 6), |
| 60 | +but only require any implementation of HTTPlug. HTTPlug uses the concept of virtual packages. |
| 61 | +Instead of depending on only the interfaces, which would be missing an implementation, |
| 62 | +or depending on one concrete implementation, you should depend on the virtual package `php-http/client-implementation` |
| 63 | +or `php-http/async-client-implementation`. There is no package with that name, |
| 64 | +but all clients and adapters implementing HTTPlug declare that they provide one of this virtual package or both. |
| 65 | + |
| 66 | +You need to edit the `composer.json` of your package to add the virtual package. |
| 67 | +For development (installing the package standalone, running tests), |
| 68 | +add a concrete implementation in the `require-dev` section to make the project installable: |
| 69 | + |
| 70 | +``` json |
| 71 | +... |
| 72 | +"require": { |
| 73 | + "php-http/client-implementation": "^1.0" |
| 74 | +}, |
| 75 | +"require-dev": { |
| 76 | + "php-http/guzzle6-adapter": "^1.0" |
| 77 | +}, |
| 78 | +... |
| 79 | +``` |
| 80 | + |
| 81 | +For extra convenience, you can use the [Discovery Component](/components/discovery) system to free the user of your |
| 82 | +package from having to instantiate the client. |
| 83 | +You should however always accept injecting the client instance to allow the user to configure the client as needed. |
| 84 | +You can find an example in the [Discovery Component](/components/discovery) documentation. |
| 85 | + |
| 86 | +Users of your package will have to select a concrete adapter in their project to make your package installable. |
| 87 | +Best point them to the [virtual package](virtual-package.md) howto page. |
| 88 | + |
| 89 | +To be able to send requests, you should not depend on a specific PSR-7 implementation, |
| 90 | +but use the [Message Factory Component](/components/message-factory) system. |
| 91 | + |
| 92 | + |
| 93 | +### Framework Integration |
| 94 | + |
| 95 | +HTTPlug can be used in any PHP based project. |
| 96 | +Nonetheless, we provide particular integration for some popular frameworks: |
| 97 | + |
| 98 | +- [HttplugBundle](https://github.com/php-http/HttplugBundle/) integration with the Symfony framework. |
| 99 | + |
| 100 | + |
| 101 | +## History |
| 102 | + |
| 103 | +This project has been started by [Eric Geloen](https://github.com/egeloen) as |
| 104 | +Ivory Http Adapter](https://github.com/egeloen/ivory-http-adapter). It never made it to a stable release, |
| 105 | +but it relied on PSR-7 which was not stable either that time. Because of the constantly changing PSR-7, |
| 106 | +Eric had to rewrite the library over and over again (at least the message handling part, |
| 107 | +which in most cases affected every adapter as well). |
| 108 | + |
| 109 | +In 2015, a decision has been made to move the library to it's own organization, so PHP HTTP was born. |
| 110 | + |
| 111 | +See [migrating](migrating.md) for a guide how to migrate your code from the Ivory adapter to HTTPlug. |
0 commit comments