Requests 2.0.12 API

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.

Table of Contents

Properties

$data  : array<string|int, mixed>
Base data for requests
$headers  : array<string|int, mixed>
Base headers for requests
$options  : array<string|int, mixed>
Base options for requests
$url  : string|null
Base URL for requests

Methods

__construct()  : mixed
Create a new session
__get()  : mixed|null
Get a property's value
__isset()  : mixed
Remove a property's value
__set()  : mixed
Set a property's value
__unset()  : mixed
Remove a property's value
delete()  : mixed
Send a DELETE request
get()  : mixed
Send a GET request
head()  : mixed
Send a HEAD request
patch()  : mixed
Send a PATCH request
post()  : mixed
Send a POST request
put()  : mixed
Send a PUT request
request()  : Response
Main interface for HTTP requests
request_multiple()  : array<string|int, mixed>
Send multiple HTTP requests simultaneously
merge_request()  : array<string|int, mixed>
Merge a request's data with the default data

Properties

$data

Base data for requests

public array<string|int, mixed> $data = []

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

$headers

Base headers for requests

public array<string|int, mixed> $headers = []

$options

Base options for requests

public array<string|int, mixed> $options = []

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

$url

Base URL for requests

public string|null $url = null

URLs will be made absolute using this as the base

Methods

__construct()

Create a new session

public __construct([string|Stringable|null $url = null ][, array<string|int, mixed> $headers = [] ][, array<string|int, mixed> $data = [] ][, array<string|int, mixed> $options = [] ]) : mixed
Parameters
$url : string|Stringable|null = null

Base URL for requests

$headers : array<string|int, mixed> = []

Default headers for requests

$data : array<string|int, mixed> = []

Default data for requests

$options : array<string|int, mixed> = []

Default options for requests

Tags
throws
InvalidArgument

When the passed $url argument is not a string, Stringable or null.

throws
InvalidArgument

When the passed $headers argument is not an array.

throws
InvalidArgument

When the passed $data argument is not an array.

throws
InvalidArgument

When the passed $options argument is not an array.

__get()

Get a property's value

public __get(string $name) : mixed|null
Parameters
$name : string

Property name.

Return values
mixed|null

Property value, null if none found

__isset()

Remove a property's value

public __isset(string $name) : mixed
Parameters
$name : string

Property name.

__set()

Set a property's value

public __set(string $name, mixed $value) : mixed
Parameters
$name : string

Property name.

$value : mixed

Property value

__unset()

Remove a property's value

public __unset(string $name) : mixed
Parameters
$name : string

Property name.

delete()

Send a DELETE request

public delete(mixed $url[, mixed $headers = [] ][, mixed $options = [] ]) : mixed
Parameters
$url : mixed
$headers : mixed = []
$options : mixed = []

get()

Send a GET request

public get(mixed $url[, mixed $headers = [] ][, mixed $options = [] ]) : mixed
Parameters
$url : mixed
$headers : mixed = []
$options : mixed = []

head()

Send a HEAD request

public head(mixed $url[, mixed $headers = [] ][, mixed $options = [] ]) : mixed
Parameters
$url : mixed
$headers : mixed = []
$options : mixed = []

patch()

Send a PATCH request

public patch(mixed $url, mixed $headers[, mixed $data = [] ][, mixed $options = [] ]) : mixed

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

Parameters
$url : mixed
$headers : mixed
$data : mixed = []
$options : mixed = []
Tags
link
https://tools.ietf.org/html/rfc5789

post()

Send a POST request

public post(mixed $url[, mixed $headers = [] ][, mixed $data = [] ][, mixed $options = [] ]) : mixed
Parameters
$url : mixed
$headers : mixed = []
$data : mixed = []
$options : mixed = []

put()

Send a PUT request

public put(mixed $url[, mixed $headers = [] ][, mixed $data = [] ][, mixed $options = [] ]) : mixed
Parameters
$url : mixed
$headers : mixed = []
$data : mixed = []
$options : mixed = []

request()

Main interface for HTTP requests

public request(string $url[, array<string|int, mixed> $headers = [] ][, array<string|int, mixed>|null $data = [] ][, string $type = Requests::GET ][, array<string|int, mixed> $options = [] ]) : Response

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

Parameters
$url : string

URL to request

$headers : array<string|int, mixed> = []

Extra headers to send with the request

$data : array<string|int, mixed>|null = []

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

$type : string = Requests::GET

HTTP request type (use \WpOrg\Requests\Requests constants)

$options : array<string|int, mixed> = []

Options for the request (see Requests::request())

Tags
see
Requests::request()
throws
Exception

On invalid URLs (nonhttp)

Return values
Response

request_multiple()

Send multiple HTTP requests simultaneously

public request_multiple(array<string|int, mixed> $requests[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>
Parameters
$requests : array<string|int, mixed>

Requests data (see Requests::request_multiple())

$options : array<string|int, mixed> = []

Global and default options (see Requests::request())

Tags
see
Requests::request_multiple()
throws
InvalidArgument

When the passed $requests argument is not an array or iterable object with array access.

throws
InvalidArgument

When the passed $options argument is not an array.

Return values
array<string|int, mixed>

Responses (either \WpOrg\Requests\Response or a \WpOrg\Requests\Exception object)

merge_request()

Merge a request's data with the default data

protected merge_request(array<string|int, mixed> $request[, bool $merge_options = true ]) : array<string|int, mixed>
Parameters
$request : array<string|int, mixed>

Request data (same form as Session::request_multiple())

$merge_options : bool = true

Should we merge options as well?

Return values
array<string|int, mixed>

Request data


        
On this page

Search results