'GET', /** * Filters the timeout value for an HTTP request. * * @since 2.7.0 * * @param int $timeout_value Time in seconds until a request times out. * Default 5. */ 'timeout' => apply_filters( 'http_request_timeout', 5 ), /** * Filters the number of redirects allowed during an HTTP request. * * @since 2.7.0 * * @param int $redirect_count Number of redirects allowed. Default 5. */ 'redirection' => apply_filters( 'http_request_redirection_count', 5 ), /** * Filters the version of the HTTP protocol used in a request. * * @since 2.7.0 * * @param string $version Version of HTTP used. Accepts '1.0' and '1.1'. * Default '1.0'. */ 'httpversion' => apply_filters( 'http_request_version', '1.0' ), /** * Filters the user agent value sent with an HTTP request. * * @since 2.7.0 * * @param string $user_agent WordPress user agent string. */ 'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ) ), /** * Filters whether to pass URLs through wp_http_validate_url() in an HTTP request. * * @since 3.6.0 * * @param bool $pass_url Whether to pass URLs through wp_http_validate_url(). * Default false. */ '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, 'data_format' => 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; } $request_args = wp_parse_args( $args, $defaults ); /** * Filters the arguments used in an HTTP request. * * @since 2.7.0 * * @param array $request_args An array of HTTP request arguments. * @param string $url The request URL. */ $request_args = apply_filters( 'http_request_args', $request_args, $url ); // The transports decrement this, store a copy of the original value for loop purposes. if ( ! isset( $request_args['_redirection'] ) ) { $request_args['_redirection'] = $request_args['redirection']; } /** * Filters whether to preempt an HTTP request's return value. * * Returning a non-false value from the filter will short-circuit the HTTP request and return * early with that value. A filter should return either: * * - An array containing 'headers', 'body', 'response', 'cookies', and 'filename' elements * - A WP_Error instance * - boolean false (to avoid short-circuiting the response) * * Returning any other value may result in unexpected behaviour. * * @since 2.9.0 * * @param false|array|WP_Error $preempt Whether to preempt an HTTP request's return value. Default false. * @param array $request_args HTTP request arguments. * @param string $url The request URL. */ $pre = apply_filters( 'pre_http_request', false, $request_args, $url ); if ( false !== $pre ) { return $pre; } if ( function_exists( 'wp_kses_bad_protocol' ) ) { if ( $request_args['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', esc_html__( 'A valid URL was not provided.' ) ); } if ( $this->block_request( $url ) ) { return new WP_Error( 'http_request_failed', esc_html__( 'User has blocked requests through HTTP.' ) ); } // If we are streaming to a file but no filename was given drop it in the WP temp dir // and pick its name using the basename of the $url if ( $request_args['stream'] ) { if ( empty( $request_args['filename'] ) ) { $request_args['filename'] = get_temp_dir() . basename( $url ); } // Force some settings if we are streaming to a file and check for existence and perms of destination directory $request_args['blocking'] = true; if ( ! wp_is_writable( dirname( $request_args['filename'] ) ) ) { return new WP_Error( 'http_request_failed', esc_html__( 'Destination directory for file streaming does not exist or is not writable.' ) ); } } if ( is_null( $request_args['headers'] ) ) { $request_args['headers'] = array(); } // WP allows passing in headers as a string, weirdly. if ( ! is_array( $request_args['headers'] ) ) { $processedHeaders = WP_Http::processHeaders( $request_args['headers'] ); $request_args['headers'] = $processedHeaders['headers']; } // Setup arguments $headers = $request_args['headers']; $data = $request_args['body']; $type = $request_args['method']; $options = array( 'timeout' => $request_args['timeout'], 'useragent' => $request_args['user-agent'], 'blocking' => $request_args['blocking'], 'hooks' => new WP_HTTP_Requests_Hooks( $url, $request_args ), ); // Ensure redirects follow browser behaviour. $options['hooks']->register( 'requests.before_redirect', array( get_class(), 'browser_redirect_compatibility' ) ); if ( $request_args['stream'] ) { $options['filename'] = $request_args['filename']; } if ( empty( $request_args['redirection'] ) ) { $options['follow_redirects'] = false; } else { $options['redirects'] = $request_args['redirection']; } // Use byte limit, if we can if ( isset( $request_args['limit_response_size'] ) ) { $options['max_bytes'] = $request_args['limit_response_size']; } // If we've got cookies, use and convert them to Requests_Cookie. if ( ! empty( $request_args['cookies'] ) ) { $options['cookies'] = WP_Http::normalize_cookies( $request_args['cookies'] ); } // SSL certificate handling if ( ! $request_args['sslverify'] ) { $options['verify'] = false; $options['verifyname'] = false; } else { $options['verify'] = $request_args['sslcertificates']; } if ( null !== $request_args['data_format'] ) { $options['data_format'] = $request_args['data_format']; } elseif ( 'HEAD' !== $type && 'GET' !== $type ) { // All non-GET/HEAD requests should put the arguments in the form body. $options['data_format'] = 'body'; } /** * Filters whether SSL should be verified for non-local requests. * * @since 2.8.0 * * @param bool $ssl_verify Whether to verify the SSL connection. Default true. */ $options['verify'] = apply_filters( 'https_ssl_verify', $options['verify'] ); // Check for proxies. $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(); } } // Avoid issues where mbstring.func_overload is enabled mbstring_binary_safe_encoding(); try { $requests_response = Requests::request( $url, $headers, $data, $type, $options ); // Convert the response into an array $http_response = new WP_HTTP_Requests_Response( $requests_response, $request_args['filename'] ); $response = $http_response->to_array(); // Add the original object to the array. $response['http_response'] = $http_response; } catch ( Requests_Exception $e ) { $response = new WP_Error( 'http_request_failed', $e->getMessage() ); } reset_mbstring_encoding(); /** * Fires after an HTTP API response is received and before the response is returned. * * @since 2.8.0 * * @param array|WP_Error $response HTTP response or WP_Error object. * @param string $context Context under which the hook is fired. * @param string $class HTTP transport used. * @param array $args HTTP request arguments. * @param string $url The request URL. */ do_action( 'http_api_debug', $response, 'response', 'Requests', $request_args, $url ); if ( is_wp_error( $response ) ) { return $response; } if ( ! $request_args['blocking'] ) { return array( 'headers' => array(), 'body' => '', 'response' => array( 'code' => false, 'message' => false, ), 'cookies' => array(), 'http_response' => null, ); } /** * Filters the HTTP API response immediately before the response is returned. * * @since 2.9.0 * * @param array $response HTTP response. * @param array $request_args HTTP request arguments. * @param string $url The request URL. */ return apply_filters( 'http_response', $response, $request_args, $url ); } }