class PendingRequest (View source)

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected Factory|null $factory

The factory instance.

protected Client $client

The Guzzle client instance.

protected callable $handler

The Guzzle HTTP handler.

protected string $baseUrl

The base URL for the request.

protected array $urlParameters

The parameters that can be substituted into the URL.

protected string $bodyFormat

The request body format.

protected StreamInterface|string $pendingBody

The raw body for the request.

protected array $pendingFiles

The pending files for the request.

protected array $cookies

The request cookies.

protected TransferStats $transferStats

The transfer stats for the request.

protected array $options

The request options.

protected Closure $throwCallback

A callback to run when throwing if a server or client error occurs.

protected Closure $throwIfCallback

A callback to check if an exception should be thrown when a server or client error occurs.

protected int $tries

The number of times to try the request.

protected Closure|int $retryDelay

The number of milliseconds to wait between retries.

protected bool $retryThrow

Whether to throw an exception when all retries fail.

protected callable|null $retryWhenCallback

The callback that will determine if the request should be retried.

protected Collection $beforeSendingCallbacks

The callbacks that should execute before the request is sent.

protected Collection|null $stubCallbacks

The stub callables that will handle requests.

protected bool $preventStrayRequests

Indicates that an exception should be thrown if any request is not faked.

protected Collection $middleware

The middleware callables added by users that will handle requests.

protected bool $async

Whether the requests should be asynchronous.

protected PromiseInterface $promise

The pending request promise.

protected Request|null $request

The sent request object, if a request has been made.

protected array $mergeableOptions

The Guzzle request options that are mergeable via array_merge_recursive.

Methods

