The end user interface for doing HTTP requests.
If you're doing multiple requests to the same hostname its 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 list of currently stored cookies using the cookies() method.
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 Adapter for sending requests. Defaults to Cake\Http\Client\Adapter\Stream
$_cookies
protected $_defaultConfig
protected array
Returns 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:
$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
_typeHeaders( string $type )
Returns headers for Accept/Content-Type based on a short type or full mime-type.
$type
Cake\Core\Exception\Exception
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.
Returns an array of cookie data arrays.
Cake\Http\Client\CookieCollection
delete( string $url , mixed $data [] , array $options [] )
Do a DELETE request.
$url
$data
optional [] $options
optional [] Cake\Http\Client\Response
get( 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\Response
head( string $url , array $data [] , array $options [] )
Do a HEAD request.
$url
$data
optional [] $options
optional [] Cake\Http\Client\Response
options( string $url , mixed $data [] , array $options [] )
Do an OPTIONS request.
$url
$data
optional [] $options
optional [] Cake\Http\Client\Response
patch( string $url , mixed $data [] , array $options [] )
Do a PATCH request.
$url
$data
optional [] $options
optional [] Cake\Http\Client\Response
post( string $url , mixed $data [] , array $options [] )
Do a POST request.
$url
$data
optional [] $options
optional [] Cake\Http\Client\Response
put( string $url , mixed $data [] , array $options [] )
Do a PUT request.
$url
$data
optional [] $options
optional [] Cake\Http\Client\Response
send( 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\Response
trace( 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\Exception
config( 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\Exception
configShallow( 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->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
$value
optional null getConfig( string|null $key 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');
$key
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\Client\Adapter\Stream
Adapter for sending requests. Defaults to Cake\Http\Client\Adapter\Stream
protected Cake\Http\Client\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' => 'Cake\Http\Client\Adapter\Stream', '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–2017 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.4/class-Cake.Http.Client.html