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
12 * @author Jacob Santos <wordpress@santosj.name>
16 * WordPress HTTP Class for managing HTTP Transports and making HTTP requests.
18 * This class is called for the functionality of making HTTP requests and should
19 * replace Snoopy functionality, eventually. There is no available functionality
20 * to add HTTP transport implementations, since most of the HTTP transports are
21 * added and available for use.
23 * The exception is that cURL is not available as a transport and lacking an
24 * implementation. It will be added later and should be a patch on the WordPress
27 * There are no properties, because none are needed and for performance reasons.
28 * Some of the functions are static and while they do have some overhead over
29 * functions in PHP4, the purpose is maintainability. When PHP5 is finally the
30 * requirement, it will be easy to add the static keyword to the code. It is not
31 * as easy to convert a function to a method after enough code uses the old way.
40 * PHP4 style Constructor - Calls PHP5 Style Constructor
50 * PHP5 style Constructor - Setup available transport if not available.
52 * PHP4 does not have the 'self' keyword and since WordPress supports PHP4,
53 * the class needs to be used for the static call.
55 * The transport are setup to save time. This should only be called once, so
56 * the overhead should be fine.
61 function __construct() {
62 WP_Http::_getTransport();
63 WP_Http::_postTransport();
67 * Tests the WordPress HTTP objects for an object to use and returns it.
69 * Tests all of the objects and returns the object that passes. Also caches
70 * that object to be used later.
72 * The order for the GET/HEAD requests are Streams, HTTP Extension, Fopen,
73 * and finally Fsockopen. fsockopen() is used last, because it has the most
74 * overhead in its implementation. There isn't any real way around it, since
75 * redirects have to be supported, much the same way the other transports
76 * also handle redirects.
78 * There are currently issues with "localhost" not resolving correctly with
79 * DNS. This may cause an error "failed to open stream: A connection attempt
80 * failed because the connected party did not properly respond after a
81 * period of time, or established connection failed because connected host
82 * has failed to respond."
87 * @param array $args Request args, default us an empty array
88 * @return object|null Null if no transports are available, HTTP transport object.
90 function &_getTransport( $args = array() ) {
91 static $working_transport, $blocking_transport, $nonblocking_transport;
93 if ( is_null($working_transport) ) {
94 if ( true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true) ) {
95 $working_transport['exthttp'] = new WP_Http_ExtHttp();
96 $blocking_transport[] = &$working_transport['exthttp'];
97 } else if ( true === WP_Http_Curl::test() && apply_filters('use_curl_transport', true) ) {
98 $working_transport['curl'] = new WP_Http_Curl();
99 $blocking_transport[] = &$working_transport['curl'];
100 } else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) {
101 $working_transport['streams'] = new WP_Http_Streams();
102 $blocking_transport[] = &$working_transport['streams'];
103 } else if ( true === WP_Http_Fopen::test() && apply_filters('use_fopen_transport', true) ) {
104 $working_transport['fopen'] = new WP_Http_Fopen();
105 $blocking_transport[] = &$working_transport['fopen'];
106 } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) ) {
107 $working_transport['fsockopen'] = new WP_Http_Fsockopen();
108 $blocking_transport[] = &$working_transport['fsockopen'];
111 foreach ( array('curl', 'streams', 'fopen', 'fsockopen', 'exthttp') as $transport ) {
112 if ( isset($working_transport[$transport]) )
113 $nonblocking_transport[] = &$working_transport[$transport];
117 if ( isset($args['blocking']) && !$args['blocking'] )
118 return $nonblocking_transport;
120 return $blocking_transport;
124 * Tests the WordPress HTTP objects for an object to use and returns it.
126 * Tests all of the objects and returns the object that passes. Also caches
127 * that object to be used later. This is for posting content to a URL and
128 * is used when there is a body. The plain Fopen Transport can not be used
129 * to send content, but the streams transport can. This is a limitation that
130 * is addressed here, by just not including that transport.
135 * @param array $args Request args, default us an empty array
136 * @return object|null Null if no transports are available, HTTP transport object.
138 function &_postTransport( $args = array() ) {
139 static $working_transport, $blocking_transport, $nonblocking_transport;
141 if ( is_null($working_transport) ) {
142 if ( true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true) ) {
143 $working_transport['exthttp'] = new WP_Http_ExtHttp();
144 $blocking_transport[] = &$working_transport['exthttp'];
145 } else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) {
146 $working_transport['streams'] = new WP_Http_Streams();
147 $blocking_transport[] = &$working_transport['streams'];
148 } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) ) {
149 $working_transport['fsockopen'] = new WP_Http_Fsockopen();
150 $blocking_transport[] = &$working_transport['fsockopen'];
153 foreach ( array('streams', 'fsockopen', 'exthttp') as $transport ) {
154 if ( isset($working_transport[$transport]) )
155 $nonblocking_transport[] = &$working_transport[$transport];
159 if ( isset($args['blocking']) && !$args['blocking'] )
160 return $nonblocking_transport;
162 return $blocking_transport;
166 * Send a HTTP request to a URI.
168 * The body and headers are part of the arguments. The 'body' argument is
169 * for the body and will accept either a string or an array. The 'headers'
170 * argument should be an array, but a string is acceptable. If the 'body'
171 * argument is an array, then it will automatically be escaped using
172 * http_build_query().
174 * The only URI that are supported in the HTTP Transport implementation are
175 * the HTTP and HTTPS protocols. HTTP and HTTPS are assumed so the server
176 * might not know how to handle the send headers. Other protocols are
177 * unsupported and most likely will fail.
179 * The defaults are 'method', 'timeout', 'redirection', 'httpversion',
180 * 'blocking' and 'user-agent'.
182 * Accepted 'method' values are 'GET', 'POST', and 'HEAD', some transports
183 * technically allow others, but should not be assumed. The 'timeout' is
184 * used to sent how long the connection should stay open before failing when
185 * no response. 'redirection' is used to track how many redirects were taken
186 * and used to sent the amount for other transports, but not all transports
187 * accept setting that value.
189 * The 'httpversion' option is used to sent the HTTP version and accepted
190 * values are '1.0', and '1.1' and should be a string. Version 1.1 is not
191 * supported, because of chunk response. The 'user-agent' option is the
192 * user-agent and is used to replace the default user-agent, which is
193 * 'WordPress/WP_Version', where WP_Version is the value from $wp_version.
195 * 'blocking' is the default, which is used to tell the transport, whether
196 * it should halt PHP while it performs the request or continue regardless.
197 * Actually, that isn't entirely correct. Blocking mode really just means
198 * whether the fread should just pull what it can whenever it gets bytes or
199 * if it should wait until it has enough in the buffer to read or finishes
200 * reading the entire content. It doesn't actually always mean that PHP will
201 * continue going after making the request.
206 * @param string $url URI resource.
207 * @param str|array $args Optional. Override the defaults.
210 function request( $url, $args = array() ) {
215 'timeout' => apply_filters( 'http_request_timeout', 5),
216 'redirection' => apply_filters( 'http_request_redirection_count', 5),
217 'httpversion' => apply_filters( 'http_request_version', '1.0'),
218 'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version ),
220 'headers' => array(), 'body' => null
223 $r = wp_parse_args( $args, $defaults );
224 $r = apply_filters( 'http_request_args', $r );
226 if ( is_null( $r['headers'] ) )
227 $r['headers'] = array();
229 if ( ! is_array($r['headers']) ) {
230 $processedHeaders = WP_Http::processHeaders($r['headers']);
231 $r['headers'] = $processedHeaders['headers'];
234 if ( isset($r['headers']['User-Agent']) ) {
235 $r['user-agent'] = $r['headers']['User-Agent'];
236 unset($r['headers']['User-Agent']);
239 if ( isset($r['headers']['user-agent']) ) {
240 $r['user-agent'] = $r['headers']['user-agent'];
241 unset($r['headers']['user-agent']);
244 if ( is_null($r['body']) ) {
245 // Some servers fail when sending content without the content-length
247 $r['headers']['Content-Length'] = 0;
248 $transports = WP_Http::_getTransport($r);
250 if ( is_array( $r['body'] ) || is_object( $r['body'] ) ) {
251 $r['body'] = http_build_query($r['body'], null, '&');
252 $r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
253 $r['headers']['Content-Length'] = strlen($r['body']);
256 if ( ! isset( $r['headers']['Content-Length'] ) && ! isset( $r['headers']['content-length'] ) )
257 $r['headers']['Content-Length'] = strlen($r['body']);
259 $transports = WP_Http::_postTransport($r);
262 $response = array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
263 foreach( (array) $transports as $transport ) {
264 $response = $transport->request($url, $r);
266 if( !is_wp_error($response) )
274 * Uses the POST HTTP method.
276 * Used for sending data that is expected to be in the body.
281 * @param string $url URI resource.
282 * @param str|array $args Optional. Override the defaults.
285 function post($url, $args = array()) {
286 $defaults = array('method' => 'POST');
287 $r = wp_parse_args( $args, $defaults );
288 return $this->request($url, $r);
292 * Uses the GET HTTP method.
294 * Used for sending data that is expected to be in the body.
299 * @param string $url URI resource.
300 * @param str|array $args Optional. Override the defaults.
303 function get($url, $args = array()) {
304 $defaults = array('method' => 'GET');
305 $r = wp_parse_args( $args, $defaults );
306 return $this->request($url, $r);
310 * Uses the HEAD HTTP method.
312 * Used for sending data that is expected to be in the body.
317 * @param string $url URI resource.
318 * @param str|array $args Optional. Override the defaults.
321 function head($url, $args = array()) {
322 $defaults = array('method' => 'HEAD');
323 $r = wp_parse_args( $args, $defaults );
324 return $this->request($url, $r);
328 * Parses the responses and splits the parts into headers and body.
334 * @param string $strResponse The full response string
335 * @return array Array with 'headers' and 'body' keys.
337 function processResponse($strResponse) {
338 list($theHeaders, $theBody) = explode("\r\n\r\n", $strResponse, 2);
339 return array('headers' => $theHeaders, 'body' => $theBody);
343 * Transform header string into an array.
345 * If an array is given then it is assumed to be raw header data with
346 * numeric keys with the headers as the values. No headers must be passed
347 * that were already processed.
353 * @param string|array $headers
354 * @return array Processed string headers
356 function processHeaders($headers) {
357 if ( is_string($headers) )
358 $headers = explode("\n", str_replace(array("\r\n", "\r"), "\n", $headers) );
360 $response = array('code' => 0, 'message' => '');
362 $newheaders = array();
363 foreach ( $headers as $tempheader ) {
364 if ( empty($tempheader) )
367 if ( false === strpos($tempheader, ':') ) {
368 list( , $iResponseCode, $strResponseMsg) = explode(' ', $tempheader, 3);
369 $response['code'] = $iResponseCode;
370 $response['message'] = $strResponseMsg;
374 list($key, $value) = explode(':', $tempheader, 2);
376 if ( ! empty($value) )
377 $newheaders[strtolower($key)] = trim($value);
380 return array('response' => $response, 'headers' => $newheaders);
384 * Decodes chunk transfer-encoding, based off the HTTP 1.1 specification.
386 * Based off the HTTP http_encoding_dechunk function. Does not support
387 * UTF-8. Does not support returning footer headers. Shouldn't be too
388 * difficult to support it though.
390 * @todo Add support for footer chunked headers.
395 * @param string $body Body content
396 * @return string Chunked decoded body on success or raw body on failure.
398 function chunkTransferDecode($body) {
399 $body = str_replace(array("\r\n", "\r"), "\n", $body);
400 // The body is not chunked encoding or is malformed.
401 if ( ! preg_match( '/^[0-9a-f]+(\s|\n)+/mi', trim($body) ) )
405 //$parsedHeaders = array(); Unsupported
408 $hasChunk = (bool) preg_match( '/^([0-9a-f]+)(\s|\n)+/mi', $body, $match );
411 if ( empty($match[1]) )
414 $length = hexdec( $match[1] );
415 $chunkLength = strlen( $match[0] );
417 $strBody = substr($body, $chunkLength, $length);
418 $parsedBody .= $strBody;
420 $body = ltrim(str_replace(array($match[0], $strBody), '', $body), "\n");
422 if( "0" == trim($body) )
423 return $parsedBody; // Ignore footer headers.
432 * HTTP request method uses fsockopen function to retrieve the url.
434 * This would be the preferred method, but the fsockopen implementation has the
435 * most overhead of all the HTTP transport implementations.
441 class WP_Http_Fsockopen {
443 * Send a HTTP request to a URI using fsockopen().
445 * Does not support non-blocking mode.
447 * @see WP_Http::request For default options descriptions.
451 * @param string $url URI resource.
452 * @param str|array $args Optional. Override the defaults.
453 * @return array 'headers', 'body', and 'response' keys.
455 function request($url, $args = array()) {
457 'method' => 'GET', 'timeout' => 5,
458 'redirection' => 5, 'httpversion' => '1.0',
460 'headers' => array(), 'body' => null
463 $r = wp_parse_args( $args, $defaults );
465 if ( isset($r['headers']['User-Agent']) ) {
466 $r['user-agent'] = $r['headers']['User-Agent'];
467 unset($r['headers']['User-Agent']);
468 } else if( isset($r['headers']['user-agent']) ) {
469 $r['user-agent'] = $r['headers']['user-agent'];
470 unset($r['headers']['user-agent']);
473 $iError = null; // Store error number
474 $strError = null; // Store error string
476 $arrURL = parse_url($url);
478 $secure_transport = false;
480 if ( ! isset($arrURL['port']) ) {
481 if ( ($arrURL['scheme'] == 'ssl' || $arrURL['scheme'] == 'https') && extension_loaded('openssl') ) {
482 $arrURL['host'] = 'ssl://' . $arrURL['host'];
483 $arrURL['port'] = apply_filters('http_request_port', 443);
484 $secure_transport = true;
486 $arrURL['port'] = apply_filters('http_request_default_port', 80);
489 $arrURL['port'] = apply_filters('http_request_port', $arrURL['port']);
492 // There are issues with the HTTPS and SSL protocols that cause errors
493 // that can be safely ignored and should be ignored.
494 if ( true === $secure_transport )
495 $error_reporting = error_reporting(0);
497 $startDelay = time();
499 if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) )
500 $handle = @fsockopen($arrURL['host'], $arrURL['port'], $iError, $strError, $r['timeout'] );
502 $handle = fsockopen($arrURL['host'], $arrURL['port'], $iError, $strError, $r['timeout'] );
506 // If the delay is greater than the timeout then fsockopen should't be
507 // used, because it will cause a long delay.
508 $elapseDelay = ($endDelay-$startDelay) > $r['timeout'];
509 if ( true === $elapseDelay )
510 add_option( 'disable_fsockopen', $endDelay, null, true );
512 if ( false === $handle )
513 return new WP_Error('http_request_failed', $iError . ': ' . $strError);
515 // WordPress supports PHP 4.3, which has this function. Removed sanity
516 // checking for performance reasons.
517 stream_set_timeout($handle, $r['timeout'] );
519 $requestPath = $arrURL['path'] . ( isset($arrURL['query']) ? '?' . $arrURL['query'] : '' );
520 $requestPath = empty($requestPath) ? '/' : $requestPath;
523 $strHeaders .= strtoupper($r['method']) . ' ' . $requestPath . ' HTTP/' . $r['httpversion'] . "\r\n";
524 $strHeaders .= 'Host: ' . $arrURL['host'] . "\r\n";
526 if( isset($r['user-agent']) )
527 $strHeaders .= 'User-agent: ' . $r['user-agent'] . "\r\n";
529 if ( is_array($r['headers']) ) {
530 foreach ( (array) $r['headers'] as $header => $headerValue )
531 $strHeaders .= $header . ': ' . $headerValue . "\r\n";
533 $strHeaders .= $r['headers'];
536 $strHeaders .= "\r\n";
538 if ( ! is_null($r['body']) )
539 $strHeaders .= $r['body'];
541 fwrite($handle, $strHeaders);
543 if ( ! $r['blocking'] ) {
545 return array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
549 while ( ! feof($handle) )
550 $strResponse .= fread($handle, 4096);
554 if ( true === $secure_transport )
555 error_reporting($error_reporting);
557 $process = WP_Http::processResponse($strResponse);
558 $arrHeaders = WP_Http::processHeaders($process['headers']);
560 // Is the response code within the 400 range?
561 if ( (int) $arrHeaders['response']['code'] >= 400 && (int) $arrHeaders['response']['code'] < 500 )
562 return new WP_Error('http_request_failed', $arrHeaders['response']['code'] . ': ' . $arrHeaders['response']['message']);
564 // If location is found, then assume redirect and redirect to location.
565 if ( isset($arrHeaders['headers']['location']) ) {
566 if ( $r['redirection']-- > 0 ) {
567 return $this->request($arrHeaders['headers']['location'], $r);
569 return new WP_Error('http_request_failed', __('Too many redirects.'));
573 // If the body was chunk encoded, then decode it.
574 if ( ! empty( $process['body'] ) && isset( $arrHeaders['headers']['transfer-encoding'] ) && 'chunked' == $arrHeaders['headers']['transfer-encoding'] )
575 $process['body'] = WP_Http::chunkTransferDecode($process['body']);
577 return array('headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response']);
581 * Whether this class can be used for retrieving an URL.
585 * @return boolean False means this class can not be used, true means it can.
588 if ( false !== ($option = get_option( 'disable_fsockopen' )) && time()-$option < 43200 ) // 12 hours
591 if ( function_exists( 'fsockopen' ) )
599 * HTTP request method uses fopen function to retrieve the url.
601 * Requires PHP version greater than 4.3.0 for stream support. Does not allow
602 * for $context support, but should still be okay, to write the headers, before
603 * getting the response. Also requires that 'allow_url_fopen' to be enabled.
609 class WP_Http_Fopen {
611 * Send a HTTP request to a URI using fopen().
613 * This transport does not support sending of headers and body, therefore
614 * should not be used in the instances, where there is a body and headers.
616 * Notes: Does not support non-blocking mode. Ignores 'redirection' option.
618 * @see WP_Http::retrieve For default options descriptions.
623 * @param string $url URI resource.
624 * @param str|array $args Optional. Override the defaults.
625 * @return array 'headers', 'body', and 'response' keys.
627 function request($url, $args = array()) {
628 global $http_response_header;
631 'method' => 'GET', 'timeout' => 5,
632 'redirection' => 5, 'httpversion' => '1.0',
634 'headers' => array(), 'body' => null
637 $r = wp_parse_args( $args, $defaults );
639 $arrURL = parse_url($url);
641 if ( false === $arrURL )
642 return new WP_Error('http_request_failed', sprintf(__('Malformed URL: %s'), $url));
644 if ( 'http' != $arrURL['scheme'] && 'https' != $arrURL['scheme'] )
645 $url = str_replace($arrURL['scheme'], 'http', $url);
647 if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) )
648 $handle = @fopen($url, 'r');
650 $handle = fopen($url, 'r');
653 return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url));
655 // WordPress supports PHP 4.3, which has this function. Removed sanity
656 // checking for performance reasons.
657 stream_set_timeout($handle, $r['timeout'] );
659 if ( ! $r['blocking'] ) {
661 return array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
665 while ( ! feof($handle) )
666 $strResponse .= fread($handle, 4096);
669 if ( function_exists('stream_get_meta_data') ) {
670 $meta = stream_get_meta_data($handle);
671 $theHeaders = $meta['wrapper_data'];
672 if( isset( $meta['wrapper_data']['headers'] ) )
673 $theHeaders = $meta['wrapper_data']['headers'];
675 if( ! isset( $http_response_header ) )
676 global $http_response_header;
677 $theHeaders = $http_response_header;
682 $processedHeaders = WP_Http::processHeaders($theHeaders);
684 if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] )
685 $strResponse = WP_Http::chunkTransferDecode($strResponse);
687 return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response']);
691 * Whether this class can be used for retrieving an URL.
694 * @return boolean False means this class can not be used, true means it can.
697 if ( ! function_exists('fopen') || (function_exists('ini_get') && true != ini_get('allow_url_fopen')) )
705 * HTTP request method uses Streams to retrieve the url.
707 * Requires PHP 5.0+ and uses fopen with stream context. Requires that
708 * 'allow_url_fopen' PHP setting to be enabled.
710 * Second preferred method for getting the URL, for PHP 5.
716 class WP_Http_Streams {
718 * Send a HTTP request to a URI using streams with fopen().
724 * @param str|array $args Optional. Override the defaults.
725 * @return array 'headers', 'body', and 'response' keys.
727 function request($url, $args = array()) {
729 'method' => 'GET', 'timeout' => 5,
730 'redirection' => 5, 'httpversion' => '1.0',
732 'headers' => array(), 'body' => null
735 $r = wp_parse_args( $args, $defaults );
737 if ( isset($r['headers']['User-Agent']) ) {
738 $r['user-agent'] = $r['headers']['User-Agent'];
739 unset($r['headers']['User-Agent']);
740 } else if( isset($r['headers']['user-agent']) ) {
741 $r['user-agent'] = $r['headers']['user-agent'];
742 unset($r['headers']['user-agent']);
745 $arrURL = parse_url($url);
747 if ( false === $arrURL )
748 return new WP_Error('http_request_failed', sprintf(__('Malformed URL: %s'), $url));
750 if ( 'http' != $arrURL['scheme'] && 'https' != $arrURL['scheme'] )
751 $url = str_replace($arrURL['scheme'], 'http', $url);
753 // Convert Header array to string.
755 if ( is_array( $r['headers'] ) )
756 foreach( $r['headers'] as $name => $value )
757 $strHeaders .= "{$name}: $value\r\n";
758 else if ( is_string( $r['headers'] ) )
759 $strHeaders = $r['headers'];
761 $arrContext = array('http' =>
763 'method' => strtoupper($r['method']),
764 'user_agent' => $r['user-agent'],
765 'max_redirects' => $r['redirection'],
766 'protocol_version' => (float) $r['httpversion'],
767 'header' => $strHeaders,
768 'timeout' => $r['timeout']
772 if ( ! is_null($r['body']) && ! empty($r['body'] ) )
773 $arrContext['http']['content'] = $r['body'];
775 $context = stream_context_create($arrContext);
777 if ( ! defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) )
778 $handle = @fopen($url, 'r', false, $context);
780 $handle = fopen($url, 'r', false, $context);
783 return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url));
785 // WordPress supports PHP 4.3, which has this function. Removed sanity
786 // checking for performance reasons.
787 stream_set_timeout($handle, $r['timeout'] );
789 if ( ! $r['blocking'] ) {
790 stream_set_blocking($handle, 0);
792 return array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
795 $strResponse = stream_get_contents($handle);
796 $meta = stream_get_meta_data($handle);
798 $processedHeaders = array();
799 if( isset( $meta['wrapper_data']['headers'] ) )
800 $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']['headers']);
802 $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']);
804 if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] )
805 $strResponse = WP_Http::chunkTransferDecode($strResponse);
809 return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response']);
813 * Whether this class can be used for retrieving an URL.
819 * @return boolean False means this class can not be used, true means it can.
822 if ( ! function_exists('fopen') || (function_exists('ini_get') && true != ini_get('allow_url_fopen')) )
825 if ( version_compare(PHP_VERSION, '5.0', '<') )
833 * HTTP request method uses HTTP extension to retrieve the url.
835 * Requires the HTTP extension to be installed. This would be the preferred
836 * transport since it can handle a lot of the problems that forces the others to
837 * use the HTTP version 1.0. Even if PHP 5.2+ is being used, it doesn't mean
838 * that the HTTP extension will be enabled.
844 class WP_Http_ExtHTTP {
846 * Send a HTTP request to a URI using HTTP extension.
848 * Does not support non-blocking.
854 * @param str|array $args Optional. Override the defaults.
855 * @return array 'headers', 'body', and 'response' keys.
857 function request($url, $args = array()) {
859 'method' => 'GET', 'timeout' => 5,
860 'redirection' => 5, 'httpversion' => '1.0',
862 'headers' => array(), 'body' => null
865 $r = wp_parse_args( $args, $defaults );
867 if ( isset($r['headers']['User-Agent']) ) {
868 $r['user-agent'] = $r['headers']['User-Agent'];
869 unset($r['headers']['User-Agent']);
870 } else if( isset($r['headers']['user-agent']) ) {
871 $r['user-agent'] = $r['headers']['user-agent'];
872 unset($r['headers']['user-agent']);
875 switch ( $r['method'] ) {
877 $r['method'] = HTTP_METH_POST;
880 $r['method'] = HTTP_METH_HEAD;
884 $r['method'] = HTTP_METH_GET;
887 $arrURL = parse_url($url);
889 if ( 'http' != $arrURL['scheme'] || 'https' != $arrURL['scheme'] )
890 $url = str_replace($arrURL['scheme'], 'http', $url);
893 'timeout' => $r['timeout'],
894 'connecttimeout' => $r['timeout'],
895 'redirect' => $r['redirection'],
896 'useragent' => $r['user-agent'],
897 'headers' => $r['headers'],
900 if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) ) //Emits warning level notices for max redirects and timeouts
901 $strResponse = @http_request($r['method'], $url, $r['body'], $options, $info);
903 $strResponse = http_request($r['method'], $url, $r['body'], $options, $info); //Emits warning level notices for max redirects and timeouts
905 if ( false === $strResponse || ! empty($info['error']) ) //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
906 return new WP_Error('http_request_failed', $info['response_code'] . ': ' . $info['error']);
908 if ( ! $r['blocking'] )
909 return array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
911 list($theHeaders, $theBody) = explode("\r\n\r\n", $strResponse, 2);
912 $theHeaders = WP_Http::processHeaders($theHeaders);
914 if ( ! empty( $theBody ) && isset( $theHeaders['headers']['transfer-encoding'] ) && 'chunked' == $theHeaders['headers']['transfer-encoding'] ) {
915 if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) )
916 $theBody = @http_chunked_decode($theBody);
918 $theBody = http_chunked_decode($theBody);
921 $theResponse = array();
922 $theResponse['code'] = $info['response_code'];
923 $theResponse['message'] = get_status_header_desc($info['response_code']);
925 return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $theResponse);
929 * Whether this class can be used for retrieving an URL.
934 * @return boolean False means this class can not be used, true means it can.
937 if ( function_exists('http_request') )
945 * HTTP request method uses Curl extension to retrieve the url.
947 * Requires the Curl extension to be installed.
955 * Send a HTTP request to a URI using cURL extension.
961 * @param str|array $args Optional. Override the defaults.
962 * @return array 'headers', 'body', and 'response' keys.
964 function request($url, $args = array()) {
966 'method' => 'GET', 'timeout' => 5,
967 'redirection' => 5, 'httpversion' => '1.0',
969 'headers' => array(), 'body' => null
972 $r = wp_parse_args( $args, $defaults );
974 if ( isset($r['headers']['User-Agent']) ) {
975 $r['user-agent'] = $r['headers']['User-Agent'];
976 unset($r['headers']['User-Agent']);
977 } else if( isset($r['headers']['user-agent']) ) {
978 $r['user-agent'] = $r['headers']['user-agent'];
979 unset($r['headers']['user-agent']);
982 // cURL extension will sometimes fail when the timeout is less than 1 as
983 // it may round down to 0, which gives it unlimited timeout.
984 if ( $r['timeout'] > 0 && $r['timeout'] < 1 )
987 $handle = curl_init();
988 curl_setopt( $handle, CURLOPT_URL, $url);
990 // The cURL extension requires that the option be set for the HEAD to
992 if ( 'HEAD' === $r['method'] ) {
993 curl_setopt( $handle, CURLOPT_NOBODY, true );
996 if ( true === $r['blocking'] ) {
997 curl_setopt( $handle, CURLOPT_HEADER, true );
998 curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 1 );
1000 curl_setopt( $handle, CURLOPT_HEADER, false );
1001 curl_setopt( $handle, CURLOPT_NOBODY, true );
1002 curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 0 );
1005 curl_setopt( $handle, CURLOPT_USERAGENT, $r['user-agent'] );
1006 curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 1 );
1007 curl_setopt( $handle, CURLOPT_TIMEOUT, $r['timeout'] );
1008 curl_setopt( $handle, CURLOPT_MAXREDIRS, $r['redirection'] );
1010 // The option doesn't work with safe mode or when open_basedir is set.
1011 if ( !ini_get('safe_mode') && !ini_get('open_basedir') )
1012 curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, true );
1014 if( ! is_null($r['headers']) )
1015 curl_setopt( $handle, CURLOPT_HTTPHEADER, $r['headers'] );
1017 if ( $r['httpversion'] == '1.0' )
1018 curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
1020 curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
1022 if ( ! $r['blocking'] ) {
1023 curl_exec( $handle );
1024 curl_close( $handle );
1025 return array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
1028 $theResponse = curl_exec( $handle );
1030 if ( !empty($theResponse) ) {
1031 $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
1032 $theHeaders = trim( substr($theResponse, 0, $headerLength) );
1033 $theBody = substr( $theResponse, $headerLength );
1034 if ( false !== strrpos($theHeaders, "\r\n\r\n") ) {
1035 $headerParts = explode("\r\n\r\n", $theHeaders);
1036 $theHeaders = $headerParts[ count($headerParts) -1 ];
1038 $theHeaders = WP_Http::processHeaders($theHeaders);
1040 if ( $curl_error = curl_error($handle) )
1041 return new WP_Error('http_request_failed', $curl_error);
1042 if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array(301, 302) ) )
1043 return new WP_Error('http_request_failed', __('Too many redirects.'));
1045 $theHeaders = array( 'headers' => array() );
1048 $response = array();
1049 $response['code'] = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
1050 $response['message'] = get_status_header_desc($response['code']);
1052 curl_close( $handle );
1054 return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response);
1058 * Whether this class can be used for retrieving an URL.
1063 * @return boolean False means this class can not be used, true means it can.
1066 if ( function_exists('curl_init') && function_exists('curl_exec') )
1074 * Returns the initialized WP_Http Object
1079 * @return WP_Http HTTP Transport object.
1081 function &_wp_http_get_object() {
1084 if ( is_null($http) )
1085 $http = new WP_Http();
1091 * Retrieve the raw response from the HTTP request.
1093 * The array structure is a little complex.
1096 * $res = array( 'headers' => array(), 'response' => array('code', 'message') );
1099 * All of the headers in $res['headers'] are with the name as the key and the
1100 * value as the value. So to get the User-Agent, you would do the following.
1103 * $user_agent = $res['headers']['user-agent'];
1106 * The body is the raw response content and can be retrieved from $res['body'].
1108 * This function is called first to make the request and there are other API
1109 * functions to abstract out the above convoluted setup.
1113 * @param string $url Site URL to retrieve.
1114 * @param array $args Optional. Override the defaults.
1115 * @return string The body of the response
1117 function wp_remote_request($url, $args = array()) {
1118 $objFetchSite = _wp_http_get_object();
1119 return $objFetchSite->request($url, $args);
1123 * Retrieve the raw response from the HTTP request using the GET method.
1125 * @see wp_remote_request() For more information on the response array format.
1129 * @param string $url Site URL to retrieve.
1130 * @param array $args Optional. Override the defaults.
1131 * @return string The body of the response
1133 function wp_remote_get($url, $args = array()) {
1134 $objFetchSite = _wp_http_get_object();
1136 return $objFetchSite->get($url, $args);
1140 * Retrieve the raw response from the HTTP request using the POST method.
1142 * @see wp_remote_request() For more information on the response array format.
1146 * @param string $url Site URL to retrieve.
1147 * @param array $args Optional. Override the defaults.
1148 * @return string The body of the response
1150 function wp_remote_post($url, $args = array()) {
1151 $objFetchSite = _wp_http_get_object();
1152 return $objFetchSite->post($url, $args);
1156 * Retrieve the raw response from the HTTP request using the HEAD method.
1158 * @see wp_remote_request() For more information on the response array format.
1162 * @param string $url Site URL to retrieve.
1163 * @param array $args Optional. Override the defaults.
1164 * @return string The body of the response
1166 function wp_remote_head($url, $args = array()) {
1167 $objFetchSite = _wp_http_get_object();
1168 return $objFetchSite->head($url, $args);
1172 * Retrieve only the headers from the raw response.
1176 * @param array $response HTTP response.
1177 * @return array The headers of the response. Empty array if incorrect parameter given.
1179 function wp_remote_retrieve_headers(&$response) {
1180 if ( ! isset($response['headers']) || ! is_array($response['headers']))
1183 return $response['headers'];
1187 * Retrieve a single header by name from the raw response.
1191 * @param array $response
1192 * @param string $header Header name to retrieve value from.
1193 * @return array The header value. Empty string on if incorrect parameter given.
1195 function wp_remote_retrieve_header(&$response, $header) {
1196 if ( ! isset($response['headers']) || ! is_array($response['headers']))
1199 if ( array_key_exists($header, $response['headers']) )
1200 return $response['headers'][$header];
1206 * Retrieve only the response code from the raw response.
1208 * Will return an empty array if incorrect parameter value is given.
1212 * @param array $response HTTP response.
1213 * @return array The keys 'code' and 'message' give information on the response.
1215 function wp_remote_retrieve_response_code(&$response) {
1216 if ( ! isset($response['response']) || ! is_array($response['response']))
1219 return $response['response']['code'];
1223 * Retrieve only the response message from the raw response.
1225 * Will return an empty array if incorrect parameter value is given.
1229 * @param array $response HTTP response.
1230 * @return array The keys 'code' and 'message' give information on the response.
1232 function wp_remote_retrieve_response_message(&$response) {
1233 if ( ! isset($response['response']) || ! is_array($response['response']))
1236 return $response['response']['message'];
1240 * Retrieve only the body from the raw response.
1244 * @param array $response HTTP response.
1245 * @return string The body of the response. Empty string if no body or incorrect parameter given.
1247 function wp_remote_retrieve_body(&$response) {
1248 if ( ! isset($response['body']) )
1251 return $response['body'];