class Response extends Response

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected mixed $decoded

The decoded JSON response.

from  Response
protected bool $decodedJson

Indicates if the JSON response has been decoded.

from  Response
protected JSON_THROW_ON_ERROR> $decodingFlags

The flags that were used when decoding the JSON response.

from  Response
protected Closure|null $decodeUsing

The custom decode callback.

from  Response
CookieJar|null $cookies

The request cookies.

from  Response
TransferStats|null $transferStats

The transfer stats for the request.

from  Response
protected false|int|null $truncateExceptionsAt

The length at which request exceptions will be truncated.

from  Response
static JSON_THROW_ON_ERROR> $defaultJsonDecodingFlags

The flags passed to json_decode by default.

from  Response
protected bool $cached

Whether the response came from the cache.

protected bool $mocked

Whether the response came from a mock client.

protected FakeResponse|null $fakeResponse

The fake response that produced this response.

protected TDto> $pendingRequest

The pending request that produced the response.

protected RequestInterface $psrRequest

The final application-owned PSR request.

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 handle calls to the class.

__construct(ResponseInterface $response)

Create a new response instance.

from  Response
string
body()

Get the response body.

lines()

Read lines from the current response body position without rewinding.

from  Response
jsonLines(int|null $flags = null)

Decode non-empty JSON lines from the current response body position.

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

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

from  Response
mixed
object(int|null $flags = null)

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

from  Response
decodeUsing(Closure|null $callback)

Set a custom decode callback.

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

Decode the given response body.

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

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

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

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

from  Response
mixed
resource()

Get the body of the response as a PHP resource.

from  Response
string
header(string $header)

Get a header from the response.

from  Response
array
headers()

Get the headers from the response.

from  Response
int
status()

Get the status code of the response.

from  Response
string
reason()

Get the reason phrase of the response.

from  Response
UriInterface|null
effectiveUri()

Get the effective URI of the response.

from  Response
bool
successful()

Determine if the request was successful.

from  Response
bool
redirect()

Determine if the response was a redirect.

from  Response
bool
failed()

Determine if the integration considers the response failed.

bool
clientError()

Determine if the response indicates a client error occurred.

from  Response
bool
serverError()

Determine if the response indicates a server error occurred.

from  Response
onError(callable $callback)

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

from  Response
CookieJar|null
cookies()

Get the response cookies.

from  Response
array
handlerStats()

Get the handler stats of the response.

from  Response
close()

Close the stream and any underlying resources.

from  Response
ResponseInterface
toPsrResponse()

Get the underlying PSR response for the response.

from  Response
toException()

Create an exception when the integration considers the response throwable.

newRequestException()

Create a new request exception for the response.

throw(callable|null $callback = null)

Throw an exception when the integration considers the response throwable.

throwIf(bool|Closure $condition, callable|null $callback = null)

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

from  Response
throwUnless(bool|Closure $condition, callable|null $callback = null)

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

from  Response
throwIfStatus(int|callable $statusCode)

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

from  Response
throwUnlessStatus(int|callable $statusCode)

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

from  Response
throwIfClientError()

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

from  Response
throwIfServerError()

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

from  Response
truncateExceptionsAt(int $length)

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

from  Response
dontTruncateExceptions()

Indicate that request exceptions should not be truncated.

from  Response
dump(string|null $key = null)

Dump the content from the response.

from  Response
never
dd(string|null $key = null)

Dump the content from the response and end the script.

from  Response
dumpHeaders()

Dump the headers from the response.

from  Response
never
ddHeaders()

Dump the headers from the response and end the script.

from  Response
bool
offsetExists(string $offset)

Determine if the given offset exists.

from  Response
mixed
offsetGet(string $offset)

Get the value for a given offset.

from  Response
void
offsetSet(string $offset, mixed $value)

Set the value at the given offset.

from  Response
void
offsetUnset(string $offset)

Unset the value at the given offset.

from  Response
static void
flushState()

Flush all static state.

from  Response
string
__toString()

Get the body of the response.

from  Response
static Response
fromResponse(Response $response, PendingRequest $pendingRequest, RequestInterface $psrRequest)

Create a Saloon response from a framework response.

StreamInterface
stream()

Get the response body stream.

pendingRequest()

Get the pending request.

connector()

Get the connector.

request()

Get the original request.

RequestInterface
toPsrRequest()

Get the final application-owned PSR request.

bool
shouldThrowRequestException()

Determine if the response should throw a request exception.

mixed
dto()

Convert the response into a data object.

