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): self
withJson()
Create a response with JSON content.
public static function withJson(
mixed $data,
int|Status $status = Status::OK,
array $headers = [],
int $options = 0
): self
noContent()
Create a 204 No Content response.
public static function noContent(array $headers = []): self
created()
Create a 201 Created response with optional JSON body.
public static function created(
string $location,
mixed $data = null,
array $headers = []
): self
withRedirect()
Create a redirect response.
public static function withRedirect(
string $location,
int|Status $status = Status::FOUND,
array $headers = []
): self
Response 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): mixed
object()
Get the body as a JSON-decoded object.
public function object(bool $throwOnError = true): object
array()
Get the body as a JSON-decoded array.
public function array(bool $throwOnError = true): array
text()
Get the body as plain text.
public function text(): string
body()
Get the raw body content.
public function body(): string
blob()
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(): string
xml()
Parse the body as XML.
public function xml(int $options = 0, bool $throwOnError = true): ?SimpleXMLElement
Status Code Methods
status()
Get the status code of the response.
public function status(): int
statusText()
Get the status text for the response (e.g., "OK").
public function statusText(): string
statusEnum()
Get the status as an enum.
public function statusEnum(): ?Status
isStatus()
Check if the response has the given status code.
public function isStatus(int|Status $status): bool
Status Category Methods
isInformational()
Check if the response status code is informational (1xx).
public function isInformational(): bool
ok()
/ successful()
Check if the response status code is a success (2xx).
public function ok(): bool
public function successful(): bool
isRedirection()
/ redirect()
Check if the response status code is a redirection (3xx).
public function isRedirection(): bool
public function redirect(): bool
isClientError()
/ clientError()
Check if the response status code is a client error (4xx).
public function isClientError(): bool
public function clientError(): bool
isServerError()
/ serverError()
Check if the response status code is a server error (5xx).
public function isServerError(): bool
public function serverError(): bool
failed()
Determine if the response is a client or server error.
public function failed(): bool
Specific Status Code Methods
isOk()
Check if the response has a 200 status code.
public function isOk(): bool
isCreated()
Check if the response has a 201 status code.
public function isCreated(): bool
isAccepted()
Check if the response has a 202 status code.
public function isAccepted(): bool
isNoContent()
Check if the response has a 204 status code.
public function isNoContent(): bool
isMovedPermanently()
Check if the response has a 301 status code.
public function isMovedPermanently(): bool
isFound()
Check if the response has a 302 status code.
public function isFound(): bool
isBadRequest()
Check if the response has a 400 status code.
public function isBadRequest(): bool
isUnauthorized()
Check if the response has a 401 status code.
public function isUnauthorized(): bool
isForbidden()
Check if the response has a 403 status code.
public function isForbidden(): bool
isNotFound()
Check if the response has a 404 status code.
public function isNotFound(): bool
isConflict()
Check if the response has a 409 status code.
public function isConflict(): bool
isUnprocessableEntity()
Check if the response has a 422 status code.
public function isUnprocessableEntity(): bool
isTooManyRequests()
Check if the response has a 429 status code.
public function isTooManyRequests(): bool
isInternalServerError()
Check if the response has a 500 status code.
public function isInternalServerError(): bool
isServiceUnavailable()
Check if the response has a 503 status code.
public function isServiceUnavailable(): bool
Header Methods
headers()
Get the headers from the response as an array.
public function headers(): array
header()
Get a specific header from the response.
public function header(string $header): ?string
hasHeader()
Determine if the response contains a specific header.
public function hasHeader($header): bool
Content Type Methods
contentType()
Get the Content-Type header from the response.
public function contentType(): ?string
contentTypeEnum()
Get the Content-Type as an enum.
public function contentTypeEnum(): ?ContentType
hasJsonContent()
Check if the response has JSON content.
public function hasJsonContent(): bool
hasHtmlContent()
Check if the response has HTML content.
public function hasHtmlContent(): bool
hasTextContent()
Check if the response has text content.
public function hasTextContent(): bool
ArrayAccess Implementation
offsetExists()
Determine if the given offset exists in the JSON response.
public function offsetExists($offset): bool
offsetGet()
Get the value at the given offset from the JSON response.
public function offsetGet($offset): mixed
offsetSet()
Set the value at the given offset in the JSON response (unsupported).
public function offsetSet($offset, $value): void
offsetUnset()
Unset the value at the given offset from the JSON response (unsupported).
public function offsetUnset($offset): void
Utility 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(): string
PSR-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 = ''): static
withAddedHeader()
Return an instance with the specified header appended with the given value.
public function withAddedHeader($name, $value): static
withoutHeader()
Return an instance without the specified header.
public function withoutHeader($name): static
withHeader()
Return an instance with the provided value replacing the specified header.
public function withHeader($name, $value): static
withProtocolVersion()
Return an instance with the specified protocol version.
public function withProtocolVersion($version): static
withBody()
Return an instance with the specified body.
public function withBody(StreamInterface $body): static