Client API Reference
The complete API reference for the Client class in the Fetch HTTP client package.
Class Declaration
php
namespace Fetch\Http;
class Client implements ClientInterface, LoggerAwareInterface
{
// ...
}Constructor
php
/**
* Client constructor.
*
* @param ClientHandlerInterface|null $handler The client handler
* @param array<string, mixed> $options Default request options
* @param LoggerInterface|null $logger PSR-3 logger
*/
public function __construct(
?ClientHandlerInterface $handler = null,
array $options = [],
?LoggerInterface $logger = null
)Factory Methods
createWithBaseUri()
Creates a new client with a base URI.
php
public static function createWithBaseUri(string $baseUri, array $options = []): staticPSR-7 Implementation
sendRequest()
Sends a PSR-7 request and returns a PSR-7 response. Implements the PSR-18 ClientInterface.
php
public function sendRequest(RequestInterface $request): PsrResponseInterfaceThrows
NetworkException- If there's a network errorRequestException- If there's an error with the requestClientException- For unexpected errors
Fetch API
fetch()
Creates and sends an HTTP request. Returns the handler for method chaining if no URL is provided.
php
public function fetch(?string $url = null, ?array $options = []): ResponseInterface|ClientHandlerInterfaceHTTP Methods
get()
Makes a GET request.
php
public function get(string $url, ?array $queryParams = null, ?array $options = []): ResponseInterfacepost()
Makes a POST request.
php
public function post(
string $url,
mixed $body = null,
string|ContentType $contentType = ContentType::JSON,
?array $options = []
): ResponseInterfaceput()
Makes a PUT request.
php
public function put(
string $url,
mixed $body = null,
string|ContentType $contentType = ContentType::JSON,
?array $options = []
): ResponseInterfacepatch()
Makes a PATCH request.
php
public function patch(
string $url,
mixed $body = null,
string|ContentType $contentType = ContentType::JSON,
?array $options = []
): ResponseInterfacedelete()
Makes a DELETE request.
php
public function delete(
string $url,
mixed $body = null,
string|ContentType $contentType = ContentType::JSON,
?array $options = []
): ResponseInterfacehead()
Makes a HEAD request.
php
public function head(string $url, ?array $options = []): ResponseInterfaceoptions()
Makes an OPTIONS request.
php
public function options(string $url, ?array $options = []): ResponseInterfacemethodRequest()
Makes a request with a specific HTTP method (protected method used internally).
php
protected function methodRequest(
Method $method,
string $url,
mixed $body = null,
string|ContentType $contentType = ContentType::JSON,
?array $options = []
): ResponseInterfaceClient Handling
getHandler()
Gets the underlying client handler.
php
public function getHandler(): ClientHandlerInterfacegetHttpClient()
Gets the PSR-7 HTTP client.
php
public function getHttpClient(): ClientInterfaceLogger Integration
setLogger()
Sets a PSR-3 logger. Implements PSR-3 LoggerAwareInterface.
php
public function setLogger(LoggerInterface $logger): voidProtected Methods
extractOptionsFromRequest()
Extracts options from a PSR-7 request.
php
protected function extractOptionsFromRequest(RequestInterface $request): array