mixed
dtoOrFail()

Convert the response into a data object or fail.

xml(mixed ...$arguments)

Convert the XML response into a SimpleXMLElement.

Crawler
dom()

Parse the HTML or XML response into a DOM crawler.

string
dataUrl()

Convert the response into a data URL.

mixed
getRawStream()

Create a temporary resource containing the response body.

void
saveBodyToFile(mixed $resourceOrPath, bool $closeResource = true)

Save the response body to a path or resource.

bool
isCached()

Determine if the response came from the cache.

setCached(bool $cached)

Set whether the response came from the cache.

bool
isMocked()

Determine if the response came from a mock client.

setMocked(bool $mocked)

Set whether the response came from a mock client.

bool
isFaked()

Determine if the response came from a short-circuit source.

setFakeResponse(FakeResponse $fakeResponse)

Set the fake response that produced this response.

fakeResponse()

Get the fake response that produced this 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 17
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

in Macroable at line 103
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Response at line 84
__construct(ResponseInterface $response)

Create a new response instance.

Parameters

ResponseInterface $response

at line 78
string body()

Get the response body.

Return Value

string

in Response at line 101
Generator lines()

Read lines from the current response body position without rewinding.

Return Value

Generator

in Response at line 135
Generator jsonLines(int|null $flags = null)

Decode non-empty JSON lines from the current response body position.

Parameters

int|null $flags

Return Value

Generator

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

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

Parameters

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

Return Value

mixed

in Response at line 185
mixed object(int|null $flags = null)

Get the decoded JSON 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. Scalar JSON values remain their decoded scalar type.

Parameters

int|null $flags

Return Value

mixed

in Response at line 201
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: Whether JSON objects should be decoded as objects.

Parameters

Closure|null $callback

Return Value

Response

in Response at line 216
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

in Response at line 230
Collection collect(string|null $key = null, int|null $flags = null)

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

Parameters

string|null $key
int|null $flags

Return Value

Collection

in Response at line 240
Fluent fluent(string|null $key = null, int|null $flags = null)

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

Parameters

string|null $key
int|null $flags

Return Value

Fluent

in Response at line 252
mixed resource()

Get the body of the response as a PHP resource.

Return Value

mixed

Exceptions

InvalidArgumentException

in Response at line 260
string header(string $header)

Get a header from the response.

Parameters

string $header

Return Value

string

in Response at line 268
array headers()

Get the headers from the response.

Return Value

array

in Response at line 276
int status()

Get the status code of the response.

Return Value

int

in Response at line 284
string reason()

Get the reason phrase of the response.

Return Value

string

in Response at line 292
UriInterface|null effectiveUri()

Get the effective URI of the response.

Return Value

UriInterface|null

in Response at line 300
bool successful()

Determine if the request was successful.

Return Value

bool

in Response at line 308
bool redirect()

Determine if the response was a redirect.

Return Value

bool

at line 144
bool failed()

Determine if the integration considers the response failed.

Return Value

bool

in Response at line 324
bool clientError()

Determine if the response indicates a client error occurred.

Return Value

bool

in Response at line 332
bool serverError()

Determine if the response indicates a server error occurred.

Return Value

bool

in Response at line 340
Response onError(callable $callback)

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

Parameters

callable $callback

Return Value

Response

in Response at line 352
CookieJar|null cookies()

Get the response cookies.

Return Value

CookieJar|null

in Response at line 360
array handlerStats()

Get the handler stats of the response.

Return Value

array

in Response at line 368
Response close()

Close the stream and any underlying resources.

Return Value

Response

in Response at line 378
ResponseInterface toPsrResponse()

Get the underlying PSR response for the response.

Return Value

ResponseInterface

at line 169
RequestException|null toException()

Create an exception when the integration considers the response throwable.

Return Value

RequestException|null

at line 198
protected RequestException newRequestException()

Create a new request exception for the response.

Return Value

RequestException

at line 180
Response throw(callable|null $callback = null)

Throw an exception when the integration considers the response throwable.

Parameters

callable|null $callback

Return Value

Response

Exceptions

RequestException

in Response at line 430
Response throwIf(bool|Closure $condition, callable|null $callback = null)

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

Parameters

bool|Closure $condition
callable|null $callback

Return Value

Response

Exceptions

RequestException

in Response at line 442
Response throwUnless(bool|Closure $condition, callable|null $callback = null)

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

Parameters

bool|Closure $condition
callable|null $callback

Return Value

Response

Exceptions

RequestException

