An HTTPlug adapter for the React Http client.
Use Composer to install the React adapter. It will also install React as it’s a dependency.
$ composer require php-http/react-adapter
This client does not come with a PSR-7 implementation out of the box, so you have to install one as well (for example Guzzle PSR-7):
$ composer require guzzlehttp/psr7
In order to provide full interoperability, message implementations are accessed through factories. Message factories for Diactoros, Guzzle PSR-7 and Slim Framework are available in the message component:
$ composer require php-http/message
Alternatively you can install the discovery layer to be able to automatically find installed resources, like factories:
$ composer require php-http/discovery
The React client adapter needs a message factory in order to to work:
use Http\Adapter\React\Client; $client = new Client($messageFactory);
For simplicity, all required objects are instantiated automatically if not explicitly specified:
|The event loop used inside the React engine.|
|The HTTP client instance that must be adapted.|
If you need more control on the React instances, you can inject them during initialization:
use Http\Adapter\React\Client; $eventLoop = React\EventLoop\Factory::create(); $dnsResolverFactory = new React\Dns\Resolver\Factory(); $dnsResolver = $dnsResolverFactory->createCached('126.96.36.199', $loop); $factory = new React\HttpClient\Factory(); $reactHttp = $factory->create($loop, $dnsResolver); $adapter = new Client($messageFactory, $eventLoop, $reactHttp);
If you choose to inject a custom React HTTP client, you must inject the loop used during its construction. But if you already use an EventLoop inside your code, you can inject only this object.
You can also use a
ReactFactory in order to initialize React instances:
use Http\Adapter\React\ReactFactory; $eventLoop = ReactFactory::buildEventLoop(); $reactHttp = ReactFactory::buildHttpClient($eventLoop);
Then you can use the adapter to send synchronous requests:
use GuzzleHttp\Psr7\Request; $request = new Request('GET', 'http://httpbin.org'); // Returns a Psr\Http\Message\ResponseInterface $response = $adapter->sendRequest($request);
Or send asynchronous ones:
use GuzzleHttp\Psr7\Request; $request = new Request('GET', 'http://httpbin.org'); // Returns a Http\Promise\Promise $promise = $adapter->sendAsyncRequest(request);
- Use plugins to customize the way HTTP requests are sent and responses processed by following redirects, adding Authentication or Cookie headers and more.
- Learn how you can decouple your code from any PSR-7 implementation by using the HTTP factories.
- Read more about promises when using asynchronous requests.