Factory method for building controllers from request/response pairs.
Base class for full-stack applications
This class serves as a base class for applications that are using CakePHP as a full stack framework. If you are only using the Http or Console libraries you should implement the relevant interfaces directly.
The application class is responsible for bootstrapping the application, and ensuring that middleware is attached. It is also invoked as the last piece of middleware, and delegates request/response handling to the correct controller.
Stream implementation that generates its content using a callback.
The callback is invoked once when the stream contents are read. The
callback can either return a string or use echo to output content
directly. Non-string return values are ignored.
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 {@link \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:
$client = new Client(['adapter' => \Cake\Http\Client\Adapter\Stream::class]);
$response = $client->post($url, $json, [
'stream' => true,
'readTimeout' => 60,
'headers' => ['Content-Type' => 'application/json'],
]);
$body = $response->getBody();
try {
while (!$body->eof()) {
$chunk = $body->read(8192);
// Process bytes incrementally. Chunks need not contain complete SSE events.
}
} finally {
$body->close();
}
To relay bytes to a browser, attach the body to a server response using
withBody(). Set the appropriate Content-Type (e.g. text/event-stream),
Cache-Control and X-Accel-Buffering headers. The emitter reads and flushes
non-seekable bodies incrementally and closes them after emission. Server and
proxy buffering must also be configured to allow streaming. For nested PHP
output buffers, use new \Cake\Http\ResponseEmitter(flushOutputBuffers: true).
This removes inner buffers before emission, discarding empty compression handlers.
Nonempty custom handlers must be drained before calling the emitter.
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.
Negotiates the preferred content type from what the application provides and what the request has in its Accept header.
A builder object that assists in defining Cross Origin Request related headers.
Each of the methods in this object provide a fluent interface. Once you've
set all the headers you want to use, the build() method can be used to return
a modified Response.
It is most convenient to get this object via Response::cors().
The FlashMessage class provides a way for you to write a flash variable to the session, to be rendered in a view with the FlashHelper.
Provides helper methods related to HTTP headers
Base class for standalone HTTP applications
Provides a base class to inherit from for applications using only the http package. This class defines a fallback handler that renders a simple 404 response.
You can overload the handle method to provide your own logic
to run when no middleware generates a response.
Provides methods for creating and manipulating a "queue" of middlewares. This queue is used to process a request and generate response via \Cake\Http\Runner.
MimeType Class
Handles MIME type operations and provides functionality for working with Multipurpose Internet Mail Extensions (MIME) types in the application. This class is responsible for MIME type detection and mapping file extensions to their corresponding MIME types.
Factory for creating request instances.
Responses contain the response text, status and headers of a HTTP response.
There are external packages such as fig/http-message-util that provide HTTP
status code constants. These can be used with any method that accepts or
returns a status code integer. Keep in mind that these constants might
include status codes that are not allowed which will throw an
\InvalidArgumentException.
Emits a Response to the PHP Server API.
Factory class for creating response instances.
Executes the middleware queue and provides the next callable
that allows the queue to be iterated.
Runs an application invoking all the PSR7 middleware and the registered application.
A class that helps wrap Request information and particulars about a single request. Provides methods commonly used to introspect on the request headers and request body.
Factory for making ServerRequest instances.
This adds in CakePHP specific behavior to populate the basePath and webroot attributes. Furthermore the Uri's path is corrected to only contain the 'virtual' path for the request.
This class is a wrapper for the native PHP session functions. It provides several presets for the most common session configuration via external handlers and helps with using sessions in CLI without any warnings.
Sessions can be created from the defaults using Session::create() or you can get
an instance of a new session by just instantiating this class and passing the complete
options you want to use.
When specific options are omitted, this class will take its defaults from the configuration
values from the session.* directives in php.ini. This class will also alter such
directives when configuration values are provided.
PSR-7 stream implementation wrapping a PHP stream resource.
Factory class for creating stream instances.
PSR-7 uploaded file implementation.
Factory class for creating uploaded file instances.
Immutable PSR-7 URI using PHP's RFC 3986 parser.
Components preserve percent-encoded sequences and dot segments. Non-ASCII input is percent-encoded before parsing.
Factory class for creating uri instances.
Shared validation for PSR-7 message header mutations.
Implements the methods defined in Psr\Http\Message\MessageInterface.