class Response implements ArrayAccess, Stringable mixin ResponseInterface

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected mixed $decoded

The decoded JSON response.

protected bool $hasDecoded

Whether the response body has been decoded.

protected int $decodingFlags

The flags that were used when decoding the JSON response.

protected Closure|null $decodeUsing

The custom decode callback.

CookieJar|null $cookies

The request cookies.

TransferStats|null $transferStats

The transfer stats for the request.

protected false|int|null $truncateExceptionsAt

The length at which request exceptions will be truncated.

static int $defaultJsonDecodingFlags

The flags passed to json_decode by default.

Methods

bool
ok()

Determine if the response code was 200 "OK" response.

bool
created()

Determine if the response code was 201 "Created" response.

bool
accepted()

Determine if the response code was 202 "Accepted" response.

bool
noContent(int $status = 204)

Determine if the response code was the given status code and the body has no content.

bool
movedPermanently()

Determine if the response code was a 301 "Moved Permanently".

bool
found()

Determine if the response code was a 302 "Found" response.

bool
notModified()

Determine if the response code was a 304 "Not Modified" response.

bool
badRequest()

Determine if the response was a 400 "Bad Request" response.

bool
unauthorized()

Determine if the response was a 401 "Unauthorized" response.

bool
paymentRequired()

Determine if the response was a 402 "Payment Required" response.

bool
forbidden()

Determine if the response was a 403 "Forbidden" response.

bool
notFound()

Determine if the response was a 404 "Not Found" response.

bool
requestTimeout()

Determine if the response was a 408 "Request Timeout" response.

bool
conflict()

Determine if the response was a 409 "Conflict" response.

bool
unprocessableContent()

Determine if the response was a 422 "Unprocessable Content" response.

bool
unprocessableEntity()

Determine if the response was a 422 "Unprocessable Content" response.

bool
tooManyRequests()

Determine if the response was a 429 "Too Many Requests" response.

mixed
tap(callable|null $callback = null)

Call the given Closure with this instance then return the instance.

from  Tappable
static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically proxy other methods to the underlying response.

__construct(ResponseInterface $response)

Create a new response instance.

string
body()

Get the body of the response.

mixed
json(string|null $key = null, mixed $default = null, int|null $flags = null)

Get the JSON decoded body of the response as an array or scalar value.

array|object|null
object(int|null $flags = null)

Get the JSON decoded body of the response as an object.

decodeUsing(Closure|null $callback)

Set a custom decode callback.

mixed
decode(string $body, bool $asObject = false, int $flags = 0)

Decode the given response body.

collect(string|null $key = null, int|null $flags = null)

Get the JSON decoded body of the response as a collection.

fluent(string|null $key = null, int|null $flags = null)

Get the JSON decoded body of the response as a fluent object.

resource
resource()

Get the body of the response as a PHP resource.

string
header(string $header)

Get a header from the response.

array
headers()

Get the headers from the response.

int
status()

Get the status code of the response.

string
reason()

Get the reason phrase of the response.

UriInterface|null
effectiveUri()

Get the effective URI of the response.

bool
successful()

Determine if the request was successful.

bool
redirect()

Determine if the response was a redirect.

bool
failed()

Determine if the response indicates a client or server error occurred.

bool
clientError()

Determine if the response indicates a client error occurred.

bool
serverError()

Determine if the response indicates a server error occurred.

onError(callable $callback)

Execute the given callback if there was a server or client error.

CookieJar
cookies()

Get the response cookies.

array
handlerStats()

Get the handler stats of the response.

close()

Close the stream and any underlying resources.

ResponseInterface
toPsrResponse()

Get the underlying PSR response for the response.

toException()

Create an exception if a server or client error occurred.

throw()

Throw an exception if a server or client error occurred.

throwIf(bool|Closure $condition)

Throw an exception if a server or client error occurred and the given condition evaluates to true.

throwUnless(bool|Closure $condition)

Throw an exception if a server or client error occurred and the given condition evaluates to false.

throwIfStatus(callable|int $statusCode)

Throw an exception if the response status code matches the given code.

throwUnlessStatus(callable|int $statusCode)

Throw an exception unless the response status code matches the given code.

throwIfClientError()

Throw an exception if the response status code is a 4xx level code.

throwIfServerError()

Throw an exception if the response status code is a 5xx level code.

truncateExceptionsAt(int $length)

Indicate that request exceptions should be truncated to the given length.

