Documentation

Response
in package

HTTP response class

Contains a response from \WpOrg\Requests\Requests::request()

Table of Contents

Properties

$body  : string
Response body
$cookies  : Jar
Cookies from the request
$headers  : Headers
Headers, as an associative array
$history  : array<string|int, mixed>
Previous requests (from redirects)
$protocol_version  : float|bool
Protocol version, false if non-blocking
$raw  : string
Raw HTTP data from the transport
$redirects  : int
Number of redirects the request used
$status_code  : int|bool
Status code, false if non-blocking
$success  : bool
Whether the request succeeded or not
$url  : string
URL requested

Methods

__construct()  : mixed
Constructor
decode_body()  : array<string|int, mixed>
JSON decode the response body.
is_redirect()  : bool
Is the response a redirect?
throw_for_status()  : mixed
Throws an exception if the request was not successful

Properties

$body

Response body

public string $body = ''

$cookies

Cookies from the request

public Jar $cookies = []

Array-like object representing a cookie jar

$headers

Headers, as an associative array

public Headers $headers = []

Array-like object representing headers

$history

Previous requests (from redirects)

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

Array of \WpOrg\Requests\Response objects

$protocol_version

Protocol version, false if non-blocking

public float|bool $protocol_version = false

$raw

Raw HTTP data from the transport

public string $raw = ''

$redirects

Number of redirects the request used

public int $redirects = 0

$status_code

Status code, false if non-blocking

public int|bool $status_code = false

$success

Whether the request succeeded or not

public bool $success = false

$url

URL requested

public string $url = ''

Methods

__construct()

Constructor

public __construct() : mixed

decode_body()

JSON decode the response body.

public decode_body([bool|null $associative = true ][, int $depth = 512 ][, int $options = 0 ]) : array<string|int, mixed>

The method parameters are the same as those for the PHP native json_decode() function.

Parameters
$associative : bool|null = true

Optional. When true, JSON objects will be returned as associative arrays; When false, JSON objects will be returned as objects. When null, JSON objects will be returned as associative arrays or objects depending on whether JSON_OBJECT_AS_ARRAY is set in the flags. Defaults to true (in contrast to the PHP native default of null).

$depth : int = 512

Optional. Maximum nesting depth of the structure being decoded. Defaults to 512.

$options : int = 0

Optional. Bitmask of JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR. Defaults to 0 (no options set).

Tags
link
https://php.net/json-decode
throws
Exception

If $this->body is not valid json.

Return values
array<string|int, mixed>

is_redirect()

Is the response a redirect?

public is_redirect() : bool
Return values
bool

True if redirect (3xx status), false if not.

throw_for_status()

Throws an exception if the request was not successful

public throw_for_status([bool $allow_redirects = true ]) : mixed
Parameters
$allow_redirects : bool = true

Set to false to throw on a 3xx as well

Tags
throws
Exception

If $allow_redirects is false, and code is 3xx (response.no_redirects)

throws
Http

On non-successful status code. Exception class corresponds to "Status" + code (e.g. Status404)


        
On this page

Search results