Skip to content

marko/http-guzzle

Guzzle-powered HTTP client driver --- makes real HTTP requests using the battle-tested Guzzle library. Implements HttpClientInterface from marko/http using Guzzle under the hood. Connection failures throw ConnectionException; HTTP errors throw HttpException with the response attached.

Terminal window
composer require marko/http-guzzle

This automatically installs marko/http.

Bind the interface to the Guzzle implementation in your module.php:

module.php
use Marko\Http\Contracts\HttpClientInterface;
use Marko\Http\Guzzle\GuzzleHttpClient;
return [
'bindings' => [
HttpClientInterface::class => GuzzleHttpClient::class,
],
];

Then inject HttpClientInterface anywhere:

use Marko\Http\Contracts\HttpClientInterface;
class ApiClient
{
public function __construct(
private HttpClientInterface $httpClient,
) {}
public function fetchData(): array
{
$response = $this->httpClient->get('https://api.example.com/data', [
'timeout' => 10,
'headers' => ['Accept' => 'application/json'],
]);
return $response->json();
}
}
use Marko\Http\Exceptions\ConnectionException;
use Marko\Http\Exceptions\HttpException;
try {
$response = $this->httpClient->get('https://api.example.com/resource');
} catch (ConnectionException $e) {
// Network failure (DNS, timeout, etc.)
} catch (HttpException $e) {
// HTTP error (4xx, 5xx) --- response may be available
$errorResponse = $e->getResponse();
}

The following request options are forwarded to Guzzle:

OptionDescription
headersAssociative array of HTTP headers
bodyRaw request body
jsonData to send as JSON (automatically sets Content-Type)
queryQuery string parameters
timeoutRequest timeout in seconds

Extend GuzzleHttpClient via Preference to customize the underlying Guzzle client:

use Marko\Core\Attributes\Preference;
use Marko\Http\Guzzle\GuzzleHttpClient;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
#[Preference(replaces: GuzzleHttpClient::class)]
class CustomGuzzleClient extends GuzzleHttpClient
{
protected function createClient(): GuzzleClientInterface
{
return new Client([
'base_uri' => 'https://api.example.com',
'timeout' => 30,
]);
}
}

The createClient() method is called lazily on first request --- override it to set base URIs, default timeouts, middleware, or any other Guzzle configuration.

Implements HttpClientInterface. See marko/http for the full contract.

MethodDescription
request(string $method, string $url, array $options = []): HttpResponseSend a request with any HTTP method
get(string $url, array $options = []): HttpResponseSend a GET request
post(string $url, array $options = []): HttpResponseSend a POST request
put(string $url, array $options = []): HttpResponseSend a PUT request
patch(string $url, array $options = []): HttpResponseSend a PATCH request
delete(string $url, array $options = []): HttpResponseSend a DELETE request
createClient(): GuzzleClientInterfaceProtected --- override via Preference to customize the Guzzle client

All methods throw ConnectionException on network failures and HttpException on HTTP error responses (4xx, 5xx). The HttpException carries the HttpResponse when available.