/** * Send a HTTP request to a URI using HTTP extension. * * Does not support non-blocking. * * @access public * @since 2.7 * * @param string $url * @param str|array $args Optional. Override the defaults. * @return array 'headers', 'body', 'cookies' and 'response' keys. */ function request($url, $args = array()) { $defaults = array('method' => 'GET', 'timeout' => 5, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => null, 'cookies' => array()); $r = wp_parse_args($args, $defaults); if (isset($r['headers']['User-Agent'])) { $r['user-agent'] = $r['headers']['User-Agent']; unset($r['headers']['User-Agent']); } else { if (isset($r['headers']['user-agent'])) { $r['user-agent'] = $r['headers']['user-agent']; unset($r['headers']['user-agent']); } } // Construct Cookie: header if any cookies are set WP_Http::buildCookieHeader($r); switch ($r['method']) { case 'POST': $r['method'] = HTTP_METH_POST; break; case 'HEAD': $r['method'] = HTTP_METH_HEAD; break; case 'PUT': $r['method'] = HTTP_METH_PUT; break; case 'GET': default: $r['method'] = HTTP_METH_GET; } $arrURL = parse_url($url); if ('http' != $arrURL['scheme'] || 'https' != $arrURL['scheme']) { $url = preg_replace('|^' . preg_quote($arrURL['scheme'], '|') . '|', 'http', $url); } $is_local = isset($args['local']) && $args['local']; $ssl_verify = isset($args['sslverify']) && $args['sslverify']; if ($is_local) { $ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify); } elseif (!$is_local) { $ssl_verify = apply_filters('https_ssl_verify', $ssl_verify); } $r['timeout'] = (int) ceil($r['timeout']); $options = array('timeout' => $r['timeout'], 'connecttimeout' => $r['timeout'], 'redirect' => $r['redirection'], 'useragent' => $r['user-agent'], 'headers' => $r['headers'], 'ssl' => array('verifypeer' => $ssl_verify, 'verifyhost' => $ssl_verify)); if (HTTP_METH_HEAD == $r['method']) { $options['redirect'] = 0; } // Assumption: Docs seem to suggest that this means do not follow. Untested. // The HTTP extensions offers really easy proxy support. $proxy = new WP_HTTP_Proxy(); if ($proxy->is_enabled() && $proxy->send_through_proxy($url)) { $options['proxyhost'] = $proxy->host(); $options['proxyport'] = $proxy->port(); $options['proxytype'] = HTTP_PROXY_HTTP; if ($proxy->use_authentication()) { $options['proxyauth'] = $proxy->authentication(); $options['proxyauthtype'] = HTTP_AUTH_ANY; } } if (!WP_DEBUG) { //Emits warning level notices for max redirects and timeouts $strResponse = @http_request($r['method'], $url, $r['body'], $options, $info); } else { $strResponse = http_request($r['method'], $url, $r['body'], $options, $info); } //Emits warning level notices for max redirects and timeouts // Error may still be set, Response may return headers or partial document, and error // contains a reason the request was aborted, eg, timeout expired or max-redirects reached. if (false === $strResponse || !empty($info['error'])) { return new WP_Error('http_request_failed', $info['response_code'] . ': ' . $info['error']); } if (!$r['blocking']) { return array('headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array()); } $headers_body = WP_HTTP::processResponse($strResponse); $theHeaders = $headers_body['headers']; $theBody = $headers_body['body']; unset($headers_body); $theHeaders = WP_Http::processHeaders($theHeaders); if (!empty($theBody) && isset($theHeaders['headers']['transfer-encoding']) && 'chunked' == $theHeaders['headers']['transfer-encoding']) { if (!WP_DEBUG) { $theBody = @http_chunked_decode($theBody); } else { $theBody = http_chunked_decode($theBody); } } if (true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders['headers'])) { $theBody = http_inflate($theBody); } $theResponse = array(); $theResponse['code'] = $info['response_code']; $theResponse['message'] = get_status_header_desc($info['response_code']); return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $theResponse, 'cookies' => $theHeaders['cookies']); }