dontTruncateExceptions()

Indicate that request exceptions should not be truncated.

dump(string|null $key = null)

Dump the content from the response.

void
dd(string|null $key = null)

Dump the content from the response and end the script.

dumpHeaders()

Dump the headers from the response.

never
ddHeaders()

Dump the headers from the response and end the script.

bool
offsetExists(string $offset)

Determine if the given offset exists.

mixed
offsetGet(string $offset)

Get the value for a given offset.

void
offsetSet(string $offset, mixed $value)

Set the value at the given offset.

void
offsetUnset(string $offset)

Unset the value at the given offset.

static void
flushState()

Flush all static state.

string
__toString()

Get the body of the response.

Details

bool ok()

Determine if the response code was 200 "OK" response.

Return Value

bool

bool created()

Determine if the response code was 201 "Created" response.

Return Value

bool

bool accepted()

Determine if the response code was 202 "Accepted" response.

Return Value

bool

bool noContent(int $status = 204)

Determine if the response code was the given status code and the body has no content.

Parameters

int $status

Return Value

bool

bool movedPermanently()

Determine if the response code was a 301 "Moved Permanently".

Return Value

bool

bool found()

Determine if the response code was a 302 "Found" response.

Return Value

bool

bool notModified()

Determine if the response code was a 304 "Not Modified" response.

Return Value

bool

bool badRequest()

Determine if the response was a 400 "Bad Request" response.

Return Value

bool

bool unauthorized()

Determine if the response was a 401 "Unauthorized" response.

Return Value

bool

bool paymentRequired()

Determine if the response was a 402 "Payment Required" response.

Return Value

bool

bool forbidden()

Determine if the response was a 403 "Forbidden" response.

Return Value

bool

in DeterminesStatusCode at line 100
bool notFound()

Determine if the response was a 404 "Not Found" response.

Return Value

bool

in DeterminesStatusCode at line 108
bool requestTimeout()

Determine if the response was a 408 "Request Timeout" response.

Return Value

bool

in DeterminesStatusCode at line 116
bool conflict()

Determine if the response was a 409 "Conflict" response.

Return Value

bool

in DeterminesStatusCode at line 124
bool unprocessableContent()

Determine if the response was a 422 "Unprocessable Content" response.

Return Value

bool

in DeterminesStatusCode at line 132
bool unprocessableEntity()

Determine if the response was a 422 "Unprocessable Content" response.

Return Value

bool

in DeterminesStatusCode at line 140
bool tooManyRequests()

Determine if the response was a 429 "Too Many Requests" response.

Return Value

bool

in Tappable at line 15
mixed tap(callable|null $callback = null)

Call the given Closure with this instance then return the instance.

Parameters

callable|null $callback

Return Value

mixed

in Macroable at line 28
static void macro(string $name, callable|object $macro)

Register a custom macro.

Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 41
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 57
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 68
static void flushMacros()

Flush the existing macros.

Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 78
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

at line 567
mixed __call(string $method, array $parameters)

Dynamically proxy other methods to the underlying response.

Parameters

string $method
array $parameters

Return Value

mixed

at line 75
__construct(ResponseInterface $response)

Create a new response instance.

Parameters

ResponseInterface $response

at line 82
string body()

Get the body of the response.

Return Value

string

at line 90
mixed json(string|null $key = null, mixed $default = null, int|null $flags = null)

Get the JSON decoded body of the response as an array or scalar value.

Parameters

string|null $key
mixed $default
int|null $flags

Return Value

mixed

at line 121
array|object|null object(int|null $flags = null)

Get the JSON decoded body of the response as an object.

Example: If the response body is: [ {"id": 1, "name": "John"}, {"id": 2, "name": "Jane"} ]

This method will return an array of objects.

Parameters

int|null $flags

Return Value

array|object|null

at line 137
Response decodeUsing(Closure|null $callback)

Set a custom decode callback.

The callback will be invoked with the following parameters:

  • string $body: The raw response body.
  • bool $asObject: When true, the decoder should return an array of objects.

Parameters

Closure|null $callback

Return Value

Response

at line 150
protected mixed decode(string $body, bool $asObject = false, int $flags = 0)

Decode the given response body.

Parameters

string $body
bool $asObject
int $flags

Return Value

mixed

at line 162
Collection collect(string|null $key = null, int|null $flags = null)

Get the JSON decoded body of the response as a collection.

Parameters

string|null $key
int|null $flags

Return Value

Collection

