class Client implements SplSubject, Countable (View source)

The HTTP client. See http\Client\Curl's options which is the only driver currently supported.

Constants

DEBUG_INFO

Debug callback's $data contains human readable text.

DEBUG_IN

Debug callback's $data contains data received.

DEBUG_OUT

Debug callback's $data contains data sent.

DEBUG_HEADER

Debug callback's $data contains headers.

DEBUG_BODY

Debug callback's $data contains a body part.

DEBUG_SSL

Debug callback's $data contains SSL data.

Properties

protected array $options

Set options.

protected Message $history

Request/response history.

bool $recordHistory

Whether to record history in http\Client::$history.

Methods

__construct(string $driver = null, string $persistent_handle_id = null)

Create a new HTTP client.

addCookies(array $cookies = null)

Add custom cookies.

addSslOptions(array $ssl_options = null)

Add specific SSL options.

void
attach(SplObserver $observer)

Implements SplSubject. Attach another observer.

configure(array $configuration)

Configure the client's low level options.

int
count()

Implements Countable. Retrieve the number of enqueued requests.

dequeue(Request $request)

Dequeue the http\Client\Request $request.

void
detach(SplObserver $observer)

Implements SplSubject. Detach $observer, which has been previously attached.

enableEvents(bool $enable = true)

Enable usage of an event library like libevent, which might improve performance with big socket sets.

enablePipelining(bool $enable = true)

Enable sending pipelined requests to the same host if the driver supports it.

enqueue(Request $request, callable $cb = null)

Add another http\Client\Request to the request queue.

array
getAvailableConfiguration()

Get a list of available configuration options and their default values.

array
getAvailableDrivers()

List available drivers.

array
getAvailableOptions()

Retrieve a list of available request options and their default values.

array
getCookies()

Get priorly set custom cookies.

getHistory()

Simply returns the http\Message chain representing the request/response history.

getObservers()

Returns the SplObjectStorage holding attached observers.

array
getOptions()

Get priorly set options.

object|null
getProgressInfo(Request $request)

Retrieve the progress information for $request.

Response|null
getResponse(Request $request = null)

Retrieve the corresponding response of an already finished request, or the last received response if $request is not set.

array
getSslOptions()

Retrieve priorly set SSL options.

object
getTransferInfo(Request $request)

Get transfer related information for a running or finished request.

void
notify(Request $request = null, object $progress = null)

Implements SplSubject. Notify attached observers about progress with $request.

bool
once()

Perform outstanding transfer actions.

requeue(Request $request, callable $cb = null)

Requeue an http\Client\Request.

reset()

Reset the client to the initial state.

send()

Send all enqueued requests.

setCookies(array $cookies = null)

Set custom cookies.

setDebug(callable $callback)

Set client debugging callback.

setOptions(array $options = null)

Set client options.

setSslOptions(array $ssl_options = null)

Specifically set SSL options.

bool
wait(float $timeout = 0)

Wait for $timeout seconds for transfers to provide data.

Details

__construct(string $driver = null, string $persistent_handle_id = null)

Create a new HTTP client.

Currently only "curl" is supported as a $driver, and used by default. Persisted resources identified by $persistent_handle_id will be re-used if available.

Parameters

string $driver

The HTTP client driver to employ. Currently only the default driver, "curl", is supported.

string $persistent_handle_id

If supplied, created curl handles will be persisted with this identifier for later reuse.

Exceptions

InvalidArgumentException
UnexpectedValueException
RuntimeException

Client addCookies(array $cookies = null)

Add custom cookies.

See http\Client::setCookies().

Parameters

array $cookies

Custom cookies to add.

Return Value

Client self.

Exceptions

InvalidArgumentException

Client addSslOptions(array $ssl_options = null)

Add specific SSL options.

See http\Client::setSslOptions(), http\Client::setOptions() and http\Client\Curl\$ssl options.

Parameters

array $ssl_options

Add this SSL options.

Return Value

Client self.

Exceptions

InvalidArgumentException

void attach(SplObserver $observer)

Implements SplSubject. Attach another observer.

Attached observers will be notified with progress of each transfer.

Parameters

SplObserver $observer

The SplObserver to attach.

Return Value

void

Exceptions

InvalidArgumentException
UnexpectedValueException

Client configure(array $configuration)

Configure the client's low level options.

NOTE: This method has been added in v2.3.0.

Parameters

array $configuration

Key/value pairs of low level options. See f.e. the configuration options for the Curl driver.

Return Value

Client self.

Exceptions

InvalidArgumentException
UnexpectedValueException

int count()

Implements Countable. Retrieve the number of enqueued requests.

NOTE: The enqueued requests are counted without regard whether they are finished or not.

Return Value

int

The custom count as an integer.

The return value is cast to an integer.

Client dequeue(Request $request)

Dequeue the http\Client\Request $request.

See http\Client::requeue(), if you want to requeue the request, instead of calling http\Client::dequeue() and then http\Client::enqueue().

Parameters

Request $request

The request to cancel.

Return Value

Client self.

Exceptions

InvalidArgumentException
BadMethodCallException
RuntimeException

void detach(SplObserver $observer)

Implements SplSubject. Detach $observer, which has been previously attached.

Parameters

SplObserver $observer

The SplObserver to detach.

Return Value

void

Exceptions

InvalidArgumentException
UnexpectedValueException

Client enableEvents(bool $enable = true)

Enable usage of an event library like libevent, which might improve performance with big socket sets.

Parameters

bool $enable

Whether to enable libevent usage.

Return Value

Client self.

Exceptions

InvalidArgumentException
UnexpectedValueException

See also

Client::configure

Client enablePipelining(bool $enable = true)

Enable sending pipelined requests to the same host if the driver supports it.

Parameters

