Class: Requests_Session

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.

Properties


Properties

Name Type(s) Default Value Summary
url string | null Base URL for requests

URLs will be made absolute using this as the base

headers array Base headers for requests
data array Base data for requests

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

options array Base options for requests

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';


Methods

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

Create a new session

Arguments

Name Type(s) Default Value Description
$url string | null

Base URL for requests

$headers array

Default headers for requests

$data array

Default data for requests

$options array

Default options for requests


Requests_Session:: __get( string $key )

Get a property's value

Arguments

Name Type(s) Default Value Description
$key string

Property key

Returns

mixed | null

Property value, null if none found


Requests_Session:: __isset( string $key )

Remove a property's value

Arguments

Name Type(s) Default Value Description
$key string

Property key


Requests_Session:: __set( string $key, mixed $value )

Set a property's value

Arguments

Name Type(s) Default Value Description
$key string

Property key

$value mixed

Property value


Requests_Session:: __unset( string $key )

Remove a property's value

Arguments

Name Type(s) Default Value Description
$key string

Property key


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

Send a DELETE request

Arguments

Name Type(s) Default Value Description
$url
$headers
$options

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

Send a GET request

Arguments

Name Type(s) Default Value Description
$url
$headers
$options

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

Send a HEAD request

Arguments

Name Type(s) Default Value Description
$url
$headers
$options

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

Merge a request's data with the default data

Arguments

Name Type(s) Default Value Description
$request array

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

$merge_options boolean

Should we merge options as well?

Returns

array

Request data


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

Send a PATCH request

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

Arguments

Name Type(s) Default Value Description
$url
$headers
$data
$options

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

Send a POST request

Arguments

Name Type(s) Default Value Description
$url
$headers
$data
$options

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

Send a PUT request

Arguments

Name Type(s) Default Value Description
$url
$headers
$data
$options

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

Main interface for HTTP requests

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

Arguments

Name Type(s) Default Value Description
$url string

URL to request

$headers array

Extra headers to send with the request

$data array | null

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})

Returns

Requests_Response


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

Send multiple HTTP requests simultaneously

Arguments

Name Type(s) Default Value Description
$requests array

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

$options array

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

Returns

array

Responses (either Requests_Response or a Requests_Exception object)


WordPress Developer Newsletter

Stay on top of the latest WordPress API changes, developer tool updates, security alerts and more.