WP_Http::request() – Send an HTTP request to a URI.

You appear to be a bot. Output may be restricted

Description

Send an HTTP request to a URI.

Please note: The only URI that are supported in the HTTP Transport implementation are the HTTP and HTTPS protocols.

Usage

$array|WP_Error = WP_Http::request( $url, $args );

Parameters

$url
( string ) required – The request URL.
$args
( string|array ) optional – { Optional. Array or string of HTTP request arguments.
$method
( string ) optional – Request method. Accepts 'GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'TRACE', 'OPTIONS', or 'PATCH'. Some transports technically allow others, but should not be assumed. Default 'GET'.
$timeout
( float ) optional – How long the connection should stay open in seconds. Default 5.
$redirection
( int ) optional – Number of allowed redirects. Not supported by all transports. Default 5.
$httpversion
( string ) optional – Version of the HTTP protocol to use. Accepts '1.0' and '1.1'. Default '1.0'.
$user-agent
( string ) optional – User-agent value sent. Default 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ).
$reject_unsafe_urls
( bool ) optional – Whether to pass URLs through wp_http_validate_url(). Default false.
$blocking
( bool ) optional – Whether the calling code requires the result of the request. 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.
$headers
( string|array ) optional – Array or string of headers to send with the request. Default empty array.
$cookies
( array ) optional – List of cookies to send with the request. Default empty array.
$body
( string|array ) optional – Body to send with the request. Default null.
$compress
( bool ) optional – Whether to compress the $body when sending the request. Default false.
$decompress
( bool ) optional – 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.
$sslverify
( bool ) optional – Whether to verify SSL for the request. Default true.
$sslcertificates
( string ) optional – Absolute path to an SSL certificate .crt file. Default ABSPATH . WPINC . '/certificates/ca-bundle.crt'.
$stream
( bool ) optional – 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.
$filename
( string ) optional – Filename of the file to write to when streaming. $stream must be set to true. Default null.
$limit_response_size
( int ) optional – Size in bytes to limit the response to. Default null. }

Returns

array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error.

Source

File name: wordpress/wp-includes/class-wp-http.php
Lines:

1 to 100 of 300
  public function request( $url, $args = array() ) {
    $defaults = array(
      'method'              => 'GET',
      
/**
 * Filters the timeout value for an HTTP request.
 *
 * @since 2.7.0
 * @since 5.1.0 The `$url` parameter was added.
 *
 * @param float  $timeout_value Time in seconds until a request times out. Default 5.
 * @param string $url           The request URL.
 */
      'timeout'             => apply_filters( 'http_request_timeout', 5, $url ),
      
/**
 * Filters the number of redirects allowed during an HTTP request.
 *
 * @since 2.7.0
 * @since 5.1.0 The `$url` parameter was added.
 *
 * @param int    $redirect_count Number of redirects allowed. Default 5.
 * @param string $url            The request URL.
 */
      'redirection'         => apply_filters( 'http_request_redirection_count', 5, $url ),
      
/**
 * Filters the version of the HTTP protocol used in a request.
 *
 * @since 2.7.0
 * @since 5.1.0 The `$url` parameter was added.
 *
 * @param string $version Version of HTTP used. Accepts '1.0' and '1.1'. Default '1.0'.
 * @param string $url     The request URL.
 */
      'httpversion'         => apply_filters( 'http_request_version', '1.0', $url ),
      
/**
 * Filters the user agent value sent with an HTTP request.
 *
 * @since 2.7.0
 * @since 5.1.0 The `$url` parameter was added.
 *
 * @param string $user_agent WordPress user agent string.
 * @param string $url        The request URL.
 */
      'user-agent'          => apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ), $url ),
      
/**
 * Filters whether to pass URLs through wp_http_validate_url() in an HTTP request.
 *
 * @since 3.6.0
 * @since 5.1.0 The `$url` parameter was added.
 *
 * @param bool   $pass_url Whether to pass URLs through wp_http_validate_url(). Default false.
 * @param string $url      The request URL.
 */
      'reject_unsafe_urls'  => apply_filters( 'http_request_reject_unsafe_urls', false, $url ),
      '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,
    );

    // Pre-parse for the HEAD checks.
    $args = wp_parse_args( $args );

    // By default, HEAD requests do not cause redirections.
    if ( isset( $args['method'] ) && 'HEAD' === $args['method'] ) {
      $defaults['redirection'] = 0;
    }

    $parsed_args = wp_parse_args( $args, $defaults );
    
/**
 * Filters the arguments used in an HTTP request.
 *
 * @since 2.7.0
 *
 * @param array  $parsed_args An array of HTTP request arguments.
 * @param string $url         The request URL.
 */
    $parsed_args = apply_filters( 'http_request_args', $parsed_args, $url );

    // The transports decrement this, store a copy of the original value for loop purposes.
    if ( ! isset( $parsed_args['_redirection'] ) ) {
      $parsed_args['_redirection'] = $parsed_args['redirection'];
    }

    
/**
 * Filters the preemptive return value of an HTTP request.
 *
 * Returning a non-false value from the filter will short-circuit the HTTP request and return
 * early with that value. A filter should return one of:
 *
 *  - An array containing 'headers', 'body', 'response', 'cookies', and 'filename' elements
 *  - A WP_Error instance
 *  - boolean false to avoid short-circuiting the response
 *
 

 View on GitHub View on Trac