- Full name:
\GuzzleHttp\Client
- This class implements:
\GuzzleHttp\ClientInterface
,\Psr\Http\Client\ClientInterface
private array $config
Clients accept an array of constructor parameters.
public __construct(array $config = []): mixed
Here's an example of creating a client using a base_uri and an array of default request options to apply to each request:
$client = new Client([
'base_uri' => 'http://www.foo.com/1.0/',
'timeout' => 0,
'allow_redirects' => false,
'proxy' => '192.168.16.1:10'
]);
Client configuration settings include the following options:
- handler: (callable) Function that transfers HTTP requests over the wire. The function is called with a Psr7\Http\Message\RequestInterface and array of transfer options, and must return a GuzzleHttp\Promise\PromiseInterface that is fulfilled with a Psr7\Http\Message\ResponseInterface on success. If no handler is provided, a default handler will be created that enables all of the request options below by attaching all of the default middleware to the handler.
- base_uri: (string|UriInterface) Base URI of the client that is merged into relative URIs. Can be a string or instance of UriInterface.
- **: any request option
Parameters:
Parameter | Type | Description |
---|---|---|
$config |
array | Client configuration settings. |
See Also:
- \GuzzleHttp\RequestOptions - for a list of available request options.
public __call(string $method, array $args): \GuzzleHttp\Promise\PromiseInterface|\Psr\Http\Message\ResponseInterface
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
Parameters:
Parameter | Type | Description |
---|---|---|
$method |
string | |
$args |
array |
Asynchronously send an HTTP request.
public sendAsync(\Psr\Http\Message\RequestInterface $request, array $options = []): \GuzzleHttp\Promise\PromiseInterface
Parameters:
Parameter | Type | Description |
---|---|---|
$request |
\Psr\Http\Message\RequestInterface | Request to send |
$options |
array | Request options to apply to the given request and to the transfer. See \GuzzleHttp\RequestOptions. |
Send an HTTP request.
public send(\Psr\Http\Message\RequestInterface $request, array $options = []): \Psr\Http\Message\ResponseInterface
Parameters:
Parameter | Type | Description |
---|---|---|
$request |
\Psr\Http\Message\RequestInterface | Request to send |
$options |
array | Request options to apply to the given request and to the transfer. See \GuzzleHttp\RequestOptions. |
The HttpClient PSR (PSR-18) specify this method.
public sendRequest(\Psr\Http\Message\RequestInterface $request): \Psr\Http\Message\ResponseInterface
Parameters:
Parameter | Type | Description |
---|---|---|
$request |
\Psr\Http\Message\RequestInterface |
Create and send an asynchronous HTTP request.
public requestAsync(string $method, string|\Psr\Http\Message\UriInterface $uri = '', array $options = []): \GuzzleHttp\Promise\PromiseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well. Use an array to provide a URL template and additional variables to use in the URL template expansion.
Parameters:
Parameter | Type | Description |
---|---|---|
$method |
string | HTTP method |
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. See \GuzzleHttp\RequestOptions. |
Create and send an HTTP request.
public request(string $method, string|\Psr\Http\Message\UriInterface $uri = '', array $options = []): \Psr\Http\Message\ResponseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well.
Parameters:
Parameter | Type | Description |
---|---|---|
$method |
string | HTTP method. |
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. See \GuzzleHttp\RequestOptions. |
Get a client configuration option.
public getConfig(string|null $option = null): mixed
These options include default request options of the client, a "handler" (if utilized by the concrete client), and a "base_uri" if utilized by the concrete client.
- Warning: this method is deprecated. This means that this method will likely be removed in a future version.
Parameters:
Parameter | Type | Description |
---|---|---|
$option |
string|null | The config option to retrieve. |
private buildUri(\Psr\Http\Message\UriInterface $uri, array $config): \Psr\Http\Message\UriInterface
Parameters:
Parameter | Type | Description |
---|---|---|
$uri |
\Psr\Http\Message\UriInterface | |
$config |
array |
Configures the default options for a client.
private configureDefaults(array $config): void
Parameters:
Parameter | Type | Description |
---|---|---|
$config |
array |
Merges default options into the array.
private prepareDefaults(array $options): array
Parameters:
Parameter | Type | Description |
---|---|---|
$options |
array | Options to modify by reference |
Transfers the given request and applies request options.
private transfer(\Psr\Http\Message\RequestInterface $request, array $options): \GuzzleHttp\Promise\PromiseInterface
The URI of the request is not modified and the request options are used as-is without merging in default options.
Parameters:
Parameter | Type | Description |
---|---|---|
$request |
\Psr\Http\Message\RequestInterface | |
$options |
array | See \GuzzleHttp\RequestOptions. |
Applies the array of request options to a request.
private applyOptions(\Psr\Http\Message\RequestInterface $request, array& $options): \Psr\Http\Message\RequestInterface
Parameters:
Parameter | Type | Description |
---|---|---|
$request |
\Psr\Http\Message\RequestInterface | |
$options |
array |
Return an InvalidArgumentException with pre-set message.
private invalidBody(): \GuzzleHttp\Exception\InvalidArgumentException
Create and send an HTTP request.
public request(string $method, string|\Psr\Http\Message\UriInterface $uri, array $options = []): \Psr\Http\Message\ResponseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well.
- This method is abstract.
Parameters:
Parameter | Type | Description |
---|---|---|
$method |
string | HTTP method. |
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |
Create and send an HTTP GET request.
public get(string|\Psr\Http\Message\UriInterface $uri, array $options = []): \Psr\Http\Message\ResponseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well.
Parameters:
Parameter | Type | Description |
---|---|---|
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |
Create and send an HTTP HEAD request.
public head(string|\Psr\Http\Message\UriInterface $uri, array $options = []): \Psr\Http\Message\ResponseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well.
Parameters:
Parameter | Type | Description |
---|---|---|
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |
Create and send an HTTP PUT request.
public put(string|\Psr\Http\Message\UriInterface $uri, array $options = []): \Psr\Http\Message\ResponseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well.
Parameters:
Parameter | Type | Description |
---|---|---|
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |
Create and send an HTTP POST request.
public post(string|\Psr\Http\Message\UriInterface $uri, array $options = []): \Psr\Http\Message\ResponseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well.
Parameters:
Parameter | Type | Description |
---|---|---|
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |
Create and send an HTTP PATCH request.
public patch(string|\Psr\Http\Message\UriInterface $uri, array $options = []): \Psr\Http\Message\ResponseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well.
Parameters:
Parameter | Type | Description |
---|---|---|
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |
Create and send an HTTP DELETE request.
public delete(string|\Psr\Http\Message\UriInterface $uri, array $options = []): \Psr\Http\Message\ResponseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well.
Parameters:
Parameter | Type | Description |
---|---|---|
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |
Create and send an asynchronous HTTP request.
public requestAsync(string $method, string|\Psr\Http\Message\UriInterface $uri, array $options = []): \GuzzleHttp\Promise\PromiseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well. Use an array to provide a URL template and additional variables to use in the URL template expansion.
- This method is abstract.
Parameters:
Parameter | Type | Description |
---|---|---|
$method |
string | HTTP method |
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |
Create and send an asynchronous HTTP GET request.
public getAsync(string|\Psr\Http\Message\UriInterface $uri, array $options = []): \GuzzleHttp\Promise\PromiseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well. Use an array to provide a URL template and additional variables to use in the URL template expansion.
Parameters:
Parameter | Type | Description |
---|---|---|
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |
Create and send an asynchronous HTTP HEAD request.
public headAsync(string|\Psr\Http\Message\UriInterface $uri, array $options = []): \GuzzleHttp\Promise\PromiseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well. Use an array to provide a URL template and additional variables to use in the URL template expansion.
Parameters:
Parameter | Type | Description |
---|---|---|
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |
Create and send an asynchronous HTTP PUT request.
public putAsync(string|\Psr\Http\Message\UriInterface $uri, array $options = []): \GuzzleHttp\Promise\PromiseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well. Use an array to provide a URL template and additional variables to use in the URL template expansion.
Parameters:
Parameter | Type | Description |
---|---|---|
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |
Create and send an asynchronous HTTP POST request.
public postAsync(string|\Psr\Http\Message\UriInterface $uri, array $options = []): \GuzzleHttp\Promise\PromiseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well. Use an array to provide a URL template and additional variables to use in the URL template expansion.
Parameters:
Parameter | Type | Description |
---|---|---|
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |
Create and send an asynchronous HTTP PATCH request.
public patchAsync(string|\Psr\Http\Message\UriInterface $uri, array $options = []): \GuzzleHttp\Promise\PromiseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well. Use an array to provide a URL template and additional variables to use in the URL template expansion.
Parameters:
Parameter | Type | Description |
---|---|---|
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |
Create and send an asynchronous HTTP DELETE request.
public deleteAsync(string|\Psr\Http\Message\UriInterface $uri, array $options = []): \GuzzleHttp\Promise\PromiseInterface
Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URL can contain the query string as well. Use an array to provide a URL template and additional variables to use in the URL template expansion.
Parameters:
Parameter | Type | Description |
---|---|---|
$uri |
string|\Psr\Http\Message\UriInterface | URI object or string. |
$options |
array | Request options to apply. |