Quickstart
This example uses both fast-forward/http-factory
and
fast-forward/http-client
so it works in a fresh FastForward project without
hidden prerequisites.
<?php
declare(strict_types=1);
use FastForward\Http\Client\ServiceProvider\HttpClientServiceProvider;
use FastForward\Http\Message\Factory\ServiceProvider\HttpMessageFactoryServiceProvider;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use function FastForward\Container\container;
$container = container(
new HttpMessageFactoryServiceProvider(),
new HttpClientServiceProvider(),
);
/** @var RequestFactoryInterface $requestFactory */
$requestFactory = $container->get(RequestFactoryInterface::class);
$request = $requestFactory
->createRequest('GET', 'https://example.com')
->withHeader('Accept', 'text/html');
/** @var ClientInterface $client */
$client = $container->get(ClientInterface::class);
$response = $client->sendRequest($request);
$statusCode = $response->getStatusCode();
$body = (string) $response->getBody();
// Example outcome:
// $statusCode === 200
// $body contains the HTML returned by https://example.com
What Happens Here
HttpMessageFactoryServiceProvidermakes PSR-17 factories available in the container.HttpClientServiceProviderregisters the Symfony transport and the PSR-18 client adapter.RequestFactoryInterfacecreates an immutable PSR-7 request.ClientInterfacesends that request and returns a PSR-7 response.
If You Prefer One Provider
If your project already depends on
fast-forward/http, you can
register FastForward\Http\ServiceProvider\HttpServiceProvider
instead of
listing the factory and client providers separately.
use FastForward\Http\ServiceProvider\HttpServiceProvider;
use function FastForward\Container\container;
$container = container(new HttpServiceProvider());
What To Read Next
- Getting Services for the service map and retrieval rules.
- Using The PSR-18 Client for request-building and error-handling details.
- Overriding Services if you want custom timeouts, headers, or a different client registration strategy.