WpOrgRequestsSession
Session handler for persistent requests with shared default parameters.
Source: wp-includes/Requests/src/Session.php
Namespace: WpOrgRequests
Overview
Session allows setting a base URL, default headers, data, and options that are merged into every request. A shared cookie jar is automatically created if none is provided, enabling cookie persistence across requests.
Options can be set as properties on the Session object via magic methods (e.g. $session->useragent = 'X').
Properties
| Property | Type | Default | Description |
|---|---|---|---|
$url | string|null | null | Base URL — per-request URLs are resolved relative to this |
$headers | array | [] | Default headers merged with per-request headers |
$data | array | [] | Default data merged with per-request data (both must be arrays) |
$options | array | [] | Default options merged with per-request options |
Methods
__construct()
public function __construct(
string|Stringable|null $url = null,
array $headers = [],
array $data = [],
array $options = []
)| Parameter | Type | Description |
|---|---|---|
$url | string|Stringable|null | Base URL |
$headers | array | Default headers |
$data | array | Default data |
$options | array | Default options |
Creates a shared CookieJar in $options['cookies'] if not provided.
Throws: InvalidArgument for invalid parameter types.
Magic Methods
public function __get( string $name ): mixed|null
public function __set( string $name, mixed $value ): void
public function __isset( string $name ): bool
public function __unset( string $name ): voidProxy get/set/isset/unset to $this->options[$name]. Allows $session->useragent = 'MyApp' as shorthand for $session->options['useragent'] = 'MyApp'.
__wakeup()
public function __wakeup(): voidThrows LogicException — Session should never be unserialized.
HTTP Method Shortcuts
Without body (GET, HEAD, DELETE):
public function get( string $url, array $headers = [], array $options = [] ): Response
public function head( string $url, array $headers = [], array $options = [] ): Response
public function delete( string $url, array $headers = [], array $options = [] ): ResponseWith body (POST, PUT, PATCH):
public function post( string $url, array $headers = [], array $data = [], array $options = [] ): Response
public function put( string $url, array $headers = [], array $data = [], array $options = [] ): Response
public function patch( string $url, array $headers, array $data = [], array $options = [] ): ResponseNote: patch() requires $headers (not optional), same as Requests::patch().
All delegate to $this->request().
request()
public function request(
string $url,
array $headers = [],
array|null $data = [],
string $type = Requests::GET,
array $options = []
): ResponseMerges session defaults with per-request values via merge_request(), then delegates to Requests::request().
request_multiple()
public function request_multiple( array $requests, array $options = [] ): arraySend multiple requests. Each request is merged with session defaults. The type option is removed from merged global options (type is per-request only).
Throws: InvalidArgument for invalid parameter types.
Protected Methods
merge_request()
protected function merge_request( array $request, bool $merge_options = true ): arrayMerges a request’s data with session defaults:
- If
$this->urlis set, resolves$request['url']relative to it usingIri::absolutize() - Merges headers (
array_merge) - Merges data if both are arrays
- Merges options (if
$merge_optionsis true), removingtypefrom merged result