$this|TWhenReturnType
when($value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

$this|TUnlessReturnType
unless($value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

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

Register a custom macro.

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

Mix another object into the class.

static bool
hasMacro(string $name)

Checks 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.

void
__construct(Factory|null $factory = null, array $middleware = [])

Create a new HTTP Client instance.

$this
baseUrl(string $url)

Set the base URL for the pending request.

$this
withBody(StreamInterface|string $content, string $contentType = 'application/json')

Attach a raw body to the request.

$this
asJson()

Indicate the request contains JSON.

$this
asForm()

Indicate the request contains form parameters.

$this
attach(string|array $name, string|resource $contents = '', string|null $filename = null, array $headers = [])

Attach a file to the request.

$this
asMultipart()

Indicate the request is a multi-part form request.

$this
bodyFormat(string $format)

Specify the body format of the request.

$this
withQueryParameters(array $parameters)

Set the given query parameters in the request URI.

$this
contentType(string $contentType)

Specify the request's content type.

$this
acceptJson()

Indicate that JSON should be returned by the server.

$this
accept(string $contentType)

Indicate the type of content that should be returned by the server.

$this
withHeaders(array $headers)

Add the given headers to the request.

$this
withHeader(string $name, mixed $value)

Add the given header to the request.

$this
replaceHeaders(array $headers)

Replace the given headers on the request.

$this
withBasicAuth(string $username, string $password)

Specify the basic authentication username and password for the request.

$this
withDigestAuth(string $username, string $password)

Specify the digest authentication username and password for the request.

$this
withToken(string $token, string $type = 'Bearer')

Specify an authorization token for the request.

$this
withUserAgent(string|bool $userAgent)

Specify the user agent for the request.

$this
withUrlParameters(array $parameters = [])

Specify the URL parameters that can be substituted into the request URL.

$this
withCookies(array $cookies, string $domain)

Specify the cookies that should be included with the request.

$this
maxRedirects(int $max)

Specify the maximum number of redirects to allow.

$this
withoutRedirecting()

Indicate that redirects should not be followed.

$this
withoutVerifying()

Indicate that TLS certificates should not be verified.

$this
sink(string|resource $to)

Specify the path where the body of the response should be stored.

$this
timeout(int|float $seconds)

Specify the timeout (in seconds) for the request.

$this
connectTimeout(int|float $seconds)

Specify the connect timeout (in seconds) for the request.

$this
retry(array|int $times, Closure|int $sleepMilliseconds = 0, callable|null $when = null, bool $throw = true)

Specify the number of times the request should be attempted.

$this
withOptions(array $options)

Replace the specified options on the request.

$this
withMiddleware(callable $middleware)

Add new middleware the client handler stack.

$this
withRequestMiddleware(callable $middleware)

Add new request middleware the client handler stack.

$this
withResponseMiddleware(callable $middleware)

Add new response middleware the client handler stack.

$this
beforeSending(callable $callback)

Add a new "before sending" callback to the request.

$this
throw(callable|null $callback = null)

Throw an exception if a server or client error occurs.

$this
throwIf(callable|bool $condition)

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

$this
throwUnless(callable|bool $condition)

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

$this
dump()

Dump the request before sending.

$this
dd()

Dump the request before sending and end the script.

get(string $url, array|string|null $query = null)

Issue a GET request to the given URL.

head(string $url, array|string|null $query = null)

Issue a HEAD request to the given URL.

post(string $url, array $data = [])

Issue a POST request to the given URL.

patch(string $url, array $data = [])

Issue a PATCH request to the given URL.

put(string $url, array $data = [])

Issue a PUT request to the given URL.

delete(string $url, array $data = [])

Issue a DELETE request to the given URL.

Response>
pool(callable $callback)

Send a pool of asynchronous requests concurrently.

send(string $method, string $url, array $options = [])

Send the request to the given URL.

string
expandUrlParameters(string $url)

Substitute the URL parameters in the given URL.

array
parseHttpOptions(array $options)

Parse the given HTTP options and set the appropriate additional options.

array|array[]
parseMultipartBodyFormat(array $data)

Parse multi-part form data.

PromiseInterface
makePromise(string $method, string $url, array $options = [], int $attempt = 1)

Send an asynchronous request to the given URL.

mixed
handlePromiseResponse(TransferException $response, string $method, string $url, array $options, int $attempt)

Handle the response of an asynchronous request.

MessageInterface|PromiseInterface
sendRequest(string $method, string $url, array $options = [])

Send a request either synchronously or asynchronously.

array
parseRequestData(string $method, string $url, array $options)

Get the request data as an array so that we can attach it to the request for convenient assertions.

array
normalizeRequestOptions(array $options)

Normalize the given request options.

populateResponse(Response $response)

Populate the given response with additional data.

Client
buildClient()

Build the Guzzle client.

bool
requestsReusableClient()

Determine if a reusable client is required.

Client
getReusableClient()

Retrieve a reusable Guzzle client.

Client
createClient(HandlerStack $handlerStack)

Create new Guzzle client.

HandlerStack
buildHandlerStack()

Build the Guzzle client handler stack.

HandlerStack
pushHandlers(HandlerStack $handlerStack)

Add the necessary handlers to the given handler stack.

buildBeforeSendingHandler()

Build the before sending handler.

buildRecorderHandler()

Build the recorder handler.

buildStubHandler()

Build the stub handler.

sinkStubHandler(string $sink)

Get the sink stub handler callback.

RequestInterface
runBeforeSendingCallbacks(RequestInterface $request, array $options)

Execute the "before sending" callbacks.

array
mergeOptions(array ...$options)

Replace the given options with the current request options.

newResponse(MessageInterface $response)

Create a new response instance using the given PSR response.

$this
stub(callable $callback)

Register a stub callable that will intercept requests and be able to return stub responses.

$this
preventStrayRequests(bool $prevent = true)

Indicate that an exception should be thrown if any request is not faked.

$this
async(bool $async = true)

Toggle asynchronicity in requests.

PromiseInterface|null
getPromise()

Retrieve the pending request promise.

void
dispatchRequestSendingEvent()

Dispatch the RequestSending event if a dispatcher is available.

void
dispatchResponseReceivedEvent(Response $response)

Dispatch the ResponseReceived event if a dispatcher is available.

void
dispatchConnectionFailedEvent(Request $request, ConnectionException $exception)

Dispatch the ConnectionFailed event if a dispatcher is available.

$this
setClient(Client $client)

Set the client instance.

$this
setHandler(callable $handler)

Create a new client instance using the given handler.

array
getOptions()

Get the pending request options.

Details

$this|TWhenReturnType when($value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

Parameters

$value
callable|null $callback
callable|null $default

Return Value

$this|TWhenReturnType

$this|TUnlessReturnType unless($value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

Parameters

$value
callable|null $callback
callable|null $default

Return Value

$this|TUnlessReturnType

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

Register a custom macro.

Parameters

string $name
object|callable $macro

Return Value

void

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

Mix another object into the class.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

static bool hasMacro(string $name)

Checks if macro is registered.

Parameters

string $name

Return Value

bool

static void flushMacros()

Flush the existing macros.

Return Value

void

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

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

void __construct(Factory|null $factory = null, array $middleware = [])

Create a new HTTP Client instance.

Parameters

Factory|null $factory
array $middleware

Return Value

void

$this baseUrl(string $url)

Set the base URL for the pending request.

Parameters

string $url

Return Value

$this

$this withBody(StreamInterface|string $content, string $contentType = 'application/json')

Attach a raw body to the request.

Parameters

StreamInterface|string $content
string $contentType

Return Value

$this

$this asJson()

Indicate the request contains JSON.

Return Value

$this

$this asForm()

Indicate the request contains form parameters.

Return Value

$this

$this attach(string|array $name, string|resource $contents = '', string|null $filename = null, array $headers = [])

Attach a file to the request.

Parameters

string|array $name
string|resource $contents
string|null $filename
array $headers

Return Value

$this

$this asMultipart()

Indicate the request is a multi-part form request.

Return Value

$this

$this bodyFormat(string $format)

Specify the body format of the request.

Parameters

string $format

Return Value

$this

$this withQueryParameters(array $parameters)

Set the given query parameters in the request URI.

Parameters

array $parameters

Return Value

$this

$this contentType(string $contentType)

Specify the request's content type.

Parameters

string $contentType

Return Value

$this

$this acceptJson()

Indicate that JSON should be returned by the server.

Return Value

$this

$this accept(string $contentType)

Indicate the type of content that should be returned by the server.

Parameters

string $contentType

Return Value

$this

$this withHeaders(array $headers)

Add the given headers to the request.

Parameters

array $headers

Return Value

$this

$this withHeader(string $name, mixed $value)

Add the given header to the request.

Parameters

string $name
mixed $value

Return Value

$this

$this replaceHeaders(array $headers)

Replace the given headers on the request.

Parameters

array $headers

Return Value

$this

$this withBasicAuth(string $username, string $password)

Specify the basic authentication username and password for the request.

Parameters

string $username
string $password

Return Value

$this

$this withDigestAuth(string $username, string $password)

Specify the digest authentication username and password for the request.

Parameters

string $username
string $password

Return Value

$this

$this withToken(string $token, string $type = 'Bearer')

Specify an authorization token for the request.

Parameters

string $token
string $type

Return Value

$this

$this withUserAgent(string|bool $userAgent)

Specify the user agent for the request.

Parameters

string|bool $userAgent

Return Value

$this

$this withUrlParameters(array $parameters = [])

Specify the URL parameters that can be substituted into the request URL.

Parameters

array $parameters

Return Value

$this

$this withCookies(array $cookies, string $domain)

Specify the cookies that should be included with the request.

Parameters

array $cookies
string $domain

Return Value

$this

$this maxRedirects(int $max)

Specify the maximum number of redirects to allow.

Parameters

int $max

Return Value

$this

$this withoutRedirecting()

Indicate that redirects should not be followed.

Return Value

$this

$this withoutVerifying()

Indicate that TLS certificates should not be verified.

Return Value

$this

$this sink(string|resource $to)

Specify the path where the body of the response should be stored.

Parameters

string|resource $to

Return Value

$this

$this timeout(int|float $seconds)

Specify the timeout (in seconds) for the request.

Parameters

int|float $seconds

Return Value

$this

$this connectTimeout(int|float $seconds)

Specify the connect timeout (in seconds) for the request.

Parameters

int|float $seconds

Return Value

$this

$this retry(array|int $times, Closure|int $sleepMilliseconds = 0, callable|null $when = null, bool $throw = true)

Specify the number of times the request should be attempted.

Parameters

array|int $times
Closure|int $sleepMilliseconds
callable|null $when
bool $throw

Return Value

$this

$this withOptions(array $options)

Replace the specified options on the request.

Parameters

array $options

Return Value

$this

$this withMiddleware(callable $middleware)

Add new middleware the client handler stack.

Parameters

callable $middleware

Return Value

$this

$this withRequestMiddleware(callable $middleware)

Add new request middleware the client handler stack.

Parameters

callable $middleware

Return Value

$this

$this withResponseMiddleware(callable $middleware)

Add new response middleware the client handler stack.

Parameters

callable $middleware

Return Value

$this

$this beforeSending(callable $callback)

Add a new "before sending" callback to the request.

Parameters

callable $callback

Return Value

$this

$this throw(callable|null $callback = null)

Throw an exception if a server or client error occurs.

Parameters

callable|null $callback

Return Value

$this

$this throwIf(callable|bool $condition)

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

Parameters

callable|bool $condition

Return Value

$this

$this throwUnless(callable|bool $condition)

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

Parameters

callable|bool $condition

Return Value

$this

$this dump()

Dump the request before sending.

Return Value

$this

$this dd()

Dump the request before sending and end the script.

Return Value

$this

Response get(string $url, array|string|null $query = null)

Issue a GET request to the given URL.

Parameters

string $url
array|string|null $query

Return Value

Response

Exceptions

ConnectionException

Response head(string $url, array|string|null $query = null)

Issue a HEAD request to the given URL.

Parameters

string $url
array|string|null $query

Return Value

Response

Exceptions

ConnectionException

Response post(string $url, array $data = [])

Issue a POST request to the given URL.

Parameters

string $url
array $data

Return Value

Response

Exceptions

ConnectionException

Response patch(string $url, array $data = [])

Issue a PATCH request to the given URL.

Parameters

string $url
array $data

Return Value

Response

Exceptions

ConnectionException

Response put(string $url, array $data = [])

Issue a PUT request to the given URL.

Parameters

string $url
array $data

Return Value

Response

Exceptions

ConnectionException

Response delete(string $url, array $data = [])

Issue a DELETE request to the given URL.

Parameters

string $url
array $data

Return Value

Response

Exceptions

ConnectionException

Response> pool(callable $callback)

Send a pool of asynchronous requests concurrently.

Parameters

callable $callback

Return Value

Response>

Response send(string $method, string $url, array $options = [])

Send the request to the given URL.

Parameters

string $method
string $url
array $options

Return Value

Response

Exceptions

Exception
ConnectionException

protected string expandUrlParameters(string $url)

Substitute the URL parameters in the given URL.

Parameters

string $url

Return Value

string

protected array parseHttpOptions(array $options)

Parse the given HTTP options and set the appropriate additional options.

Parameters

array $options

Return Value

array

protected array|array[] parseMultipartBodyFormat(array $data)

Parse multi-part form data.

Parameters

array $data

Return Value

array|array[]

protected PromiseInterface makePromise(string $method, string $url, array $options = [], int $attempt = 1)

Send an asynchronous request to the given URL.

Parameters

string $method
string $url
array $options
int $attempt

Return Value

PromiseInterface

protected mixed handlePromiseResponse(TransferException $response, string $method, string $url, array $options, int $attempt)

Handle the response of an asynchronous request.

Parameters

TransferException $response
string $method
string $url
array $options
int $attempt

Return Value

mixed

protected MessageInterface|PromiseInterface sendRequest(string $method, string $url, array $options = [])

Send a request either synchronously or asynchronously.

Parameters

string $method
string $url
array $options

Return Value

MessageInterface|PromiseInterface

Exceptions

Exception

protected array parseRequestData(string $method, string $url, array $options)

Get the request data as an array so that we can attach it to the request for convenient assertions.

Parameters

string $method
string $url
array $options

Return Value

array

protected array normalizeRequestOptions(array $options)

Normalize the given request options.

Parameters

array $options

Return Value

array

protected Response populateResponse(Response $response)

Populate the given response with additional data.

Parameters

Response $response

Return Value

Response

Client buildClient()

Build the Guzzle client.

Return Value

Client

protected bool requestsReusableClient()

Determine if a reusable client is required.

Return Value

bool

protected Client getReusableClient()

Retrieve a reusable Guzzle client.

Return Value

Client

Client createClient(HandlerStack $handlerStack)

Create new Guzzle client.

Parameters

HandlerStack $handlerStack

Return Value

Client

HandlerStack buildHandlerStack()

Build the Guzzle client handler stack.

Return Value

HandlerStack

HandlerStack pushHandlers(HandlerStack $handlerStack)

Add the necessary handlers to the given handler stack.

Parameters

HandlerStack $handlerStack

Return Value

HandlerStack

Closure buildBeforeSendingHandler()

Build the before sending handler.

Return Value

Closure

Closure buildRecorderHandler()

Build the recorder handler.

Return Value

Closure

Closure buildStubHandler()

Build the stub handler.

Return Value

Closure

protected Closure sinkStubHandler(string $sink)

Get the sink stub handler callback.

Parameters

string $sink

Return Value

Closure

RequestInterface runBeforeSendingCallbacks(RequestInterface $request, array $options)

Execute the "before sending" callbacks.

Parameters

RequestInterface $request
array $options

Return Value

RequestInterface

array mergeOptions(array ...$options)

Replace the given options with the current request options.

Parameters

array ...$options

Return Value

array

protected Response newResponse(MessageInterface $response)

Create a new response instance using the given PSR response.

Parameters

MessageInterface $response

Return Value

Response

$this stub(callable $callback)

Register a stub callable that will intercept requests and be able to return stub responses.

Parameters

callable $callback

Return Value

$this

$this preventStrayRequests(bool $prevent = true)

Indicate that an exception should be thrown if any request is not faked.

Parameters

bool $prevent

Return Value

$this

$this async(bool $async = true)

Toggle asynchronicity in requests.

Parameters

bool $async

Return Value

$this

PromiseInterface|null getPromise()

Retrieve the pending request promise.

Return Value

PromiseInterface|null

protected void dispatchRequestSendingEvent()

Dispatch the RequestSending event if a dispatcher is available.

Return Value

void

protected void dispatchResponseReceivedEvent(Response $response)

Dispatch the ResponseReceived event if a dispatcher is available.

Parameters

Response $response

Return Value

void

protected void dispatchConnectionFailedEvent(Request $request, ConnectionException $exception)

Dispatch the ConnectionFailed event if a dispatcher is available.

Parameters

Request $request
ConnectionException $exception

Return Value

void

$this setClient(Client $client)

Set the client instance.

Parameters

Client $client

Return Value

$this

$this setHandler(callable $handler)

Create a new client instance using the given handler.

Parameters

callable $handler

Return Value

$this

array getOptions()

Get the pending request options.

Return Value

array