The end user interface for doing HTTP requests.
If you're doing multiple requests to the same hostname it's often convenient to use the constructor arguments to create a scoped client. This allows you to keep your code DRY and not repeat hostnames, authentication, and other options.
Once you've created an instance of Client you can do requests using several methods. Each corresponds to a different HTTP method.
Client will maintain cookies from the responses done with a client instance. These cookies will be automatically added to future requests to matching hosts. Cookies will respect the Expires, Path and Domain attributes. You can get the client's CookieCollection using cookies()
You can use the 'cookieJar' constructor option to provide a custom cookie jar instance you've restored from cache/disk. By default an empty instance of Cake\Http\Client\CookieCollection will be created.
By default any POST/PUT/PATCH/DELETE request with $data will send their data as application/x-www-form-urlencoded unless there are attached files. In that case multipart/form-data will be used.
When sending request bodies you can use the type option to set the Content-Type for the request:
$http->get('/users', [], ['type' => 'json']); The type option sets both the Content-Type and Accept header, to the same mime type. When using type you can use either a full mime type or an alias. If you need different types in the Accept and Content-Type headers you should set them manually and not use type
By using the auth key you can use authentication. The type sub option can be used to specify which authentication strategy you want to use. CakePHP comes with a few built-in strategies:
By using the proxy key you can set authentication credentials for a proxy if you need to use one. The type sub option can be used to specify which authentication strategy you want to use. CakePHP comes with built-in support for basic authentication.
$_adapter protected $_cookies protected $_defaultConfig protected arrayReturns headers for Accept/Content-Type based on a short type or full mime-type.
__construct( array $config = [] )
Create a new HTTP Client.
You can set the following options when creating a client:
curl extension is loaded else \Cake\Http\Client\Adapter\Stream.$config optional [] _addAuthentication( Cake\Http\Client\Request $request , array $options )
Add authentication headers to the request.
Uses the authentication type to choose the correct strategy and use its methods to add headers.
Cake\Http\Client\Request $request $options Cake\Http\Client\Request_addProxy( Cake\Http\Client\Request $request , array $options )
Add proxy authentication headers.
Uses the authentication type to choose the correct strategy and use its methods to add headers.
Cake\Http\Client\Request $request $options Cake\Http\Client\Request_createAuth( array $auth , array $options )
Create the authentication strategy.
Use the configuration options to create the correct authentication strategy handler.
$auth $options Cake\Core\Exception\Exception_createRequest( string $method , string $url , mixed $data , array $options )
Creates a new request object based on the parameters.
$method $url $data $options Cake\Http\Client\Request_doRequest( string $method , string $url , mixed $data , array $options )
Helper method for doing non-GET requests.
$method $url $data $options Cake\Http\Client\Response_mergeOptions( array $options )
Does a recursive merge of the parameter with the scope config.
$options _sendRequest( Cake\Http\Client\Request $request , array $options )
Send a request without redirection.
Cake\Http\Client\Request $request $options Cake\Http\Client\Response_typeHeaders( string $type )
Returns headers for Accept/Content-Type based on a short type or full mime-type.
$type Cake\Core\Exception\ExceptionaddCookie( Cake\Http\Cookie\CookieInterface $cookie )
Adds a cookie to the Client collection.
Cake\Http\Cookie\CookieInterface $cookie buildUrl( string $url , string|array $query = [] , array $options = [] )
Generate a URL based on the scoped client options.
$url $query optional [] $options optional [] cookies( )
Get the cookies stored in the Client.
Cake\Http\Cookie\CookieCollectiondelete( string $url , mixed $data = [] , array $options = [] )
Do a DELETE request.
$url $data optional [] $options optional [] Cake\Http\Client\Responseget( string $url , array $data = [] , array $options = [] )
Do a GET request.
The $data argument supports a special _content key for providing a request body in a GET request. This is generally not used, but services like ElasticSearch use this feature.
$url $data optional [] $options optional [] Cake\Http\Client\Responsehead( string $url , array $data = [] , array $options = [] )
Do a HEAD request.
$url $data optional [] $options optional [] Cake\Http\Client\Responseoptions( string $url , mixed $data = [] , array $options = [] )
Do an OPTIONS request.
$url $data optional [] $options optional [] Cake\Http\Client\Responsepatch( string $url , mixed $data = [] , array $options = [] )
Do a PATCH request.
$url $data optional [] $options optional [] Cake\Http\Client\Responsepost( string $url , mixed $data = [] , array $options = [] )
Do a POST request.
$url $data optional [] $options optional [] Cake\Http\Client\Responseput( string $url , mixed $data = [] , array $options = [] )
Do a PUT request.
$url $data optional [] $options optional [] Cake\Http\Client\Responsesend( Cake\Http\Client\Request $request , array $options = [] )
Send a request.
Used internally by other methods, but can also be used to send handcrafted Request objects.
Cake\Http\Client\Request $request $options optional [] Cake\Http\Client\Responsetrace( string $url , mixed $data = [] , array $options = [] )
Do a TRACE request.
$url $data optional [] $options optional [] Cake\Http\Client\Response_configDelete( string $key )
Deletes a single config key.
$key Cake\Core\Exception\Exception_configRead( string|null $key )
Reads a config key.
$key _configWrite( string|array $key , mixed $value , boolean|string $merge = false )
Writes a config key.
$key $value $merge optional false True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Cake\Core\Exception\Exceptionconfig( string|array|null $key = null , mixed|null $value = null , boolean $merge = true )
Gets/Sets the config.
Reading the whole config:
$this->config();
Reading a specific value:
$this->config('key'); Reading a nested value:
$this->config('some.nested.key'); Setting a specific value:
$this->config('key', $value); Setting a nested value:
$this->config('some.nested.key', $value); Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
$key optional null $value optional null $merge optional true Cake\Core\Exception\ExceptionconfigShallow( string|array $key , mixed|null $value = null )
Merge provided config with existing config. Unlike config() which does a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->configShallow('key', $value); Setting a nested value:
$this->configShallow('some.nested.key', $value); Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
$key $value optional null getConfig( string|null $key = null , mixed $default = null )
Returns the config.
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key'); Reading a nested value:
$this->getConfig('some.nested.key'); Reading with default value:
$this->getConfig('some-key', 'default-value'); $key optional null $default optional null setConfig( string|array $key , mixed|null $value = null , boolean $merge = true )
Sets the config.
Setting a specific value:
$this->setConfig('key', $value); Setting a nested value:
$this->setConfig('some.nested.key', $value); Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
$key $value optional null $merge optional true Cake\Core\Exception\Exception protected Cake\Http\Cookie\CookieCollection
List of cookies from responses made with this client.
Cookies are indexed by the cookie's domain or request host name.
protected array
Default configuration for the client.
[
'adapter' => null,
'host' => null,
'port' => null,
'scheme' => 'http',
'timeout' => 30,
'ssl_verify_peer' => true,
'ssl_verify_peer_name' => true,
'ssl_verify_depth' => 5,
'ssl_verify_host' => true,
'redirect' => false,
]
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.8/class-Cake.Http.Client.html