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\Network\Http\CookieCollection will be created.
By default any POST/PUT/PATCH/DELETE request with $data will
send their data as multipart/form-data.
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.
Container class for cookies used in Http\Client.
Provides cookie jar like features for storing cookies between requests, as well as appending cookies to new requests.
Provides an interface for building multipart/form-encoded message bodies.
Used by Http\Client to upload POST/PUT data and files.
Base class for other HTTP requests/responses
Defines some common helper methods, constants and properties.
Implements methods for HTTP requests.
Used by Cake\Network\Http\Client to contain request information for making requests.
Implements methods for HTTP responses.
All of the following examples assume that $response is an
instance of this class.
Header names are case-insensitive, but normalized to Title-Case when the response is parsed.
$val = $response->header('content-type');
Will read the Content-Type header. You can get all set headers using:
$response->header();
You can also get at the headers using object access. When getting headers with object access, you have to use case-sensitive header names:
$val = $response->headers['Content-Type'];
You can access the response body using:
$content = $response->body();
You can also use object access:
$content = $response->body;
If your response body is in XML or JSON you can use special content type specific accessors to read the decoded data. JSON data will be returned as arrays, while XML data will be returned as SimpleXML nodes:
// Get as xml
$content = $response->xml
// Get as json
$content = $response->json
If the response cannot be decoded, null will be returned.
You can access the response status code using:
$content = $response->statusCode();
You can also use object access:
$content = $response->code;