3 * Core HTTP Request API
5 * Standardizes the HTTP requests for WordPress. Handles cookies, gzip encoding and decoding, chunk
6 * decoding, if HTTP 1.1 and various other difficult HTTP protocol implementations.
13 * Returns the initialized WP_Http Object
18 * @staticvar WP_Http $http
20 * @return WP_Http HTTP Transport object.
22 function _wp_http_get_object() {
25 if ( is_null( $http ) ) {
26 $http = new WP_Http();
32 * Retrieve the raw response from a safe HTTP request.
34 * This function is ideal when the HTTP request is being made to an arbitrary
35 * URL. The URL is validated to avoid redirection and request forgery attacks.
39 * @see wp_remote_request() For more information on the response array format.
40 * @see WP_Http::request() For default arguments information.
42 * @param string $url Site URL to retrieve.
43 * @param array $args Optional. Request arguments. Default empty array.
44 * @return WP_Error|array The response or WP_Error on failure.
46 function wp_safe_remote_request( $url, $args = array() ) {
47 $args['reject_unsafe_urls'] = true;
48 $http = _wp_http_get_object();
49 return $http->request( $url, $args );
53 * Retrieve the raw response from a safe HTTP request using the GET method.
55 * This function is ideal when the HTTP request is being made to an arbitrary
56 * URL. The URL is validated to avoid redirection and request forgery attacks.
60 * @see wp_remote_request() For more information on the response array format.
61 * @see WP_Http::request() For default arguments information.
63 * @param string $url Site URL to retrieve.
64 * @param array $args Optional. Request arguments. Default empty array.
65 * @return WP_Error|array The response or WP_Error on failure.
67 function wp_safe_remote_get( $url, $args = array() ) {
68 $args['reject_unsafe_urls'] = true;
69 $http = _wp_http_get_object();
70 return $http->get( $url, $args );
74 * Retrieve the raw response from a safe HTTP request using the POST method.
76 * This function is ideal when the HTTP request is being made to an arbitrary
77 * URL. The URL is validated to avoid redirection and request forgery attacks.
81 * @see wp_remote_request() For more information on the response array format.
82 * @see WP_Http::request() For default arguments information.
84 * @param string $url Site URL to retrieve.
85 * @param array $args Optional. Request arguments. Default empty array.
86 * @return WP_Error|array The response or WP_Error on failure.
88 function wp_safe_remote_post( $url, $args = array() ) {
89 $args['reject_unsafe_urls'] = true;
90 $http = _wp_http_get_object();
91 return $http->post( $url, $args );
95 * Retrieve the raw response from a safe HTTP request using the HEAD method.
97 * This function is ideal when the HTTP request is being made to an arbitrary
98 * URL. The URL is validated to avoid redirection and request forgery attacks.
102 * @see wp_remote_request() For more information on the response array format.
103 * @see WP_Http::request() For default arguments information.
105 * @param string $url Site URL to retrieve.
106 * @param array $args Optional. Request arguments. Default empty array.
107 * @return WP_Error|array The response or WP_Error on failure.
109 function wp_safe_remote_head( $url, $args = array() ) {
110 $args['reject_unsafe_urls'] = true;
111 $http = _wp_http_get_object();
112 return $http->head( $url, $args );
116 * Retrieve the raw response from the HTTP request.
118 * The array structure is a little complex:
121 * 'headers' => array(),
122 * 'response' => array(
124 * 'message' => string
128 * All of the headers in $res['headers'] are with the name as the key and the
129 * value as the value. So to get the User-Agent, you would do the following.
131 * $user_agent = $res['headers']['user-agent'];
133 * The body is the raw response content and can be retrieved from $res['body'].
135 * This function is called first to make the request and there are other API
136 * functions to abstract out the above convoluted setup.
138 * Request method defaults for helper functions:
139 * - Default 'GET' for wp_remote_get()
140 * - Default 'POST' for wp_remote_post()
141 * - Default 'HEAD' for wp_remote_head()
145 * @see WP_Http::request() For additional information on default arguments.
147 * @param string $url Site URL to retrieve.
148 * @param array $args Optional. Request arguments. Default empty array.
149 * @return WP_Error|array The response or WP_Error on failure.
151 function wp_remote_request($url, $args = array()) {
152 $http = _wp_http_get_object();
153 return $http->request( $url, $args );
157 * Retrieve the raw response from the HTTP request using the GET method.
161 * @see wp_remote_request() For more information on the response array format.
162 * @see WP_Http::request() For default arguments information.
164 * @param string $url Site URL to retrieve.
165 * @param array $args Optional. Request arguments. Default empty array.
166 * @return WP_Error|array The response or WP_Error on failure.
168 function wp_remote_get($url, $args = array()) {
169 $http = _wp_http_get_object();
170 return $http->get( $url, $args );
174 * Retrieve the raw response from the HTTP request using the POST method.
178 * @see wp_remote_request() For more information on the response array format.
179 * @see WP_Http::request() For default arguments information.
181 * @param string $url Site URL to retrieve.
182 * @param array $args Optional. Request arguments. Default empty array.
183 * @return WP_Error|array The response or WP_Error on failure.
185 function wp_remote_post($url, $args = array()) {
186 $http = _wp_http_get_object();
187 return $http->post( $url, $args );
191 * Retrieve the raw response from the HTTP request using the HEAD method.
195 * @see wp_remote_request() For more information on the response array format.
196 * @see WP_Http::request() For default arguments information.
198 * @param string $url Site URL to retrieve.
199 * @param array $args Optional. Request arguments. Default empty array.
200 * @return WP_Error|array The response or WP_Error on failure.
202 function wp_remote_head($url, $args = array()) {
203 $http = _wp_http_get_object();
204 return $http->head( $url, $args );
208 * Retrieve only the headers from the raw response.
211 * @since 4.6.0 Return value changed from an array to an Requests_Utility_CaseInsensitiveDictionary instance.
213 * @see \Requests_Utility_CaseInsensitiveDictionary
215 * @param array $response HTTP response.
216 * @return array|\Requests_Utility_CaseInsensitiveDictionary The headers of the response. Empty array if incorrect parameter given.
218 function wp_remote_retrieve_headers( $response ) {
219 if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) {
223 return $response['headers'];
227 * Retrieve a single header by name from the raw response.
231 * @param array $response
232 * @param string $header Header name to retrieve value from.
233 * @return string The header value. Empty string on if incorrect parameter given, or if the header doesn't exist.
235 function wp_remote_retrieve_header( $response, $header ) {
236 if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) {
240 if ( isset( $response['headers'][ $header ] ) ) {
241 return $response['headers'][$header];
248 * Retrieve only the response code from the raw response.
250 * Will return an empty array if incorrect parameter value is given.
254 * @param array $response HTTP response.
255 * @return int|string The response code as an integer. Empty string on incorrect parameter given.
257 function wp_remote_retrieve_response_code( $response ) {
258 if ( is_wp_error($response) || ! isset($response['response']) || ! is_array($response['response']))
261 return $response['response']['code'];
265 * Retrieve only the response message from the raw response.
267 * Will return an empty array if incorrect parameter value is given.
271 * @param array $response HTTP response.
272 * @return string The response message. Empty string on incorrect parameter given.
274 function wp_remote_retrieve_response_message( $response ) {
275 if ( is_wp_error($response) || ! isset($response['response']) || ! is_array($response['response']))
278 return $response['response']['message'];
282 * Retrieve only the body from the raw response.
286 * @param array $response HTTP response.
287 * @return string The body of the response. Empty string if no body or incorrect parameter given.
289 function wp_remote_retrieve_body( $response ) {
290 if ( is_wp_error($response) || ! isset($response['body']) )
293 return $response['body'];
297 * Retrieve only the cookies from the raw response.
301 * @param array $response HTTP response.
302 * @return array An array of `WP_Http_Cookie` objects from the response. Empty array if there are none, or the response is a WP_Error.
304 function wp_remote_retrieve_cookies( $response ) {
305 if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) {
309 return $response['cookies'];
313 * Retrieve a single cookie by name from the raw response.
317 * @param array $response HTTP response.
318 * @param string $name The name of the cookie to retrieve.
319 * @return WP_Http_Cookie|string The `WP_Http_Cookie` object. Empty string if the cookie isn't present in the response.
321 function wp_remote_retrieve_cookie( $response, $name ) {
322 $cookies = wp_remote_retrieve_cookies( $response );
324 if ( empty( $cookies ) ) {
328 foreach ( $cookies as $cookie ) {
329 if ( $cookie->name === $name ) {
338 * Retrieve a single cookie's value by name from the raw response.
342 * @param array $response HTTP response.
343 * @param string $name The name of the cookie to retrieve.
344 * @return string The value of the cookie. Empty string if the cookie isn't present in the response.
346 function wp_remote_retrieve_cookie_value( $response, $name ) {
347 $cookie = wp_remote_retrieve_cookie( $response, $name );
349 if ( ! is_a( $cookie, 'WP_Http_Cookie' ) ) {
353 return $cookie->value;
357 * Determines if there is an HTTP Transport that can process this request.
361 * @param array $capabilities Array of capabilities to test or a wp_remote_request() $args array.
362 * @param string $url Optional. If given, will check if the URL requires SSL and adds
363 * that requirement to the capabilities array.
367 function wp_http_supports( $capabilities = array(), $url = null ) {
368 $http = _wp_http_get_object();
370 $capabilities = wp_parse_args( $capabilities );
372 $count = count( $capabilities );
374 // If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array
375 if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) == $count ) {
376 $capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) );
379 if ( $url && !isset( $capabilities['ssl'] ) ) {
380 $scheme = parse_url( $url, PHP_URL_SCHEME );
381 if ( 'https' == $scheme || 'ssl' == $scheme ) {
382 $capabilities['ssl'] = true;
386 return (bool) $http->_get_first_available_transport( $capabilities );
390 * Get the HTTP Origin of the current request.
394 * @return string URL of the origin. Empty string if no origin.
396 function get_http_origin() {
398 if ( ! empty ( $_SERVER[ 'HTTP_ORIGIN' ] ) )
399 $origin = $_SERVER[ 'HTTP_ORIGIN' ];
402 * Change the origin of an HTTP request.
406 * @param string $origin The original origin for the request.
408 return apply_filters( 'http_origin', $origin );
412 * Retrieve list of allowed HTTP origins.
416 * @return array Array of origin URLs.
418 function get_allowed_http_origins() {
419 $admin_origin = parse_url( admin_url() );
420 $home_origin = parse_url( home_url() );
422 // @todo preserve port?
423 $allowed_origins = array_unique( array(
424 'http://' . $admin_origin[ 'host' ],
425 'https://' . $admin_origin[ 'host' ],
426 'http://' . $home_origin[ 'host' ],
427 'https://' . $home_origin[ 'host' ],
431 * Change the origin types allowed for HTTP requests.
435 * @param array $allowed_origins {
436 * Default allowed HTTP origins.
437 * @type string Non-secure URL for admin origin.
438 * @type string Secure URL for admin origin.
439 * @type string Non-secure URL for home origin.
440 * @type string Secure URL for home origin.
443 return apply_filters( 'allowed_http_origins' , $allowed_origins );
447 * Determines if the HTTP origin is an authorized one.
451 * @param null|string $origin Origin URL. If not provided, the value of get_http_origin() is used.
452 * @return string Origin URL if allowed, empty string if not.
454 function is_allowed_http_origin( $origin = null ) {
455 $origin_arg = $origin;
457 if ( null === $origin )
458 $origin = get_http_origin();
460 if ( $origin && ! in_array( $origin, get_allowed_http_origins() ) )
464 * Change the allowed HTTP origin result.
468 * @param string $origin Origin URL if allowed, empty string if not.
469 * @param string $origin_arg Original origin string passed into is_allowed_http_origin function.
471 return apply_filters( 'allowed_http_origin', $origin, $origin_arg );
475 * Send Access-Control-Allow-Origin and related headers if the current request
476 * is from an allowed origin.
478 * If the request is an OPTIONS request, the script exits with either access
479 * control headers sent, or a 403 response if the origin is not allowed. For
480 * other request methods, you will receive a return value.
484 * @return string|false Returns the origin URL if headers are sent. Returns false
485 * if headers are not sent.
487 function send_origin_headers() {
488 $origin = get_http_origin();
490 if ( is_allowed_http_origin( $origin ) ) {
491 @header( 'Access-Control-Allow-Origin: ' . $origin );
492 @header( 'Access-Control-Allow-Credentials: true' );
493 if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] )
498 if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) {
499 status_header( 403 );
507 * Validate a URL for safe use in the HTTP API.
512 * @return false|string URL or false on failure.
514 function wp_http_validate_url( $url ) {
515 $original_url = $url;
516 $url = wp_kses_bad_protocol( $url, array( 'http', 'https' ) );
517 if ( ! $url || strtolower( $url ) !== strtolower( $original_url ) )
520 $parsed_url = @parse_url( $url );
521 if ( ! $parsed_url || empty( $parsed_url['host'] ) )
524 if ( isset( $parsed_url['user'] ) || isset( $parsed_url['pass'] ) )
527 if ( false !== strpbrk( $parsed_url['host'], ':#?[]' ) )
530 $parsed_home = @parse_url( get_option( 'home' ) );
532 if ( isset( $parsed_home['host'] ) ) {
533 $same_host = ( strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] ) || 'localhost' === strtolower( $parsed_url['host'] ) );
538 if ( ! $same_host ) {
539 $host = trim( $parsed_url['host'], '.' );
540 if ( preg_match( '#^(([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)$#', $host ) ) {
543 $ip = gethostbyname( $host );
544 if ( $ip === $host ) // Error condition for gethostbyname()
548 $parts = array_map( 'intval', explode( '.', $ip ) );
549 if ( 127 === $parts[0] || 10 === $parts[0] || 0 === $parts[0]
550 || ( 172 === $parts[0] && 16 <= $parts[1] && 31 >= $parts[1] )
551 || ( 192 === $parts[0] && 168 === $parts[1] )
553 // If host appears local, reject unless specifically allowed.
555 * Check if HTTP request is external or not.
557 * Allows to change and allow external requests for the HTTP request.
561 * @param bool false Whether HTTP request is external or not.
562 * @param string $host IP of the requested host.
563 * @param string $url URL of the requested host.
565 if ( ! apply_filters( 'http_request_host_is_external', false, $host, $url ) )
571 if ( empty( $parsed_url['port'] ) )
574 $port = $parsed_url['port'];
575 if ( 80 === $port || 443 === $port || 8080 === $port )
578 if ( $parsed_home && $same_host && isset( $parsed_home['port'] ) && $parsed_home['port'] === $port )
585 * Whitelists allowed redirect hosts for safe HTTP requests as well.
587 * Attached to the {@see 'http_request_host_is_external'} filter.
591 * @param bool $is_external
592 * @param string $host
595 function allowed_http_request_hosts( $is_external, $host ) {
596 if ( ! $is_external && wp_validate_redirect( 'http://' . $host ) )
602 * Whitelists any domain in a multisite installation for safe HTTP requests.
604 * Attached to the {@see 'http_request_host_is_external'} filter.
608 * @global wpdb $wpdb WordPress database abstraction object.
609 * @staticvar array $queried
611 * @param bool $is_external
612 * @param string $host
615 function ms_allowed_http_request_hosts( $is_external, $host ) {
617 static $queried = array();
620 if ( $host === get_network()->domain )
622 if ( isset( $queried[ $host ] ) )
623 return $queried[ $host ];
624 $queried[ $host ] = (bool) $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $host ) );
625 return $queried[ $host ];
629 * A wrapper for PHP's parse_url() function that handles consistency in the return
630 * values across PHP versions.
632 * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including
633 * schemeless and relative url's with :// in the path. This function works around
634 * those limitations providing a standard output on PHP 5.2~5.4+.
636 * Secondly, across various PHP versions, schemeless URLs starting containing a ":"
637 * in the query are being handled inconsistently. This function works around those
638 * differences as well.
640 * Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated
641 * when URL parsing failed.
644 * @since 4.7.0 The $component parameter was added for parity with PHP's parse_url().
646 * @param string $url The URL to parse.
647 * @param int $component The specific component to retrieve. Use one of the PHP
648 * predefined constants to specify which one.
649 * Defaults to -1 (= return all parts as an array).
650 * @see http://php.net/manual/en/function.parse-url.php
651 * @return mixed False on parse failure; Array of URL components on success;
652 * When a specific component has been requested: null if the component
653 * doesn't exist in the given URL; a sting or - in the case of
654 * PHP_URL_PORT - integer when it does. See parse_url()'s return values.
656 function wp_parse_url( $url, $component = -1 ) {
658 $url = strval( $url );
660 if ( '//' === substr( $url, 0, 2 ) ) {
661 $to_unset[] = 'scheme';
662 $url = 'placeholder:' . $url;
663 } elseif ( '/' === substr( $url, 0, 1 ) ) {
664 $to_unset[] = 'scheme';
665 $to_unset[] = 'host';
666 $url = 'placeholder://placeholder' . $url;
669 $parts = @parse_url( $url );
671 if ( false === $parts ) {
676 // Remove the placeholder values.
677 foreach ( $to_unset as $key ) {
678 unset( $parts[ $key ] );
681 return _get_component_from_parsed_url_array( $parts, $component );
685 * Retrieve a specific component from a parsed URL array.
691 * @param array|false $url_parts The parsed URL. Can be false if the URL failed to parse.
692 * @param int $component The specific component to retrieve. Use one of the PHP
693 * predefined constants to specify which one.
694 * Defaults to -1 (= return all parts as an array).
695 * @see http://php.net/manual/en/function.parse-url.php
696 * @return mixed False on parse failure; Array of URL components on success;
697 * When a specific component has been requested: null if the component
698 * doesn't exist in the given URL; a sting or - in the case of
699 * PHP_URL_PORT - integer when it does. See parse_url()'s return values.
701 function _get_component_from_parsed_url_array( $url_parts, $component = -1 ) {
702 if ( -1 === $component ) {
706 $key = _wp_translate_php_url_constant_to_key( $component );
707 if ( false !== $key && is_array( $url_parts ) && isset( $url_parts[ $key ] ) ) {
708 return $url_parts[ $key ];
715 * Translate a PHP_URL_* constant to the named array keys PHP uses.
721 * @see http://php.net/manual/en/url.constants.php
723 * @param int $constant PHP_URL_* constant.
724 * @return string|bool The named key or false.
726 function _wp_translate_php_url_constant_to_key( $constant ) {
727 $translation = array(
728 PHP_URL_SCHEME => 'scheme',
729 PHP_URL_HOST => 'host',
730 PHP_URL_PORT => 'port',
731 PHP_URL_USER => 'user',
732 PHP_URL_PASS => 'pass',
733 PHP_URL_PATH => 'path',
734 PHP_URL_QUERY => 'query',
735 PHP_URL_FRAGMENT => 'fragment',
738 if ( isset( $translation[ $constant ] ) ) {
739 return $translation[ $constant ];