$_config
$_config : array
Runtime config
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.
$_cookies : \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.
$_adapter : \Cake\Http\Client\AdapterInterface
Adapter for sending requests.
setConfig(string|array $key, mixed|null $value = null, boolean $merge = true) : $this
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']);
string|array | $key | The key to set, or a complete array of configs. |
mixed|null | $value | The value to set. |
boolean | $merge | Whether to recursively merge or overwrite existing config, defaults to true. |
When trying to set a key that is invalid.
getConfig(string|null $key = null, mixed $default = null) : mixed
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');
string|null | $key | The key to get or null for the whole config. |
mixed | $default | The return value when the key does not exist. |
Config value being read.
config(string|array|null $key = null, mixed|null $value = null, boolean $merge = true) : mixed
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']);
string|array|null | $key | The key to get/set, or a complete array of configs. |
mixed|null | $value | The value to set. |
boolean | $merge | Whether to recursively merge or overwrite existing config, defaults to true. |
When trying to set a key that is invalid.
Config value being read, or the object itself on write operations.
configShallow(string|array $key, mixed|null $value = null) : $this
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']);
string|array | $key | The key to set, or a complete array of configs. |
mixed|null | $value | The value to set. |
None found |
__construct(array $config = array())
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.array | $config | Config options for scoped clients. |
None found |
cookies() : \Cake\Http\Cookie\CookieCollection
Get the cookies stored in the Client.
None found |
addCookie(\Cake\Http\Cookie\CookieInterface $cookie) : $this
Adds a cookie to the Client collection.
\Cake\Http\Cookie\CookieInterface | $cookie | Cookie object. |
None found |
get(string $url, array $data = array(), array $options = array()) : \Cake\Http\Client\Response
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.
string | $url | The url or path you want to request. |
array | $data | The query data you want to send. |
array | $options | Additional options for the request. |
None found |
post(string $url, mixed $data = array(), array $options = array()) : \Cake\Http\Client\Response
Do a POST request.
string | $url | The url or path you want to request. |
mixed | $data | The post data you want to send. |
array | $options | Additional options for the request. |
None found |
put(string $url, mixed $data = array(), array $options = array()) : \Cake\Http\Client\Response
Do a PUT request.
string | $url | The url or path you want to request. |
mixed | $data | The request data you want to send. |
array | $options | Additional options for the request. |
None found |
patch(string $url, mixed $data = array(), array $options = array()) : \Cake\Http\Client\Response
Do a PATCH request.
string | $url | The url or path you want to request. |
mixed | $data | The request data you want to send. |
array | $options | Additional options for the request. |
None found |
options(string $url, mixed $data = array(), array $options = array()) : \Cake\Http\Client\Response
Do an OPTIONS request.
string | $url | The url or path you want to request. |
mixed | $data | The request data you want to send. |
array | $options | Additional options for the request. |
None found |
trace(string $url, mixed $data = array(), array $options = array()) : \Cake\Http\Client\Response
Do a TRACE request.
string | $url | The url or path you want to request. |
mixed | $data | The request data you want to send. |
array | $options | Additional options for the request. |
None found |
delete(string $url, mixed $data = array(), array $options = array()) : \Cake\Http\Client\Response
Do a DELETE request.
string | $url | The url or path you want to request. |
mixed | $data | The request data you want to send. |
array | $options | Additional options for the request. |
None found |
head(string $url, array $data = array(), array $options = array()) : \Cake\Http\Client\Response
Do a HEAD request.
string | $url | The url or path you want to request. |
array | $data | The query string data you want to send. |
array | $options | Additional options for the request. |
None found |
send(\Cake\Http\Client\Request $request, array $options = array()) : \Cake\Http\Client\Response
Send a request.
Used internally by other methods, but can also be used to send handcrafted Request objects.
\Cake\Http\Client\Request | $request | The request to send. |
array | $options | Additional options to use. |
None found |
buildUrl(string $url, string|array $query = array(), array $options = array()) : string
Generate a URL based on the scoped client options.
string | $url | Either a full URL or just the path. |
string|array | $query | The query data for the URL. |
array | $options | The config options stored with Client::config() |
A complete url with scheme, port, host, and path.
None found |
_configRead(string|null $key) : mixed
Reads a config key.
string|null | $key | Key to read. |
None found |
_configWrite(string|array $key, mixed $value, boolean|string $merge = false) : void
Writes a config key.
string|array | $key | Key to write to. |
mixed | $value | Value to write. |
boolean|string | $merge | True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false. |
if attempting to clobber existing config
None found |
_configDelete(string $key) : void
Deletes a single config key.
string | $key | Key to delete. |
if attempting to clobber existing config
None found |
_doRequest(string $method, string $url, mixed $data, array $options) : \Cake\Http\Client\Response
Helper method for doing non-GET requests.
string | $method | HTTP method. |
string | $url | URL to request. |
mixed | $data | The request body. |
array | $options | The options to use. Contains auth, proxy, etc. |
None found |
_mergeOptions(array $options) : array
Does a recursive merge of the parameter with the scope config.
array | $options | Options to merge. |
Options merged with set config.
None found |
_sendRequest(\Cake\Http\Client\Request $request, array $options) : \Cake\Http\Client\Response
Send a request without redirection.
\Cake\Http\Client\Request | $request | The request to send. |
array | $options | Additional options to use. |
None found |
_createRequest(string $method, string $url, mixed $data, array $options) : \Cake\Http\Client\Request
Creates a new request object based on the parameters.
string | $method | HTTP method name. |
string | $url | The url including query string. |
mixed | $data | The request body. |
array | $options | The options to use. Contains auth, proxy, etc. |
None found |
_typeHeaders(string $type) : array
Returns headers for Accept/Content-Type based on a short type or full mime-type.
string | $type | short type alias or full mimetype. |
When an unknown type alias is used.
Headers to set on the request.
None found |
_addAuthentication(\Cake\Http\Client\Request $request, array $options) : \Cake\Http\Client\Request
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 | The request to modify. |
array | $options | Array of options containing the 'auth' key. |
The updated request object.
None found |
_addProxy(\Cake\Http\Client\Request $request, array $options) : \Cake\Http\Client\Request
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 | The request to modify. |
array | $options | Array of options containing the 'proxy' key. |
The updated request object.
None found |
_createAuth(array $auth, array $options) : mixed
Create the authentication strategy.
Use the configuration options to create the correct authentication strategy handler.
array | $auth | The authentication options to use. |
array | $options | The overall request options to use. |
when an invalid strategy is chosen.
Authentication strategy instance.
None found |