bool $enable

Whether to enable pipelining.

Return Value

Client self.

Exceptions

InvalidArgumentException
UnexpectedValueException

See also

Client::configure

Client enqueue(Request $request, callable $cb = null)

Add another http\Client\Request to the request queue.

If the optional callback $cb returns true, the request will be automatically dequeued.

Note: The http\Client\Response object resulting from the request is always stored internally to be retrieved at a later time, even when $cb is used.

If you are about to send a lot of requests and do not need the response after executing the callback, you can use http\Client::getResponse() within the callback to keep the memory usage level as low as possible.

See http\Client::dequeue() and http\Client::send().

Parameters

Request $request

The request to enqueue.

callable $cb

as function(\http\Response $response) : ?bool A callback to automatically call when the request has finished.

Return Value

Client self.

Exceptions

InvalidArgumentException
BadMethodCallException
RuntimeException

array getAvailableConfiguration()

Get a list of available configuration options and their default values.

See f.e. the configuration options for the Curl driver.

Return Value

array

list of key/value pairs of available configuration options and their default values.

Exceptions

InvalidArgumentException

array getAvailableDrivers()

List available drivers.

Return Value

array

list of supported drivers.

array getAvailableOptions()

Retrieve a list of available request options and their default values.

See f.e. the request options for the Curl driver.

Return Value

array

list of key/value pairs of available request options and their default values.

Exceptions

InvalidArgumentException

array getCookies()

Get priorly set custom cookies.

See http\Client::setCookies().

Return Value

array

custom cookies.

Message getHistory()

Simply returns the http\Message chain representing the request/response history.

NOTE: The history is only recorded while http\Client::$recordHistory is true.

Return Value

Message

the request/response message chain representing the client's history.

Exceptions

InvalidArgumentException

SplObjectStorage getObservers()

Returns the SplObjectStorage holding attached observers.

Return Value

SplObjectStorage

observer storage.

Exceptions

InvalidArgumentException
UnexpectedValueException

array getOptions()

Get priorly set options.

See http\Client::setOptions().

Return Value

array options.

object|null getProgressInfo(Request $request)

Retrieve the progress information for $request.

Parameters

Request $request

The request to retrieve the current progress information for.

Return Value

object|null

object stdClass instance holding progress information. or NULL if $request is not enqueued.

Exceptions

InvalidArgumentException
UnexpectedValueException

Response|null getResponse(Request $request = null)

Retrieve the corresponding response of an already finished request, or the last received response if $request is not set.

NOTE: If $request is NULL, then the response is removed from the internal storage (stack-like operation).

Parameters

Request $request

The request to fetch the stored response for.

Return Value

Response|null

\http\Client\Response the stored response for the request, or the last that was received. or NULL if no more response was available to pop, when no $request was given.

Exceptions

InvalidArgumentException
UnexpectedValueException

array getSslOptions()

Retrieve priorly set SSL options.

See http\Client::getOptions() and http\Client::setSslOptions().

Return Value

array

SSL options.

object getTransferInfo(Request $request)

Get transfer related information for a running or finished request.

Parameters

Request $request

The request to probe for transfer info.

Return Value

object

stdClass instance holding transfer related information.

Exceptions

InvalidArgumentException
UnexpectedValueException

void notify(Request $request = null, object $progress = null)

Implements SplSubject. Notify attached observers about progress with $request.

Parameters

Request $request

The request to notify about.

object $progress

stdClass instance holding progress information.

Return Value

void

Exceptions

InvalidArgumentException
UnexpectedValueException

bool once()

Perform outstanding transfer actions.

See http\Client::wait() for the completing interface.

Return Value

bool

true if there are more transfers to complete.

Client requeue(Request $request, callable $cb = null)

Requeue an http\Client\Request.

The difference simply is, that this method, in contrast to http\Client::enqueue(), does not throw an http\Exception when the request to queue is already enqueued and dequeues it automatically prior enqueueing it again.

Parameters

Request $request

The request to queue.

callable $cb

as function(\http\Response $response) : ?bool A callback to automatically call when the request has finished.

Return Value

Client self.

Exceptions

InvalidArgumentException
RuntimeException

Client reset()

Reset the client to the initial state.

Return Value

Client self.

Client send()

Send all enqueued requests.

See http\Client::once() and http\Client::wait() for a more fine grained interface.

Return Value

Client self.

Exceptions

InvalidArgumentException
RuntimeException

Client setCookies(array $cookies = null)

Set custom cookies.

See http\Client::addCookies() and http\Client::getCookies().

Parameters

array $cookies

Set the custom cookies to this array.

Return Value

Client self.

Exceptions

InvalidArgumentException

Client setDebug(callable $callback)

Set client debugging callback.

NOTE: This method has been added in v2.6.0, resp. v3.1.0.

Parameters

callable $callback

as function(http\Client $c, http\Client\Request $r, int $type, string $data) The debug callback. For $type see http\Client::DEBUG_* constants.

Return Value

Client self.

Exceptions

InvalidArgumentException

Client setOptions(array $options = null)

Set client options.

See http\Client\Curl.

NOTE: Only options specified prior enqueueing a request are applied to the request.

Parameters

array $options

The options to set.

Return Value

Client self.

Exceptions

InvalidArgumentException

Client setSslOptions(array $ssl_options = null)

Specifically set SSL options.

See http\Client::setOptions() and http\Client\Curl\$ssl options.

Parameters

array $ssl_options

Set SSL options to this array.

Return Value

Client self.

Exceptions

InvalidArgumentException

bool wait(float $timeout = 0)

Wait for $timeout seconds for transfers to provide data.

This is the completion call to http\Client::once().

Parameters

float $timeout

Seconds to wait for data on open sockets.

Return Value

bool success.