Requests::request( string|WpOrgRequestsStringable $url, array $headers = array(), array|null $data = array(), string $type = self::GET, array $options = array() ): WpOrgRequestsResponse

In this article

Main interface for HTTP requests

Description

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

The $options parameter takes an associative array with the following options:

  • timeout: How long should we wait for a response? Note: for cURL, a minimum of 1 second applies, as DNS resolution operates at second-resolution only.
    (float, seconds with a millisecond precision, default: 10, example: 0.01)
  • connect_timeout: How long should we wait while trying to connect? (float, seconds with a millisecond precision, default: 10, example: 0.01)
  • useragent: Useragent to send to the server (string, default: php-requests/$version)
  • follow_redirects: Should we follow 3xx redirects? (boolean, default: true)
  • redirects: How many times should we redirect before erroring? (integer, default: 10)
  • blocking: Should we block processing on this request? (boolean, default: true)
  • filename: File to stream the body to instead.
    (string|boolean, default: false)
  • auth: Authentication handler or array of user/password details to use for Basic authentication (\WpOrg\Requests\Auth|array|boolean, default: false)
  • proxy: Proxy details to use for proxy by-passing and authentication (\WpOrg\Requests\Proxy|array|string|boolean, default: false)
  • max_bytes: Limit for the response body size.
    (integer|boolean, default: false)
  • idn: Enable IDN parsing (boolean, default: true)
  • transport: Custom transport. Either a class name, or a transport object. Defaults to the first working transport from \WpOrg\Requests\Requests::getTransport() (string|\WpOrg\Requests\Transport, default: \WpOrg\Requests\Requests::getTransport())
  • hooks: Hooks handler.
    (\WpOrg\Requests\HookManager, default: new WpOrg\Requests\Hooks())
  • verify: Should we verify SSL certificates? Allows passing in a custom certificate file as a string. (Using true uses the system-wide root certificate store instead, but this may have different behaviour across transports.) (string|boolean, default: certificates/cacert.pem)
  • verifyname: Should we verify the common name in the SSL certificate? (boolean, default: true)
  • data_format: How should we send the $data parameter? (string, one of ‘query’ or ‘body’, default: ‘query’ for HEAD/GET/DELETE, ‘body’ for POST/PUT/OPTIONS/PATCH)

Parameters

$urlstring|WpOrgRequestsStringablerequired
URL to request
$headersarrayoptional
Extra headers to send with the request

Default:array()

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

Default:array()

$typestringoptional
HTTP request type (use Requests constants)

Default:self::GET

$optionsarrayoptional
Options for the request (see description for more information)

Default:array()

Return

WpOrgRequestsResponse

Source

public static function request($url, $headers = [], $data = [], $type = self::GET, $options = []) {
	if (InputValidator::is_string_or_stringable($url) === false) {
		throw InvalidArgument::create(1, '$url', 'string|Stringable', gettype($url));
	}

	if (is_string($type) === false) {
		throw InvalidArgument::create(4, '$type', 'string', gettype($type));
	}

	if (is_array($options) === false) {
		throw InvalidArgument::create(5, '$options', 'array', gettype($options));
	}

	if (empty($options['type'])) {
		$options['type'] = $type;
	}

	$options = array_merge(self::get_default_options(), $options);

	self::set_defaults($url, $headers, $data, $type, $options);

	$options['hooks']->dispatch('requests.before_request', [&$url, &$headers, &$data, &$type, &$options]);

	if (!empty($options['transport'])) {
		$transport = $options['transport'];

		if (is_string($options['transport'])) {
			$transport = new $transport();
		}
	} else {
		$need_ssl     = (stripos($url, 'https://') === 0);
		$capabilities = [Capability::SSL => $need_ssl];
		$transport    = self::get_transport($capabilities);
	}

	$response = $transport->request($url, $headers, $data, $options);

	$options['hooks']->dispatch('requests.before_parse', [&$response, $url, $headers, $data, $type, $options]);

	return self::parse_response($response, $url, $headers, $data, $options);
}

User Contributed Notes

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