Response API Reference
The complete API reference for the Response class in the Fetch HTTP client package.
Class Declaration
namespace Fetch\Http;
class Response extends BaseResponse implements ArrayAccess, ResponseInterface
{
use ResponseImmutabilityTrait;
// ...
}Constructor
/**
* Create new response instance.
*/
public function __construct(
int|Status $status = Status::OK,
array $headers = [],
string $body = '',
string $version = '1.1',
?string $reason = null
)Static Factory Methods
createFromBase()
Create a response from a PSR-7 response instance.
public static function createFromBase(PsrResponseInterface $response): selfwithJson()
Create a response with JSON content.
public static function withJson(
mixed $data,
int|Status $status = Status::OK,
array $headers = [],
int $options = 0
): selfnoContent()
Create a 204 No Content response.
public static function noContent(array $headers = []): selfcreated()
Create a 201 Created response with optional JSON body.
public static function created(
string $location,
mixed $data = null,
array $headers = []
): selfwithRedirect()
Create a redirect response.
public static function withRedirect(
string $location,
int|Status $status = Status::FOUND,
array $headers = []
): selfResponse Body Methods
json()
Get the body as a JSON-decoded array or object.
public function json(bool $assoc = true, bool $throwOnError = true, int $depth = 512, int $options = 0): mixedobject()
Get the body as a JSON-decoded object.
public function object(bool $throwOnError = true): objectarray()
Get the body as a JSON-decoded array.
public function array(bool $throwOnError = true): arraytext()
Get the body as plain text.
public function text(): stringbody()
Get the raw body content.
public function body(): stringblob()
Get the body as a stream (simulating a "blob" in JavaScript).
public function blob()arrayBuffer()
Get the body as an array buffer (binary data).
public function arrayBuffer(): stringxml()
Parse the body as XML.
public function xml(int $options = 0, bool $throwOnError = true): ?SimpleXMLElementStatus Code Methods
status()
Get the status code of the response.
public function status(): intstatusText()
Get the status text for the response (e.g., "OK").
public function statusText(): stringstatusEnum()
Get the status as an enum.
public function statusEnum(): ?StatusisStatus()
Check if the response has the given status code.
public function isStatus(int|Status $status): boolStatus Category Methods
isInformational()
Check if the response status code is informational (1xx).
public function isInformational(): boolok() / successful()
Check if the response status code is a success (2xx).
public function ok(): bool
public function successful(): boolisRedirection() / redirect()
Check if the response status code is a redirection (3xx).
public function isRedirection(): bool
public function redirect(): boolisClientError() / clientError()
Check if the response status code is a client error (4xx).
public function isClientError(): bool
public function clientError(): boolisServerError() / serverError()
Check if the response status code is a server error (5xx).
public function isServerError(): bool
public function serverError(): boolfailed()
Determine if the response is a client or server error.
public function failed(): boolSpecific Status Code Methods
isOk()
Check if the response has a 200 status code.
public function isOk(): boolisCreated()
Check if the response has a 201 status code.
public function isCreated(): boolisAccepted()
Check if the response has a 202 status code.
public function isAccepted(): boolisNoContent()
Check if the response has a 204 status code.
public function isNoContent(): boolisMovedPermanently()
Check if the response has a 301 status code.
public function isMovedPermanently(): boolisFound()
Check if the response has a 302 status code.
public function isFound(): boolisBadRequest()
Check if the response has a 400 status code.
public function isBadRequest(): boolisUnauthorized()
Check if the response has a 401 status code.
public function isUnauthorized(): boolisForbidden()
Check if the response has a 403 status code.
public function isForbidden(): boolisNotFound()
Check if the response has a 404 status code.
public function isNotFound(): boolisConflict()
Check if the response has a 409 status code.
public function isConflict(): boolisUnprocessableEntity()
Check if the response has a 422 status code.
public function isUnprocessableEntity(): boolisTooManyRequests()
Check if the response has a 429 status code.
public function isTooManyRequests(): boolisInternalServerError()
Check if the response has a 500 status code.
public function isInternalServerError(): boolisServiceUnavailable()
Check if the response has a 503 status code.
public function isServiceUnavailable(): boolHeader Methods
headers()
Get the headers from the response as an array.
public function headers(): arrayheader()
Get a specific header from the response.
public function header(string $header): ?stringhasHeader()
Determine if the response contains a specific header.
public function hasHeader($header): boolContent Type Methods
contentType()
Get the Content-Type header from the response.
public function contentType(): ?stringcontentTypeEnum()
Get the Content-Type as an enum.
public function contentTypeEnum(): ?ContentTypehasJsonContent()
Check if the response has JSON content.
public function hasJsonContent(): boolhasHtmlContent()
Check if the response has HTML content.
public function hasHtmlContent(): boolhasTextContent()
Check if the response has text content.
public function hasTextContent(): boolDebug Information
When withDebug() is enabled on the handler, every response stores a per-request snapshot.
withDebugInfo()
Attach a DebugInfo instance (used internally by the handler).
public function withDebugInfo(\Fetch\Support\DebugInfo $debugInfo): staticgetDebugInfo()
Retrieve the DebugInfo for this response, or null when debug mode was disabled.
public function getDebugInfo(): ?\Fetch\Support\DebugInfohasDebugInfo()
Convenience method to check whether debug data is present.
public function hasDebugInfo(): boolArrayAccess Implementation
offsetExists()
Determine if the given offset exists in the JSON response.
public function offsetExists($offset): booloffsetGet()
Get the value at the given offset from the JSON response.
public function offsetGet($offset): mixedoffsetSet()
Set the value at the given offset in the JSON response (unsupported).
public function offsetSet($offset, $value): voidoffsetUnset()
Unset the value at the given offset from the JSON response (unsupported).
public function offsetUnset($offset): voidUtility Methods
get()
Get the value for a given key from the JSON response.
public function get(string $key, mixed $default = null): mixed__toString()
Get the body of the response when converting to string.
public function __toString(): stringPSR-7 Methods (from ResponseImmutabilityTrait)
These methods override the PSR-7 response methods to ensure immutability and proper type preservation.
withStatus()
Return an instance with the specified status code and reason phrase.
public function withStatus($code, $reasonPhrase = ''): staticwithAddedHeader()
Return an instance with the specified header appended with the given value.
public function withAddedHeader($name, $value): staticwithoutHeader()
Return an instance without the specified header.
public function withoutHeader($name): staticwithHeader()
Return an instance with the provided value replacing the specified header.
public function withHeader($name, $value): staticwithProtocolVersion()
Return an instance with the specified protocol version.
public function withProtocolVersion($version): staticwithBody()
Return an instance with the specified body.
public function withBody(StreamInterface $body): static