in Response at line 452
Response throwIfStatus(int|callable $statusCode)

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

Parameters

int|callable $statusCode

Return Value

Response

Exceptions

RequestException

in Response at line 467
Response throwUnlessStatus(int|callable $statusCode)

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

Parameters

int|callable $statusCode

Return Value

Response

Exceptions

RequestException

in Response at line 481
Response throwIfClientError()

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

Return Value

Response

Exceptions

RequestException

in Response at line 491
Response throwIfServerError()

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

Return Value

Response

Exceptions

RequestException

in Response at line 499
Response truncateExceptionsAt(int $length)

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

Parameters

int $length

Return Value

Response

in Response at line 509
Response dontTruncateExceptions()

Indicate that request exceptions should not be truncated.

Return Value

Response

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

Dump the content from the response.

Parameters

string|null $key

Return Value

Response

in Response at line 543
never dd(string|null $key = null)

Dump the content from the response and end the script.

Parameters

string|null $key

Return Value

never

in Response at line 553
Response dumpHeaders()

Dump the headers from the response.

Return Value

Response

in Response at line 563
never ddHeaders()

Dump the headers from the response and end the script.

Return Value

never

in Response at line 575
bool offsetExists(string $offset)

Determine if the given offset exists.

Parameters

string $offset

Return Value

bool

in Response at line 585
mixed offsetGet(string $offset)

Get the value for a given offset.

Parameters

string $offset

Return Value

mixed

in Response at line 597
void offsetSet(string $offset, mixed $value)

Set the value at the given offset.

Parameters

string $offset
mixed $value

Return Value

void

Exceptions

LogicException

in Response at line 609
void offsetUnset(string $offset)

Unset the value at the given offset.

Parameters

string $offset

Return Value

void

Exceptions

LogicException

in Response at line 617
static void flushState()

Flush all static state.

Return Value

void

in Response at line 626
string __toString()

Get the body of the response.

Return Value

string

at line 56
static Response fromResponse(Response $response, PendingRequest $pendingRequest, RequestInterface $psrRequest)

Create a Saloon response from a framework response.

Parameters

Response $response
PendingRequest $pendingRequest
RequestInterface $psrRequest

Return Value

Response

at line 100
StreamInterface stream()

Get the response body stream.

Return Value

StreamInterface

at line 110
PendingRequest pendingRequest()

Get the pending request.

Return Value

PendingRequest

at line 118
Connector connector()

Get the connector.

Return Value

Connector

at line 128
Request request()

Get the original request.

Return Value

Request

at line 136
RequestInterface toPsrRequest()

Get the final application-owned PSR request.

Return Value

RequestInterface

at line 160
bool shouldThrowRequestException()

Determine if the response should throw a request exception.

Return Value

bool

at line 219
mixed dto()

Convert the response into a data object.

Return Value

mixed

at line 236
mixed dtoOrFail()

Convert the response into a data object or fail.

Return Value

mixed

at line 254
SimpleXMLElement|false xml(mixed ...$arguments)

Convert the XML response into a SimpleXMLElement.

Parameters

mixed ...$arguments

Return Value

SimpleXMLElement|false

See also

https://www.php.net/manual/en/book.simplexml.php

at line 262
Crawler dom()

Parse the HTML or XML response into a DOM crawler.

Return Value

Crawler

at line 270
string dataUrl()

Convert the response into a data URL.

Return Value

string

at line 280
mixed getRawStream()

Create a temporary resource containing the response body.

Return Value

mixed

at line 298
void saveBodyToFile(mixed $resourceOrPath, bool $closeResource = true)

Save the response body to a path or resource.

Parameters

mixed $resourceOrPath
bool $closeResource

Return Value

void

at line 346
bool isCached()

Determine if the response came from the cache.

Return Value

bool

at line 356
Response setCached(bool $cached)

Set whether the response came from the cache.

Parameters

bool $cached

Return Value

Response

at line 366
bool isMocked()

Determine if the response came from a mock client.

Return Value

bool

at line 376
Response setMocked(bool $mocked)

Set whether the response came from a mock client.

Parameters

bool $mocked

Return Value

Response

at line 386
bool isFaked()

Determine if the response came from a short-circuit source.

Return Value

bool

at line 396
Response setFakeResponse(FakeResponse $fakeResponse)

Set the fake response that produced this response.

Parameters

FakeResponse $fakeResponse

Return Value

Response

at line 406
FakeResponse|null fakeResponse()

Get the fake response that produced this response.

Return Value

FakeResponse|null