Response::decode_body( bool|null $associative = true, int $depth = 512, int $options ): array

In this article

JSON decode the response body.

Description

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

Parameters

$associativebool|nulloptional
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).

Default:true

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

Default:512

$optionsintoptional
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).

Return

array

Source

public function decode_body($associative = true, $depth = 512, $options = 0) {
	$data = json_decode($this->body, $associative, $depth, $options);

	if (json_last_error() !== JSON_ERROR_NONE) {
		$last_error = json_last_error_msg();
		throw new Exception('Unable to parse JSON data: ' . $last_error, 'response.invalid', $this);
	}

	return $data;
}

User Contributed Notes

You must log in before being able to contribute a note or feedback.