WP_Http::request( string $url, string|array $args = array() )
Send an HTTP request to a URI.
Description
Please note: The only URI that are supported in the HTTP Transport implementation are the HTTP and HTTPS protocols.
Parameters
- $url
-
(Required)
The request URL.
- $args
-
(Optional)
Array or string of HTTP request arguments.<br>
- 'method'
(string) Request method. Accepts 'GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'TRACE', 'OPTIONS', or 'PATCH'.<br> Some transports technically allow others, but should not be assumed. Default 'GET'.<br>
- 'timeout'
(float) How long the connection should stay open in seconds. Default 5.<br>
- 'redirection'
(int) Number of allowed redirects. Not supported by all transports.<br> Default 5.<br>
- 'httpversion'
(string) Version of the HTTP protocol to use. Accepts '1.0' and '1.1'.<br> Default '1.0'.<br>
- 'user-agent'
(string) User-agent value sent.<br> Default 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ).<br>
- 'reject_unsafe_urls'
(bool) Whether to pass URLs through wp_http_validate_url().<br> Default false.<br>
- 'blocking'
(bool) Whether the calling code requires the result of the request.<br> If set to false, the request will be sent to the remote server, and processing returned to the calling code immediately, the caller will know if the request succeeded or failed, but will not receive any response from the remote server. Default true.<br>
- 'headers'
(string|array) Array or string of headers to send with the request.<br> <br>
- 'cookies'
(array) List of cookies to send with the request. <br>
- 'body'
(string|array) Body to send with the request. Default null.<br>
- 'compress'
(bool) Whether to compress the $body when sending the request.<br> Default false.<br>
- 'decompress'
(bool) Whether to decompress a compressed response. If set to false and compressed content is returned in the response anyway, it will need to be separately decompressed. Default true.<br>
- 'sslverify'
(bool) Whether to verify SSL for the request. Default true.<br>
- 'sslcertificates'
(string) Absolute path to an SSL certificate .crt file.<br> Default ABSPATH . WPINC . '/certificates/ca-bundle.crt'.<br>
- 'stream'
(bool) Whether to stream to a file. If set to true and no filename was given, it will be dropped it in the WP temp dir and its name will be set using the basename of the URL. Default false.<br>
- 'filename'
(string) Filename of the file to write to when streaming. $stream must be set to true. Default null.<br>
- 'limit_response_size'
(int) Size in bytes to limit the response to. Default null.<br>
Default value: array()
Return
(array|WP_Error) Array containing 'headers', 'body', 'response', 'cookies', 'filename'.<br> A WP_Error instance upon error.
Source
File: wp-includes/class-wp-http.php
public function request( $url, $args = array() ) {
$defaults = array(
'method' => 'GET',
'timeout' => apply_filters( 'http_request_timeout', 5 ),
'redirection' => apply_filters( 'http_request_redirection_count', 5 ),
'httpversion' => apply_filters( 'http_request_version', '1.0' ),
'user-agent' => classicpress_user_agent(),
'reject_unsafe_urls' => apply_filters( 'http_request_reject_unsafe_urls', false ),
'blocking' => true,
'headers' => array(),
'cookies' => array(),
'body' => null,
'compress' => false,
'decompress' => true,
'sslverify' => true,
'sslcertificates' => ABSPATH . WPINC . '/certificates/ca-bundle.crt',
'stream' => false,
'filename' => null,
'limit_response_size' => null,
);
$args = wp_parse_args( $args );
if ( isset($args['method']) && 'HEAD' == $args['method'] )
$defaults['redirection'] = 0;
$r = wp_parse_args( $args, $defaults );
$r = apply_filters( 'http_request_args', $r, $url );
if ( ! isset( $r['_redirection'] ) )
$r['_redirection'] = $r['redirection'];
$pre = apply_filters( 'pre_http_request', false, $r, $url );
if ( false !== $pre )
return $pre;
if ( function_exists( 'wp_kses_bad_protocol' ) ) {
if ( $r['reject_unsafe_urls'] ) {
$url = wp_http_validate_url( $url );
}
if ( $url ) {
$url = wp_kses_bad_protocol( $url, array( 'http', 'https', 'ssl' ) );
}
}
$arrURL = @parse_url( $url );
if ( empty( $url ) || empty( $arrURL['scheme'] ) ) {
return new WP_Error('http_request_failed', __('A valid URL was not provided.'));
}
if ( $this->block_request( $url ) ) {
return new WP_Error( 'http_request_failed', __( 'User has blocked requests through HTTP.' ) );
}
if ( $r['stream'] ) {
if ( empty( $r['filename'] ) ) {
$r['filename'] = get_temp_dir() . basename( $url );
}
$r['blocking'] = true;
if ( ! wp_is_writable( dirname( $r['filename'] ) ) ) {
return new WP_Error( 'http_request_failed', __( 'Destination directory for file streaming does not exist or is not writable.' ) );
}
}
if ( is_null( $r['headers'] ) ) {
$r['headers'] = array();
}
if ( ! is_array( $r['headers'] ) ) {
$processedHeaders = WP_Http::processHeaders( $r['headers'] );
$r['headers'] = $processedHeaders['headers'];
}
$headers = $r['headers'];
$data = $r['body'];
$type = $r['method'];
$options = array(
'timeout' => $r['timeout'],
'useragent' => $r['user-agent'],
'blocking' => $r['blocking'],
'hooks' => new WP_HTTP_Requests_Hooks( $url, $r ),
);
$options['hooks']->register( 'requests.before_redirect', array( get_class(), 'browser_redirect_compatibility' ) );
if ( function_exists( 'wp_kses_bad_protocol' ) && $r['reject_unsafe_urls'] ) {
$options['hooks']->register( 'requests.before_redirect', array( get_class(), 'validate_redirects' ) );
}
if ( $r['stream'] ) {
$options['filename'] = $r['filename'];
}
if ( empty( $r['redirection'] ) ) {
$options['follow_redirects'] = false;
} else {
$options['redirects'] = $r['redirection'];
}
if ( isset( $r['limit_response_size'] ) ) {
$options['max_bytes'] = $r['limit_response_size'];
}
if ( ! empty( $r['cookies'] ) ) {
$options['cookies'] = WP_Http::normalize_cookies( $r['cookies'] );
}
if ( ! $r['sslverify'] ) {
$options['verify'] = false;
$options['verifyname'] = false;
} else {
$options['verify'] = $r['sslcertificates'];
}
if ( 'HEAD' !== $type && 'GET' !== $type ) {
$options['data_format'] = 'body';
}
$options['verify'] = apply_filters( 'https_ssl_verify', $options['verify'] );
$proxy = new WP_HTTP_Proxy();
if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
$options['proxy'] = new Requests_Proxy_HTTP( $proxy->host() . ':' . $proxy->port() );
if ( $proxy->use_authentication() ) {
$options['proxy']->use_authentication = true;
$options['proxy']->user = $proxy->username();
$options['proxy']->pass = $proxy->password();
}
}
mbstring_binary_safe_encoding();
try {
$requests_response = Requests::request( $url, $headers, $data, $type, $options );
$http_response = new WP_HTTP_Requests_Response( $requests_response, $r['filename'] );
$response = $http_response->to_array();
$response['http_response'] = $http_response;
}
catch ( Requests_Exception $e ) {
$response = new WP_Error( 'http_request_failed', $e->getMessage() );
}
reset_mbstring_encoding();
do_action( 'http_api_debug', $response, 'response', 'Requests', $r, $url );
if ( is_wp_error( $response ) ) {
return $response;
}
if ( ! $r['blocking'] ) {
return array(
'headers' => array(),
'body' => '',
'response' => array(
'code' => false,
'message' => false,
),
'cookies' => array(),
'http_response' => null,
);
}
return apply_filters( 'http_response', $response, $r, $url );
}
Expand Source Code
View on GitHub
Changelog
Changelog
Version |
Description |
2.7.0 |
Introduced. |