Session handler for persistent requests and default parameters

Allows various options to be set as default values, and merges both the options and URL properties together. A base URL can be set for all requests, with all subrequests resolved from this. Base options can be set (including a shared cookie jar), then overridden for individual requests.

package Requests
subpackage Session Handler

 Methods

Create a new session

__construct(string|null $url = null, array $headers = array(), array $data = array(), array $options = array()

Parameters

$url

stringnull

Base URL for requests

$headers

array

Default headers for requests

$data

array

Default data for requests

$options

array

Default options for requests

Get a property's value

__get(string $key) : mixed | null

Parameters

$key

string

Property key

Returns

mixednullProperty value, null if none found

Remove a property's value

__isset(string $key) 

Parameters

$key

string

Property key

Set a property's value

__set(string $key, mixed $value) 

Parameters

$key

string

Property key

$value

mixed

Property value

Remove a property's value

__unset(string $key) 

Parameters

$key

string

Property key

Send a DELETE request

delete($url, $headers = array(), $options = array()

Parameters

$url

$headers

$options

Send a GET request

get($url, $headers = array(), $options = array()

Parameters

$url

$headers

$options

Send a HEAD request

head($url, $headers = array(), $options = array()

Parameters

$url

$headers

$options

Send a PATCH request

patch($url, $headers, $data = array(), $options = array()

Note: Unlike \post and \put, $headers is required, as the specification recommends that should send an ETag

link https://tools.ietf.org/html/rfc5789

Parameters

$url

$headers

$data

$options

Send a POST request

post($url, $headers = array(), $data = array(), $options = array()

Parameters

$url

$headers

$data

$options

Send a PUT request

put($url, $headers = array(), $data = array(), $options = array()

Parameters

$url

$headers

$data

$options

Main interface for HTTP requests

request(string $url, array $headers = array(), array|null $data = array(), string $type = \Requests::GET, array $options = array()) : \Requests_Response

This method initiates a request and sends it via a transport before parsing.

see

Parameters

$url

string

URL to request

$headers

array

Extra headers to send with the request

$data

arraynull

Data to send either as a query string for GET/HEAD requests, or in the body for POST requests

$type

string

HTTP request type (use Requests constants)

$options

array

Options for the request (see {@see Requests::request})

Exceptions

\Requests_Exception On invalid URLs (`nonhttp`)

Returns

\Requests_Response

Send multiple HTTP requests simultaneously

request_multiple(array $requests, array $options = array()) : array
see

Parameters

$requests

array

Requests data (see {@see Requests::request_multiple})

$options

array

Global and default options (see {@see Requests::request})

Returns

arrayResponses (either Requests_Response or a Requests_Exception object)

Merge a request's data with the default data

merge_request(array $request, boolean $merge_options = true) : array

Parameters

$request

array

Request data (same form as {@see request_multiple})

$merge_options

boolean

Should we merge options as well?

Returns

arrayRequest data

 Properties

 

Base data for requests

$data : array

Default

array()

If both the base data and the per-request data are arrays, the data will be merged before sending the request.

 

Base headers for requests

$headers : array

Default

array()
 

Base options for requests

$options : array

Default

array()

The base options are merged with the per-request data for each request. The only default option is a shared cookie jar between requests.

Values here can also be set directly via properties on the Session object, e.g. $session->useragent = 'X';

 

Base URL for requests

$url : string | null

Default

null

URLs will be made absolute using this as the base