3 * Simple and uniform HTTP request API.
5 * Will eventually replace and standardize the WordPress HTTP requests made.
7 * @link http://trac.wordpress.org/ticket/4779 HTTP API Proposal
15 * Returns the initialized WP_Http Object
20 * @return WP_Http HTTP Transport object.
22 function &_wp_http_get_object() {
26 $http = new WP_Http();
32 * Retrieve the raw response from the HTTP request.
34 * The array structure is a little complex.
37 * $res = array( 'headers' => array(), 'response' => array('code' => int, 'message' => string) );
40 * All of the headers in $res['headers'] are with the name as the key and the
41 * value as the value. So to get the User-Agent, you would do the following.
44 * $user_agent = $res['headers']['user-agent'];
47 * The body is the raw response content and can be retrieved from $res['body'].
49 * This function is called first to make the request and there are other API
50 * functions to abstract out the above convoluted setup.
54 * @param string $url Site URL to retrieve.
55 * @param array $args Optional. Override the defaults.
56 * @return WP_Error|array The response or WP_Error on failure.
58 function wp_remote_request($url, $args = array()) {
59 $objFetchSite = _wp_http_get_object();
60 return $objFetchSite->request($url, $args);
64 * Retrieve the raw response from the HTTP request using the GET method.
66 * @see wp_remote_request() For more information on the response array format.
70 * @param string $url Site URL to retrieve.
71 * @param array $args Optional. Override the defaults.
72 * @return WP_Error|array The response or WP_Error on failure.
74 function wp_remote_get($url, $args = array()) {
75 $objFetchSite = _wp_http_get_object();
76 return $objFetchSite->get($url, $args);
80 * Retrieve the raw response from the HTTP request using the POST method.
82 * @see wp_remote_request() For more information on the response array format.
86 * @param string $url Site URL to retrieve.
87 * @param array $args Optional. Override the defaults.
88 * @return WP_Error|array The response or WP_Error on failure.
90 function wp_remote_post($url, $args = array()) {
91 $objFetchSite = _wp_http_get_object();
92 return $objFetchSite->post($url, $args);
96 * Retrieve the raw response from the HTTP request using the HEAD method.
98 * @see wp_remote_request() For more information on the response array format.
102 * @param string $url Site URL to retrieve.
103 * @param array $args Optional. Override the defaults.
104 * @return WP_Error|array The response or WP_Error on failure.
106 function wp_remote_head($url, $args = array()) {
107 $objFetchSite = _wp_http_get_object();
108 return $objFetchSite->head($url, $args);
112 * Retrieve only the headers from the raw response.
116 * @param array $response HTTP response.
117 * @return array The headers of the response. Empty array if incorrect parameter given.
119 function wp_remote_retrieve_headers(&$response) {
120 if ( is_wp_error($response) || ! isset($response['headers']) || ! is_array($response['headers']))
123 return $response['headers'];
127 * Retrieve a single header by name from the raw response.
131 * @param array $response
132 * @param string $header Header name to retrieve value from.
133 * @return string The header value. Empty string on if incorrect parameter given, or if the header doesn't exist.
135 function wp_remote_retrieve_header(&$response, $header) {
136 if ( is_wp_error($response) || ! isset($response['headers']) || ! is_array($response['headers']))
139 if ( array_key_exists($header, $response['headers']) )
140 return $response['headers'][$header];
146 * Retrieve only the response code from the raw response.
148 * Will return an empty array if incorrect parameter value is given.
152 * @param array $response HTTP response.
153 * @return string the response code. Empty string on incorrect parameter given.
155 function wp_remote_retrieve_response_code(&$response) {
156 if ( is_wp_error($response) || ! isset($response['response']) || ! is_array($response['response']))
159 return $response['response']['code'];
163 * Retrieve only the response message from the raw response.
165 * Will return an empty array if incorrect parameter value is given.
169 * @param array $response HTTP response.
170 * @return string The response message. Empty string on incorrect parameter given.
172 function wp_remote_retrieve_response_message(&$response) {
173 if ( is_wp_error($response) || ! isset($response['response']) || ! is_array($response['response']))
176 return $response['response']['message'];
180 * Retrieve only the body from the raw response.
184 * @param array $response HTTP response.
185 * @return string The body of the response. Empty string if no body or incorrect parameter given.
187 function wp_remote_retrieve_body(&$response) {
188 if ( is_wp_error($response) || ! isset($response['body']) )
191 return $response['body'];
195 * Determines if there is an HTTP Transport that can process this request.
199 * @param array $capabilities Array of capabilities to test or a wp_remote_request() $args array.
200 * @param string $url Optional. If given, will check if the URL requires SSL and adds that requirement to the capabilities array.
204 function wp_http_supports( $capabilities = array(), $url = null ) {
205 $objFetchSite = _wp_http_get_object();
207 $capabilities = wp_parse_args( $capabilities );
209 $count = count( $capabilities );
211 // If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array
212 if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) == $count ) {
213 $capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) );
216 if ( $url && !isset( $capabilities['ssl'] ) ) {
217 $scheme = parse_url( $url, PHP_URL_SCHEME );
218 if ( 'https' == $scheme || 'ssl' == $scheme ) {
219 $capabilities['ssl'] = true;
223 return (bool) $objFetchSite->_get_first_available_transport( $capabilities );
227 * Get the HTTP Origin of the current request.
231 * @return string URL of the origin. Empty string if no origin.
233 function get_http_origin() {
235 if ( ! empty ( $_SERVER[ 'HTTP_ORIGIN' ] ) )
236 $origin = $_SERVER[ 'HTTP_ORIGIN' ];
238 return apply_filters( 'http_origin', $origin );
242 * Retrieve list of allowed http origins.
246 * @return array Array of origin URLs.
248 function get_allowed_http_origins() {
249 $admin_origin = parse_url( admin_url() );
250 $home_origin = parse_url( home_url() );
252 // @todo preserve port?
253 $allowed_origins = array_unique( array(
254 'http://' . $admin_origin[ 'host' ],
255 'https://' . $admin_origin[ 'host' ],
256 'http://' . $home_origin[ 'host' ],
257 'https://' . $home_origin[ 'host' ],
260 return apply_filters( 'allowed_http_origins' , $allowed_origins );
264 * Determines if the http origin is an authorized one.
268 * @param string Origin URL. If not provided, the value of get_http_origin() is used.
269 * @return bool True if the origin is allowed. False otherwise.
271 function is_allowed_http_origin( $origin = null ) {
272 $origin_arg = $origin;
274 if ( null === $origin )
275 $origin = get_http_origin();
277 if ( $origin && ! in_array( $origin, get_allowed_http_origins() ) )
280 return apply_filters( 'allowed_http_origin', $origin, $origin_arg );
284 * Send Access-Control-Allow-Origin and related headers if the current request
285 * is from an allowed origin.
289 * @return bool|string Returns the origin URL if headers are sent. Returns false
290 * if headers are not sent.
292 function send_origin_headers() {
293 $origin = get_http_origin();
294 if ( ! is_allowed_http_origin( $origin ) )
297 @header( 'Access-Control-Allow-Origin: ' . $origin );
298 @header( 'Access-Control-Allow-Credentials: true' );