at line 170
Fluent fluent(string|null $key = null, int|null $flags = null)

Get the JSON decoded body of the response as a fluent object.

Parameters

string|null $key
int|null $flags

Return Value

Fluent

at line 182
resource resource()

Get the body of the response as a PHP resource.

Return Value

resource

Exceptions

InvalidArgumentException

at line 190
string header(string $header)

Get a header from the response.

Parameters

string $header

Return Value

string

at line 198
array headers()

Get the headers from the response.

Return Value

array

at line 206
int status()

Get the status code of the response.

Return Value

int

at line 214
string reason()

Get the reason phrase of the response.

Return Value

string

at line 222
UriInterface|null effectiveUri()

Get the effective URI of the response.

Return Value

UriInterface|null

at line 230
bool successful()

Determine if the request was successful.

Return Value

bool

at line 238
bool redirect()

Determine if the response was a redirect.

Return Value

bool

at line 246
bool failed()

Determine if the response indicates a client or server error occurred.

Return Value

bool

at line 254
bool clientError()

Determine if the response indicates a client error occurred.

Return Value

bool

at line 262
bool serverError()

Determine if the response indicates a server error occurred.

Return Value

bool

at line 270
Response onError(callable $callback)

Execute the given callback if there was a server or client error.

Parameters

callable $callback

Return Value

Response

at line 282
CookieJar cookies()

Get the response cookies.

Return Value

CookieJar

at line 290
array handlerStats()

Get the handler stats of the response.

Return Value

array

at line 298
Response close()

Close the stream and any underlying resources.

Return Value

Response

at line 308
ResponseInterface toPsrResponse()

Get the underlying PSR response for the response.

Return Value

ResponseInterface

at line 316
RequestException|null toException()

Create an exception if a server or client error occurred.

Return Value

RequestException|null

at line 330
Response throw()

Throw an exception if a server or client error occurred.

Return Value

Response

Exceptions

RequestException

at line 350
Response throwIf(bool|Closure $condition)

Throw an exception if a server or client error occurred and the given condition evaluates to true.

Parameters

bool|Closure $condition

Return Value

Response

Exceptions

RequestException

at line 360
Response throwUnless(bool|Closure $condition)

Throw an exception if a server or client error occurred and the given condition evaluates to false.

Parameters

bool|Closure $condition

Return Value

Response

Exceptions

RequestException

at line 372
Response throwIfStatus(callable|int $statusCode)

Throw an exception if the response status code matches the given code.

Parameters

callable|int $statusCode

Return Value

Response

Exceptions

RequestException

at line 389
Response throwUnlessStatus(callable|int $statusCode)

Throw an exception unless the response status code matches the given code.

Parameters

callable|int $statusCode

Return Value

Response

Exceptions

RequestException

at line 403
Response throwIfClientError()

Throw an exception if the response status code is a 4xx level code.

Return Value

Response

Exceptions

RequestException

at line 413
Response throwIfServerError()

Throw an exception if the response status code is a 5xx level code.

Return Value

Response

Exceptions

RequestException

at line 421
Response truncateExceptionsAt(int $length)

Indicate that request exceptions should be truncated to the given length.

Parameters

int $length

Return Value

Response

at line 431
Response dontTruncateExceptions()

Indicate that request exceptions should not be truncated.

Return Value

Response

at line 443
Response dump(string|null $key = null)

Dump the content from the response.

Parameters

string|null $key

Return Value

Response

at line 467
void dd(string|null $key = null)

Dump the content from the response and end the script.

Parameters

string|null $key

Return Value

void

at line 477
Response dumpHeaders()

Dump the headers from the response.

Return Value

Response

at line 489
never ddHeaders()

Dump the headers from the response and end the script.

Return Value

never

at line 501
bool offsetExists(string $offset)

Determine if the given offset exists.

Parameters

string $offset

Return Value

bool

at line 511
mixed offsetGet(string $offset)

Get the value for a given offset.

Parameters

string $offset

Return Value

mixed

at line 523
void offsetSet(string $offset, mixed $value)

Set the value at the given offset.

Parameters

string $offset
mixed $value

Return Value

void

Exceptions

LogicException

at line 535
void offsetUnset(string $offset)

Unset the value at the given offset.

Parameters

string $offset

Return Value

void

Exceptions

LogicException

at line 543
static void flushState()

Flush all static state.

Return Value

void

at line 554
string __toString()

Get the body of the response.

Return Value

string