Requests::set_defaults( string $url, array $headers, array|null $data, string $type, array $options ): void

In this article

Set the default values

Description

The $options parameter is updated with the results.

Parameters

$urlstringrequired
URL to request
$headersarrayrequired
Extra headers to send with the request
$dataarray|nullrequired
Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
$typestringrequired
HTTP request type
$optionsarrayrequired
Options for the request

Return

void

Source

protected static function set_defaults(&$url, &$headers, &$data, &$type, &$options) {
	if (!preg_match('/^http(s)?:\/\//i', $url, $matches)) {
		throw new Exception('Only HTTP(S) requests are handled.', 'nonhttp', $url);
	}

	if (empty($options['hooks'])) {
		$options['hooks'] = new Hooks();
	}

	if (is_array($options['auth'])) {
		$options['auth'] = new Basic($options['auth']);
	}

	if ($options['auth'] !== false) {
		$options['auth']->register($options['hooks']);
	}

	if (is_string($options['proxy']) || is_array($options['proxy'])) {
		$options['proxy'] = new Http($options['proxy']);
	}

	if ($options['proxy'] !== false) {
		$options['proxy']->register($options['hooks']);
	}

	if (is_array($options['cookies'])) {
		$options['cookies'] = new Jar($options['cookies']);
	} elseif (empty($options['cookies'])) {
		$options['cookies'] = new Jar();
	}

	if ($options['cookies'] !== false) {
		$options['cookies']->register($options['hooks']);
	}

	if ($options['idn'] !== false) {
		$iri       = new Iri($url);
		$iri->host = IdnaEncoder::encode($iri->ihost);
		$url       = $iri->uri;
	}

	// Massage the type to ensure we support it.
	$type = strtoupper($type);

	if (!isset($options['data_format'])) {
		if (in_array($type, [self::HEAD, self::GET, self::DELETE], true)) {
			$options['data_format'] = 'query';
		} else {
			$options['data_format'] = 'body';
		}
	}
}

User Contributed Notes

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