mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Simple and uniform HTTP request API.
mp-wp_genesis 4 *
mp-wp_genesis 5 * Will eventually replace and standardize the WordPress HTTP requests made.
mp-wp_genesis 6 *
mp-wp_genesis 7 * @link http://trac.wordpress.org/ticket/4779 HTTP API Proposal
mp-wp_genesis 8 *
mp-wp_genesis 9 * @package WordPress
mp-wp_genesis 10 * @subpackage HTTP
mp-wp_genesis 11 * @since 2.7
mp-wp_genesis 12 * @author Jacob Santos <wordpress@santosj.name>
mp-wp_genesis 13 */
mp-wp_genesis 14
mp-wp_genesis 15 /**
mp-wp_genesis 16 * WordPress HTTP Class for managing HTTP Transports and making HTTP requests.
mp-wp_genesis 17 *
mp-wp_genesis 18 * This class is called for the functionality of making HTTP requests and should
mp-wp_genesis 19 * replace Snoopy functionality, eventually. There is no available functionality
mp-wp_genesis 20 * to add HTTP transport implementations, since most of the HTTP transports are
mp-wp_genesis 21 * added and available for use.
mp-wp_genesis 22 *
mp-wp_genesis 23 * The exception is that cURL is not available as a transport and lacking an
mp-wp_genesis 24 * implementation. It will be added later and should be a patch on the WordPress
mp-wp_genesis 25 * Trac.
mp-wp_genesis 26 *
mp-wp_genesis 27 * There are no properties, because none are needed and for performance reasons.
mp-wp_genesis 28 * Some of the functions are static and while they do have some overhead over
mp-wp_genesis 29 * functions in PHP4, the purpose is maintainability. When PHP5 is finally the
mp-wp_genesis 30 * requirement, it will be easy to add the static keyword to the code. It is not
mp-wp_genesis 31 * as easy to convert a function to a method after enough code uses the old way.
mp-wp_genesis 32 *
mp-wp_genesis 33 * @package WordPress
mp-wp_genesis 34 * @subpackage HTTP
mp-wp_genesis 35 * @since 2.7
mp-wp_genesis 36 */
mp-wp_genesis 37 class WP_Http {
mp-wp_genesis 38
mp-wp_genesis 39 /**
mp-wp_genesis 40 * PHP4 style Constructor - Calls PHP5 Style Constructor
mp-wp_genesis 41 *
mp-wp_genesis 42 * @since 2.7
mp-wp_genesis 43 * @return WP_Http
mp-wp_genesis 44 */
mp-wp_genesis 45 function WP_Http() {
mp-wp_genesis 46 $this->__construct();
mp-wp_genesis 47 }
mp-wp_genesis 48
mp-wp_genesis 49 /**
mp-wp_genesis 50 * PHP5 style Constructor - Setup available transport if not available.
mp-wp_genesis 51 *
mp-wp_genesis 52 * PHP4 does not have the 'self' keyword and since WordPress supports PHP4,
mp-wp_genesis 53 * the class needs to be used for the static call.
mp-wp_genesis 54 *
mp-wp_genesis 55 * The transport are setup to save time. This should only be called once, so
mp-wp_genesis 56 * the overhead should be fine.
mp-wp_genesis 57 *
mp-wp_genesis 58 * @since 2.7
mp-wp_genesis 59 * @return WP_Http
mp-wp_genesis 60 */
mp-wp_genesis 61 function __construct() {
mp-wp_genesis 62 WP_Http::_getTransport();
mp-wp_genesis 63 WP_Http::_postTransport();
mp-wp_genesis 64 }
mp-wp_genesis 65
mp-wp_genesis 66 /**
mp-wp_genesis 67 * Tests the WordPress HTTP objects for an object to use and returns it.
mp-wp_genesis 68 *
mp-wp_genesis 69 * Tests all of the objects and returns the object that passes. Also caches
mp-wp_genesis 70 * that object to be used later.
mp-wp_genesis 71 *
mp-wp_genesis 72 * The order for the GET/HEAD requests are Streams, HTTP Extension, Fopen,
mp-wp_genesis 73 * and finally Fsockopen. fsockopen() is used last, because it has the most
mp-wp_genesis 74 * overhead in its implementation. There isn't any real way around it, since
mp-wp_genesis 75 * redirects have to be supported, much the same way the other transports
mp-wp_genesis 76 * also handle redirects.
mp-wp_genesis 77 *
mp-wp_genesis 78 * There are currently issues with "localhost" not resolving correctly with
mp-wp_genesis 79 * DNS. This may cause an error "failed to open stream: A connection attempt
mp-wp_genesis 80 * failed because the connected party did not properly respond after a
mp-wp_genesis 81 * period of time, or established connection failed because connected host
mp-wp_genesis 82 * has failed to respond."
mp-wp_genesis 83 *
mp-wp_genesis 84 * @since 2.7
mp-wp_genesis 85 * @access private
mp-wp_genesis 86 *
mp-wp_genesis 87 * @param array $args Request args, default us an empty array
mp-wp_genesis 88 * @return object|null Null if no transports are available, HTTP transport object.
mp-wp_genesis 89 */
mp-wp_genesis 90 function &_getTransport( $args = array() ) {
mp-wp_genesis 91 static $working_transport, $blocking_transport, $nonblocking_transport;
mp-wp_genesis 92
mp-wp_genesis 93 if ( is_null($working_transport) ) {
mp-wp_genesis 94 if ( true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true) ) {
mp-wp_genesis 95 $working_transport['exthttp'] = new WP_Http_ExtHttp();
mp-wp_genesis 96 $blocking_transport[] = &$working_transport['exthttp'];
mp-wp_genesis 97 } else if ( true === WP_Http_Curl::test() && apply_filters('use_curl_transport', true) ) {
mp-wp_genesis 98 $working_transport['curl'] = new WP_Http_Curl();
mp-wp_genesis 99 $blocking_transport[] = &$working_transport['curl'];
mp-wp_genesis 100 } else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) {
mp-wp_genesis 101 $working_transport['streams'] = new WP_Http_Streams();
mp-wp_genesis 102 $blocking_transport[] = &$working_transport['streams'];
mp-wp_genesis 103 } else if ( true === WP_Http_Fopen::test() && apply_filters('use_fopen_transport', true) ) {
mp-wp_genesis 104 $working_transport['fopen'] = new WP_Http_Fopen();
mp-wp_genesis 105 $blocking_transport[] = &$working_transport['fopen'];
mp-wp_genesis 106 } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) ) {
mp-wp_genesis 107 $working_transport['fsockopen'] = new WP_Http_Fsockopen();
mp-wp_genesis 108 $blocking_transport[] = &$working_transport['fsockopen'];
mp-wp_genesis 109 }
mp-wp_genesis 110
mp-wp_genesis 111 foreach ( array('curl', 'streams', 'fopen', 'fsockopen', 'exthttp') as $transport ) {
mp-wp_genesis 112 if ( isset($working_transport[$transport]) )
mp-wp_genesis 113 $nonblocking_transport[] = &$working_transport[$transport];
mp-wp_genesis 114 }
mp-wp_genesis 115 }
mp-wp_genesis 116
mp-wp_genesis 117 if ( isset($args['blocking']) && !$args['blocking'] )
mp-wp_genesis 118 return $nonblocking_transport;
mp-wp_genesis 119 else
mp-wp_genesis 120 return $blocking_transport;
mp-wp_genesis 121 }
mp-wp_genesis 122
mp-wp_genesis 123 /**
mp-wp_genesis 124 * Tests the WordPress HTTP objects for an object to use and returns it.
mp-wp_genesis 125 *
mp-wp_genesis 126 * Tests all of the objects and returns the object that passes. Also caches
mp-wp_genesis 127 * that object to be used later. This is for posting content to a URL and
mp-wp_genesis 128 * is used when there is a body. The plain Fopen Transport can not be used
mp-wp_genesis 129 * to send content, but the streams transport can. This is a limitation that
mp-wp_genesis 130 * is addressed here, by just not including that transport.
mp-wp_genesis 131 *
mp-wp_genesis 132 * @since 2.7
mp-wp_genesis 133 * @access private
mp-wp_genesis 134 *
mp-wp_genesis 135 * @param array $args Request args, default us an empty array
mp-wp_genesis 136 * @return object|null Null if no transports are available, HTTP transport object.
mp-wp_genesis 137 */
mp-wp_genesis 138 function &_postTransport( $args = array() ) {
mp-wp_genesis 139 static $working_transport, $blocking_transport, $nonblocking_transport;
mp-wp_genesis 140
mp-wp_genesis 141 if ( is_null($working_transport) ) {
mp-wp_genesis 142 if ( true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true) ) {
mp-wp_genesis 143 $working_transport['exthttp'] = new WP_Http_ExtHttp();
mp-wp_genesis 144 $blocking_transport[] = &$working_transport['exthttp'];
mp-wp_genesis 145 } else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) {
mp-wp_genesis 146 $working_transport['streams'] = new WP_Http_Streams();
mp-wp_genesis 147 $blocking_transport[] = &$working_transport['streams'];
mp-wp_genesis 148 } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) ) {
mp-wp_genesis 149 $working_transport['fsockopen'] = new WP_Http_Fsockopen();
mp-wp_genesis 150 $blocking_transport[] = &$working_transport['fsockopen'];
mp-wp_genesis 151 }
mp-wp_genesis 152
mp-wp_genesis 153 foreach ( array('streams', 'fsockopen', 'exthttp') as $transport ) {
mp-wp_genesis 154 if ( isset($working_transport[$transport]) )
mp-wp_genesis 155 $nonblocking_transport[] = &$working_transport[$transport];
mp-wp_genesis 156 }
mp-wp_genesis 157 }
mp-wp_genesis 158
mp-wp_genesis 159 if ( isset($args['blocking']) && !$args['blocking'] )
mp-wp_genesis 160 return $nonblocking_transport;
mp-wp_genesis 161 else
mp-wp_genesis 162 return $blocking_transport;
mp-wp_genesis 163 }
mp-wp_genesis 164
mp-wp_genesis 165 /**
mp-wp_genesis 166 * Send a HTTP request to a URI.
mp-wp_genesis 167 *
mp-wp_genesis 168 * The body and headers are part of the arguments. The 'body' argument is
mp-wp_genesis 169 * for the body and will accept either a string or an array. The 'headers'
mp-wp_genesis 170 * argument should be an array, but a string is acceptable. If the 'body'
mp-wp_genesis 171 * argument is an array, then it will automatically be escaped using
mp-wp_genesis 172 * http_build_query().
mp-wp_genesis 173 *
mp-wp_genesis 174 * The only URI that are supported in the HTTP Transport implementation are
mp-wp_genesis 175 * the HTTP and HTTPS protocols. HTTP and HTTPS are assumed so the server
mp-wp_genesis 176 * might not know how to handle the send headers. Other protocols are
mp-wp_genesis 177 * unsupported and most likely will fail.
mp-wp_genesis 178 *
mp-wp_genesis 179 * The defaults are 'method', 'timeout', 'redirection', 'httpversion',
mp-wp_genesis 180 * 'blocking' and 'user-agent'.
mp-wp_genesis 181 *
mp-wp_genesis 182 * Accepted 'method' values are 'GET', 'POST', and 'HEAD', some transports
mp-wp_genesis 183 * technically allow others, but should not be assumed. The 'timeout' is
mp-wp_genesis 184 * used to sent how long the connection should stay open before failing when
mp-wp_genesis 185 * no response. 'redirection' is used to track how many redirects were taken
mp-wp_genesis 186 * and used to sent the amount for other transports, but not all transports
mp-wp_genesis 187 * accept setting that value.
mp-wp_genesis 188 *
mp-wp_genesis 189 * The 'httpversion' option is used to sent the HTTP version and accepted
mp-wp_genesis 190 * values are '1.0', and '1.1' and should be a string. Version 1.1 is not
mp-wp_genesis 191 * supported, because of chunk response. The 'user-agent' option is the
mp-wp_genesis 192 * user-agent and is used to replace the default user-agent, which is
mp-wp_genesis 193 * 'WordPress/WP_Version', where WP_Version is the value from $wp_version.
mp-wp_genesis 194 *
mp-wp_genesis 195 * 'blocking' is the default, which is used to tell the transport, whether
mp-wp_genesis 196 * it should halt PHP while it performs the request or continue regardless.
mp-wp_genesis 197 * Actually, that isn't entirely correct. Blocking mode really just means
mp-wp_genesis 198 * whether the fread should just pull what it can whenever it gets bytes or
mp-wp_genesis 199 * if it should wait until it has enough in the buffer to read or finishes
mp-wp_genesis 200 * reading the entire content. It doesn't actually always mean that PHP will
mp-wp_genesis 201 * continue going after making the request.
mp-wp_genesis 202 *
mp-wp_genesis 203 * @access public
mp-wp_genesis 204 * @since 2.7
mp-wp_genesis 205 *
mp-wp_genesis 206 * @param string $url URI resource.
mp-wp_genesis 207 * @param str|array $args Optional. Override the defaults.
mp-wp_genesis 208 * @return boolean
mp-wp_genesis 209 */
mp-wp_genesis 210 function request( $url, $args = array() ) {
mp-wp_genesis 211 global $wp_version;
mp-wp_genesis 212
mp-wp_genesis 213 $defaults = array(
mp-wp_genesis 214 'method' => 'GET',
mp-wp_genesis 215 'timeout' => apply_filters( 'http_request_timeout', 5),
mp-wp_genesis 216 'redirection' => apply_filters( 'http_request_redirection_count', 5),
mp-wp_genesis 217 'httpversion' => apply_filters( 'http_request_version', '1.0'),
mp-wp_genesis 218 'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version ),
mp-wp_genesis 219 'blocking' => true,
mp-wp_genesis 220 'headers' => array(), 'body' => null
mp-wp_genesis 221 );
mp-wp_genesis 222
mp-wp_genesis 223 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 224 $r = apply_filters( 'http_request_args', $r );
mp-wp_genesis 225
mp-wp_genesis 226 if ( is_null( $r['headers'] ) )
mp-wp_genesis 227 $r['headers'] = array();
mp-wp_genesis 228
mp-wp_genesis 229 if ( ! is_array($r['headers']) ) {
mp-wp_genesis 230 $processedHeaders = WP_Http::processHeaders($r['headers']);
mp-wp_genesis 231 $r['headers'] = $processedHeaders['headers'];
mp-wp_genesis 232 }
mp-wp_genesis 233
mp-wp_genesis 234 if ( isset($r['headers']['User-Agent']) ) {
mp-wp_genesis 235 $r['user-agent'] = $r['headers']['User-Agent'];
mp-wp_genesis 236 unset($r['headers']['User-Agent']);
mp-wp_genesis 237 }
mp-wp_genesis 238
mp-wp_genesis 239 if ( isset($r['headers']['user-agent']) ) {
mp-wp_genesis 240 $r['user-agent'] = $r['headers']['user-agent'];
mp-wp_genesis 241 unset($r['headers']['user-agent']);
mp-wp_genesis 242 }
mp-wp_genesis 243
mp-wp_genesis 244 if ( is_null($r['body']) ) {
mp-wp_genesis 245 $transports = WP_Http::_getTransport($r);
mp-wp_genesis 246 } else {
mp-wp_genesis 247 if ( is_array( $r['body'] ) || is_object( $r['body'] ) ) {
mp-wp_genesis 248 $r['body'] = http_build_query($r['body'], null, '&');
mp-wp_genesis 249 $r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
mp-wp_genesis 250 $r['headers']['Content-Length'] = strlen($r['body']);
mp-wp_genesis 251 }
mp-wp_genesis 252
mp-wp_genesis 253 if ( ! isset( $r['headers']['Content-Length'] ) && ! isset( $r['headers']['content-length'] ) )
mp-wp_genesis 254 $r['headers']['Content-Length'] = strlen($r['body']);
mp-wp_genesis 255
mp-wp_genesis 256 $transports = WP_Http::_postTransport($r);
mp-wp_genesis 257 }
mp-wp_genesis 258
mp-wp_genesis 259 $response = array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
mp-wp_genesis 260 foreach( (array) $transports as $transport ) {
mp-wp_genesis 261 $response = $transport->request($url, $r);
mp-wp_genesis 262
mp-wp_genesis 263 if( !is_wp_error($response) )
mp-wp_genesis 264 return $response;
mp-wp_genesis 265 }
mp-wp_genesis 266
mp-wp_genesis 267 return $response;
mp-wp_genesis 268 }
mp-wp_genesis 269
mp-wp_genesis 270 /**
mp-wp_genesis 271 * Uses the POST HTTP method.
mp-wp_genesis 272 *
mp-wp_genesis 273 * Used for sending data that is expected to be in the body.
mp-wp_genesis 274 *
mp-wp_genesis 275 * @access public
mp-wp_genesis 276 * @since 2.7
mp-wp_genesis 277 *
mp-wp_genesis 278 * @param string $url URI resource.
mp-wp_genesis 279 * @param str|array $args Optional. Override the defaults.
mp-wp_genesis 280 * @return boolean
mp-wp_genesis 281 */
mp-wp_genesis 282 function post($url, $args = array()) {
mp-wp_genesis 283 $defaults = array('method' => 'POST');
mp-wp_genesis 284 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 285 return $this->request($url, $r);
mp-wp_genesis 286 }
mp-wp_genesis 287
mp-wp_genesis 288 /**
mp-wp_genesis 289 * Uses the GET HTTP method.
mp-wp_genesis 290 *
mp-wp_genesis 291 * Used for sending data that is expected to be in the body.
mp-wp_genesis 292 *
mp-wp_genesis 293 * @access public
mp-wp_genesis 294 * @since 2.7
mp-wp_genesis 295 *
mp-wp_genesis 296 * @param string $url URI resource.
mp-wp_genesis 297 * @param str|array $args Optional. Override the defaults.
mp-wp_genesis 298 * @return boolean
mp-wp_genesis 299 */
mp-wp_genesis 300 function get($url, $args = array()) {
mp-wp_genesis 301 $defaults = array('method' => 'GET');
mp-wp_genesis 302 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 303 return $this->request($url, $r);
mp-wp_genesis 304 }
mp-wp_genesis 305
mp-wp_genesis 306 /**
mp-wp_genesis 307 * Uses the HEAD HTTP method.
mp-wp_genesis 308 *
mp-wp_genesis 309 * Used for sending data that is expected to be in the body.
mp-wp_genesis 310 *
mp-wp_genesis 311 * @access public
mp-wp_genesis 312 * @since 2.7
mp-wp_genesis 313 *
mp-wp_genesis 314 * @param string $url URI resource.
mp-wp_genesis 315 * @param str|array $args Optional. Override the defaults.
mp-wp_genesis 316 * @return boolean
mp-wp_genesis 317 */
mp-wp_genesis 318 function head($url, $args = array()) {
mp-wp_genesis 319 $defaults = array('method' => 'HEAD');
mp-wp_genesis 320 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 321 return $this->request($url, $r);
mp-wp_genesis 322 }
mp-wp_genesis 323
mp-wp_genesis 324 /**
mp-wp_genesis 325 * Parses the responses and splits the parts into headers and body.
mp-wp_genesis 326 *
mp-wp_genesis 327 * @access public
mp-wp_genesis 328 * @static
mp-wp_genesis 329 * @since 2.7
mp-wp_genesis 330 *
mp-wp_genesis 331 * @param string $strResponse The full response string
mp-wp_genesis 332 * @return array Array with 'headers' and 'body' keys.
mp-wp_genesis 333 */
mp-wp_genesis 334 function processResponse($strResponse) {
mp-wp_genesis 335 list($theHeaders, $theBody) = explode("\r\n\r\n", $strResponse, 2);
mp-wp_genesis 336 return array('headers' => $theHeaders, 'body' => $theBody);
mp-wp_genesis 337 }
mp-wp_genesis 338
mp-wp_genesis 339 /**
mp-wp_genesis 340 * Transform header string into an array.
mp-wp_genesis 341 *
mp-wp_genesis 342 * If an array is given then it is assumed to be raw header data with
mp-wp_genesis 343 * numeric keys with the headers as the values. No headers must be passed
mp-wp_genesis 344 * that were already processed.
mp-wp_genesis 345 *
mp-wp_genesis 346 * @access public
mp-wp_genesis 347 * @static
mp-wp_genesis 348 * @since 2.7
mp-wp_genesis 349 *
mp-wp_genesis 350 * @param string|array $headers
mp-wp_genesis 351 * @return array Processed string headers
mp-wp_genesis 352 */
mp-wp_genesis 353 function processHeaders($headers) {
mp-wp_genesis 354 if ( is_string($headers) )
mp-wp_genesis 355 $headers = explode("\n", str_replace(array("\r\n", "\r"), "\n", $headers) );
mp-wp_genesis 356
mp-wp_genesis 357 $response = array('code' => 0, 'message' => '');
mp-wp_genesis 358
mp-wp_genesis 359 $newheaders = array();
mp-wp_genesis 360 foreach ( $headers as $tempheader ) {
mp-wp_genesis 361 if ( empty($tempheader) )
mp-wp_genesis 362 continue;
mp-wp_genesis 363
mp-wp_genesis 364 if ( false === strpos($tempheader, ':') ) {
mp-wp_genesis 365 list( , $iResponseCode, $strResponseMsg) = explode(' ', $tempheader, 3);
mp-wp_genesis 366 $response['code'] = $iResponseCode;
mp-wp_genesis 367 $response['message'] = $strResponseMsg;
mp-wp_genesis 368 continue;
mp-wp_genesis 369 }
mp-wp_genesis 370
mp-wp_genesis 371 list($key, $value) = explode(':', $tempheader, 2);
mp-wp_genesis 372
mp-wp_genesis 373 if ( ! empty($value) )
mp-wp_genesis 374 $newheaders[strtolower($key)] = trim($value);
mp-wp_genesis 375 }
mp-wp_genesis 376
mp-wp_genesis 377 return array('response' => $response, 'headers' => $newheaders);
mp-wp_genesis 378 }
mp-wp_genesis 379
mp-wp_genesis 380 /**
mp-wp_genesis 381 * Decodes chunk transfer-encoding, based off the HTTP 1.1 specification.
mp-wp_genesis 382 *
mp-wp_genesis 383 * Based off the HTTP http_encoding_dechunk function. Does not support
mp-wp_genesis 384 * UTF-8. Does not support returning footer headers. Shouldn't be too
mp-wp_genesis 385 * difficult to support it though.
mp-wp_genesis 386 *
mp-wp_genesis 387 * @todo Add support for footer chunked headers.
mp-wp_genesis 388 * @access public
mp-wp_genesis 389 * @since 2.7
mp-wp_genesis 390 * @static
mp-wp_genesis 391 *
mp-wp_genesis 392 * @param string $body Body content
mp-wp_genesis 393 * @return bool|string|WP_Error False if not chunked encoded. WP_Error on failure. Chunked decoded body on success.
mp-wp_genesis 394 */
mp-wp_genesis 395 function chunkTransferDecode($body) {
mp-wp_genesis 396 $body = str_replace(array("\r\n", "\r"), "\n", $body);
mp-wp_genesis 397 // The body is not chunked encoding or is malformed.
mp-wp_genesis 398 if ( ! preg_match( '/^[0-9a-f]+(\s|\n)+/mi', trim($body) ) )
mp-wp_genesis 399 return $body;
mp-wp_genesis 400
mp-wp_genesis 401 $parsedBody = '';
mp-wp_genesis 402 //$parsedHeaders = array(); Unsupported
mp-wp_genesis 403
mp-wp_genesis 404 $done = false;
mp-wp_genesis 405
mp-wp_genesis 406 do {
mp-wp_genesis 407 $hasChunk = (bool) preg_match( '/^([0-9a-f]+)(\s|\n)+/mi', $body, $match );
mp-wp_genesis 408
mp-wp_genesis 409 if ( $hasChunk ) {
mp-wp_genesis 410 if ( empty($match[1]) ) {
mp-wp_genesis 411 return new WP_Error('http_chunked_decode', __('Does not appear to be chunked encoded or body is malformed.') );
mp-wp_genesis 412 }
mp-wp_genesis 413
mp-wp_genesis 414 $length = hexdec( $match[1] );
mp-wp_genesis 415 $chunkLength = strlen( $match[0] );
mp-wp_genesis 416
mp-wp_genesis 417 $strBody = substr($body, $chunkLength, $length);
mp-wp_genesis 418 $parsedBody .= $strBody;
mp-wp_genesis 419
mp-wp_genesis 420 $body = ltrim(str_replace(array($match[0], $strBody), '', $body), "\n");
mp-wp_genesis 421
mp-wp_genesis 422 if( "0" == trim($body) ) {
mp-wp_genesis 423 $done = true;
mp-wp_genesis 424 return $parsedBody; // Ignore footer headers.
mp-wp_genesis 425 break;
mp-wp_genesis 426 }
mp-wp_genesis 427 } else {
mp-wp_genesis 428 return new WP_Error('http_chunked_decode', __('Does not appear to be chunked encoded or body is malformed.') );
mp-wp_genesis 429 }
mp-wp_genesis 430 } while ( false === $done );
mp-wp_genesis 431 }
mp-wp_genesis 432 }
mp-wp_genesis 433
mp-wp_genesis 434 /**
mp-wp_genesis 435 * HTTP request method uses fsockopen function to retrieve the url.
mp-wp_genesis 436 *
mp-wp_genesis 437 * This would be the preferred method, but the fsockopen implementation has the
mp-wp_genesis 438 * most overhead of all the HTTP transport implementations.
mp-wp_genesis 439 *
mp-wp_genesis 440 * @package WordPress
mp-wp_genesis 441 * @subpackage HTTP
mp-wp_genesis 442 * @since 2.7
mp-wp_genesis 443 */
mp-wp_genesis 444 class WP_Http_Fsockopen {
mp-wp_genesis 445 /**
mp-wp_genesis 446 * Send a HTTP request to a URI using fsockopen().
mp-wp_genesis 447 *
mp-wp_genesis 448 * Does not support non-blocking mode.
mp-wp_genesis 449 *
mp-wp_genesis 450 * @see WP_Http::request For default options descriptions.
mp-wp_genesis 451 *
mp-wp_genesis 452 * @since 2.7
mp-wp_genesis 453 * @access public
mp-wp_genesis 454 * @param string $url URI resource.
mp-wp_genesis 455 * @param str|array $args Optional. Override the defaults.
mp-wp_genesis 456 * @return array 'headers', 'body', and 'response' keys.
mp-wp_genesis 457 */
mp-wp_genesis 458 function request($url, $args = array()) {
mp-wp_genesis 459 $defaults = array(
mp-wp_genesis 460 'method' => 'GET', 'timeout' => 5,
mp-wp_genesis 461 'redirection' => 5, 'httpversion' => '1.0',
mp-wp_genesis 462 'blocking' => true,
mp-wp_genesis 463 'headers' => array(), 'body' => null
mp-wp_genesis 464 );
mp-wp_genesis 465
mp-wp_genesis 466 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 467
mp-wp_genesis 468 if ( isset($r['headers']['User-Agent']) ) {
mp-wp_genesis 469 $r['user-agent'] = $r['headers']['User-Agent'];
mp-wp_genesis 470 unset($r['headers']['User-Agent']);
mp-wp_genesis 471 } else if( isset($r['headers']['user-agent']) ) {
mp-wp_genesis 472 $r['user-agent'] = $r['headers']['user-agent'];
mp-wp_genesis 473 unset($r['headers']['user-agent']);
mp-wp_genesis 474 }
mp-wp_genesis 475
mp-wp_genesis 476 $iError = null; // Store error number
mp-wp_genesis 477 $strError = null; // Store error string
mp-wp_genesis 478
mp-wp_genesis 479 $arrURL = parse_url($url);
mp-wp_genesis 480
mp-wp_genesis 481 $secure_transport = false;
mp-wp_genesis 482
mp-wp_genesis 483 if ( ! isset($arrURL['port']) ) {
mp-wp_genesis 484 if ( ($arrURL['scheme'] == 'ssl' || $arrURL['scheme'] == 'https') && extension_loaded('openssl') ) {
mp-wp_genesis 485 $arrURL['host'] = 'ssl://' . $arrURL['host'];
mp-wp_genesis 486 $arrURL['port'] = apply_filters('http_request_port', 443);
mp-wp_genesis 487 $secure_transport = true;
mp-wp_genesis 488 } else {
mp-wp_genesis 489 $arrURL['port'] = apply_filters('http_request_default_port', 80);
mp-wp_genesis 490 }
mp-wp_genesis 491 } else {
mp-wp_genesis 492 $arrURL['port'] = apply_filters('http_request_port', $arrURL['port']);
mp-wp_genesis 493 }
mp-wp_genesis 494
mp-wp_genesis 495 // There are issues with the HTTPS and SSL protocols that cause errors
mp-wp_genesis 496 // that can be safely ignored and should be ignored.
mp-wp_genesis 497 if ( true === $secure_transport )
mp-wp_genesis 498 $error_reporting = error_reporting(0);
mp-wp_genesis 499
mp-wp_genesis 500 $startDelay = time();
mp-wp_genesis 501
mp-wp_genesis 502 if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) )
mp-wp_genesis 503 $handle = @fsockopen($arrURL['host'], $arrURL['port'], $iError, $strError, $r['timeout'] );
mp-wp_genesis 504 else
mp-wp_genesis 505 $handle = fsockopen($arrURL['host'], $arrURL['port'], $iError, $strError, $r['timeout'] );
mp-wp_genesis 506
mp-wp_genesis 507 $endDelay = time();
mp-wp_genesis 508
mp-wp_genesis 509 // If the delay is greater than the timeout then fsockopen should't be
mp-wp_genesis 510 // used, because it will cause a long delay.
mp-wp_genesis 511 $elapseDelay = ($endDelay-$startDelay) > $r['timeout'];
mp-wp_genesis 512 if ( true === $elapseDelay )
mp-wp_genesis 513 add_option( 'disable_fsockopen', $endDelay, null, true );
mp-wp_genesis 514
mp-wp_genesis 515 if ( false === $handle )
mp-wp_genesis 516 return new WP_Error('http_request_failed', $iError . ': ' . $strError);
mp-wp_genesis 517
mp-wp_genesis 518 // WordPress supports PHP 4.3, which has this function. Removed sanity
mp-wp_genesis 519 // checking for performance reasons.
mp-wp_genesis 520 stream_set_timeout($handle, $r['timeout'] );
mp-wp_genesis 521
mp-wp_genesis 522 $requestPath = $arrURL['path'] . ( isset($arrURL['query']) ? '?' . $arrURL['query'] : '' );
mp-wp_genesis 523 $requestPath = empty($requestPath) ? '/' : $requestPath;
mp-wp_genesis 524
mp-wp_genesis 525 $strHeaders = '';
mp-wp_genesis 526 $strHeaders .= strtoupper($r['method']) . ' ' . $requestPath . ' HTTP/' . $r['httpversion'] . "\r\n";
mp-wp_genesis 527 $strHeaders .= 'Host: ' . $arrURL['host'] . "\r\n";
mp-wp_genesis 528
mp-wp_genesis 529 if( isset($r['user-agent']) )
mp-wp_genesis 530 $strHeaders .= 'User-agent: ' . $r['user-agent'] . "\r\n";
mp-wp_genesis 531
mp-wp_genesis 532 if ( is_array($r['headers']) ) {
mp-wp_genesis 533 foreach ( (array) $r['headers'] as $header => $headerValue )
mp-wp_genesis 534 $strHeaders .= $header . ': ' . $headerValue . "\r\n";
mp-wp_genesis 535 } else {
mp-wp_genesis 536 $strHeaders .= $r['headers'];
mp-wp_genesis 537 }
mp-wp_genesis 538
mp-wp_genesis 539 $strHeaders .= "\r\n";
mp-wp_genesis 540
mp-wp_genesis 541 if ( ! is_null($r['body']) )
mp-wp_genesis 542 $strHeaders .= $r['body'];
mp-wp_genesis 543
mp-wp_genesis 544 fwrite($handle, $strHeaders);
mp-wp_genesis 545
mp-wp_genesis 546 if ( ! $r['blocking'] ) {
mp-wp_genesis 547 fclose($handle);
mp-wp_genesis 548 return array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
mp-wp_genesis 549 }
mp-wp_genesis 550
mp-wp_genesis 551 $strResponse = '';
mp-wp_genesis 552 while ( ! feof($handle) )
mp-wp_genesis 553 $strResponse .= fread($handle, 4096);
mp-wp_genesis 554
mp-wp_genesis 555 fclose($handle);
mp-wp_genesis 556
mp-wp_genesis 557 if ( true === $secure_transport )
mp-wp_genesis 558 error_reporting($error_reporting);
mp-wp_genesis 559
mp-wp_genesis 560 $process = WP_Http::processResponse($strResponse);
mp-wp_genesis 561 $arrHeaders = WP_Http::processHeaders($process['headers']);
mp-wp_genesis 562
mp-wp_genesis 563 // Is the response code within the 400 range?
mp-wp_genesis 564 if ( (int) $arrHeaders['response']['code'] >= 400 && (int) $arrHeaders['response']['code'] < 500 )
mp-wp_genesis 565 return new WP_Error('http_request_failed', $arrHeaders['response']['code'] . ': ' . $arrHeaders['response']['message']);
mp-wp_genesis 566
mp-wp_genesis 567 // If location is found, then assume redirect and redirect to location.
mp-wp_genesis 568 if ( isset($arrHeaders['headers']['location']) ) {
mp-wp_genesis 569 if ( $r['redirection']-- > 0 ) {
mp-wp_genesis 570 return $this->request($arrHeaders['headers']['location'], $r);
mp-wp_genesis 571 } else {
mp-wp_genesis 572 return new WP_Error('http_request_failed', __('Too many redirects.'));
mp-wp_genesis 573 }
mp-wp_genesis 574 }
mp-wp_genesis 575
mp-wp_genesis 576 // If the body was chunk encoded, then decode it.
mp-wp_genesis 577 if ( ! empty( $process['body'] ) && isset( $arrHeaders['headers']['transfer-encoding'] ) && 'chunked' == $arrHeaders['headers']['transfer-encoding'] )
mp-wp_genesis 578 $process['body'] = WP_Http::chunkTransferDecode($process['body']);
mp-wp_genesis 579
mp-wp_genesis 580 return array('headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response']);
mp-wp_genesis 581 }
mp-wp_genesis 582
mp-wp_genesis 583 /**
mp-wp_genesis 584 * Whether this class can be used for retrieving an URL.
mp-wp_genesis 585 *
mp-wp_genesis 586 * @since 2.7
mp-wp_genesis 587 * @static
mp-wp_genesis 588 * @return boolean False means this class can not be used, true means it can.
mp-wp_genesis 589 */
mp-wp_genesis 590 function test() {
mp-wp_genesis 591 if ( false !== ($option = get_option( 'disable_fsockopen' )) && time()-$option < 43200 ) // 12 hours
mp-wp_genesis 592 return false;
mp-wp_genesis 593
mp-wp_genesis 594 if ( function_exists( 'fsockopen' ) )
mp-wp_genesis 595 return true;
mp-wp_genesis 596
mp-wp_genesis 597 return false;
mp-wp_genesis 598 }
mp-wp_genesis 599 }
mp-wp_genesis 600
mp-wp_genesis 601 /**
mp-wp_genesis 602 * HTTP request method uses fopen function to retrieve the url.
mp-wp_genesis 603 *
mp-wp_genesis 604 * Requires PHP version greater than 4.3.0 for stream support. Does not allow
mp-wp_genesis 605 * for $context support, but should still be okay, to write the headers, before
mp-wp_genesis 606 * getting the response. Also requires that 'allow_url_fopen' to be enabled.
mp-wp_genesis 607 *
mp-wp_genesis 608 * @package WordPress
mp-wp_genesis 609 * @subpackage HTTP
mp-wp_genesis 610 * @since 2.7
mp-wp_genesis 611 */
mp-wp_genesis 612 class WP_Http_Fopen {
mp-wp_genesis 613 /**
mp-wp_genesis 614 * Send a HTTP request to a URI using fopen().
mp-wp_genesis 615 *
mp-wp_genesis 616 * This transport does not support sending of headers and body, therefore
mp-wp_genesis 617 * should not be used in the instances, where there is a body and headers.
mp-wp_genesis 618 *
mp-wp_genesis 619 * Notes: Does not support non-blocking mode. Ignores 'redirection' option.
mp-wp_genesis 620 *
mp-wp_genesis 621 * @see WP_Http::retrieve For default options descriptions.
mp-wp_genesis 622 *
mp-wp_genesis 623 * @access public
mp-wp_genesis 624 * @since 2.7
mp-wp_genesis 625 *
mp-wp_genesis 626 * @param string $url URI resource.
mp-wp_genesis 627 * @param str|array $args Optional. Override the defaults.
mp-wp_genesis 628 * @return array 'headers', 'body', and 'response' keys.
mp-wp_genesis 629 */
mp-wp_genesis 630 function request($url, $args = array()) {
mp-wp_genesis 631 global $http_response_header;
mp-wp_genesis 632
mp-wp_genesis 633 $defaults = array(
mp-wp_genesis 634 'method' => 'GET', 'timeout' => 5,
mp-wp_genesis 635 'redirection' => 5, 'httpversion' => '1.0',
mp-wp_genesis 636 'blocking' => true,
mp-wp_genesis 637 'headers' => array(), 'body' => null
mp-wp_genesis 638 );
mp-wp_genesis 639
mp-wp_genesis 640 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 641
mp-wp_genesis 642 $arrURL = parse_url($url);
mp-wp_genesis 643
mp-wp_genesis 644 if ( false === $arrURL )
mp-wp_genesis 645 return new WP_Error('http_request_failed', sprintf(__('Malformed URL: %s'), $url));
mp-wp_genesis 646
mp-wp_genesis 647 if ( 'http' != $arrURL['scheme'] || 'https' != $arrURL['scheme'] )
mp-wp_genesis 648 $url = str_replace($arrURL['scheme'], 'http', $url);
mp-wp_genesis 649
mp-wp_genesis 650 if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) )
mp-wp_genesis 651 $handle = @fopen($url, 'r');
mp-wp_genesis 652 else
mp-wp_genesis 653 $handle = fopen($url, 'r');
mp-wp_genesis 654
mp-wp_genesis 655 if (! $handle)
mp-wp_genesis 656 return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url));
mp-wp_genesis 657
mp-wp_genesis 658 // WordPress supports PHP 4.3, which has this function. Removed sanity
mp-wp_genesis 659 // checking for performance reasons.
mp-wp_genesis 660 stream_set_timeout($handle, $r['timeout'] );
mp-wp_genesis 661
mp-wp_genesis 662 if ( ! $r['blocking'] ) {
mp-wp_genesis 663 fclose($handle);
mp-wp_genesis 664 return array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
mp-wp_genesis 665 }
mp-wp_genesis 666
mp-wp_genesis 667 $strResponse = '';
mp-wp_genesis 668 while ( ! feof($handle) )
mp-wp_genesis 669 $strResponse .= fread($handle, 4096);
mp-wp_genesis 670
mp-wp_genesis 671 $theHeaders = '';
mp-wp_genesis 672 if ( function_exists('stream_get_meta_data') ) {
mp-wp_genesis 673 $meta = stream_get_meta_data($handle);
mp-wp_genesis 674 $theHeaders = $meta['wrapper_data'];
mp-wp_genesis 675 if( isset( $meta['wrapper_data']['headers'] ) )
mp-wp_genesis 676 $theHeaders = $meta['wrapper_data']['headers'];
mp-wp_genesis 677 } else {
mp-wp_genesis 678 if( ! isset( $http_response_header ) )
mp-wp_genesis 679 global $http_response_header;
mp-wp_genesis 680 $theHeaders = $http_response_header;
mp-wp_genesis 681 }
mp-wp_genesis 682
mp-wp_genesis 683 fclose($handle);
mp-wp_genesis 684
mp-wp_genesis 685 $processedHeaders = WP_Http::processHeaders($theHeaders);
mp-wp_genesis 686
mp-wp_genesis 687 if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] )
mp-wp_genesis 688 $strResponse = WP_Http::chunkTransferDecode($strResponse);
mp-wp_genesis 689
mp-wp_genesis 690 return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response']);
mp-wp_genesis 691 }
mp-wp_genesis 692
mp-wp_genesis 693 /**
mp-wp_genesis 694 * Whether this class can be used for retrieving an URL.
mp-wp_genesis 695 *
mp-wp_genesis 696 * @static
mp-wp_genesis 697 * @return boolean False means this class can not be used, true means it can.
mp-wp_genesis 698 */
mp-wp_genesis 699 function test() {
mp-wp_genesis 700 if ( ! function_exists('fopen') || (function_exists('ini_get') && true != ini_get('allow_url_fopen')) )
mp-wp_genesis 701 return false;
mp-wp_genesis 702
mp-wp_genesis 703 return true;
mp-wp_genesis 704 }
mp-wp_genesis 705 }
mp-wp_genesis 706
mp-wp_genesis 707 /**
mp-wp_genesis 708 * HTTP request method uses Streams to retrieve the url.
mp-wp_genesis 709 *
mp-wp_genesis 710 * Requires PHP 5.0+ and uses fopen with stream context. Requires that
mp-wp_genesis 711 * 'allow_url_fopen' PHP setting to be enabled.
mp-wp_genesis 712 *
mp-wp_genesis 713 * Second preferred method for getting the URL, for PHP 5.
mp-wp_genesis 714 *
mp-wp_genesis 715 * @package WordPress
mp-wp_genesis 716 * @subpackage HTTP
mp-wp_genesis 717 * @since 2.7
mp-wp_genesis 718 */
mp-wp_genesis 719 class WP_Http_Streams {
mp-wp_genesis 720 /**
mp-wp_genesis 721 * Send a HTTP request to a URI using streams with fopen().
mp-wp_genesis 722 *
mp-wp_genesis 723 * @access public
mp-wp_genesis 724 * @since 2.7
mp-wp_genesis 725 *
mp-wp_genesis 726 * @param string $url
mp-wp_genesis 727 * @param str|array $args Optional. Override the defaults.
mp-wp_genesis 728 * @return array 'headers', 'body', and 'response' keys.
mp-wp_genesis 729 */
mp-wp_genesis 730 function request($url, $args = array()) {
mp-wp_genesis 731 $defaults = array(
mp-wp_genesis 732 'method' => 'GET', 'timeout' => 5,
mp-wp_genesis 733 'redirection' => 5, 'httpversion' => '1.0',
mp-wp_genesis 734 'blocking' => true,
mp-wp_genesis 735 'headers' => array(), 'body' => null
mp-wp_genesis 736 );
mp-wp_genesis 737
mp-wp_genesis 738 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 739
mp-wp_genesis 740 if ( isset($r['headers']['User-Agent']) ) {
mp-wp_genesis 741 $r['user-agent'] = $r['headers']['User-Agent'];
mp-wp_genesis 742 unset($r['headers']['User-Agent']);
mp-wp_genesis 743 } else if( isset($r['headers']['user-agent']) ) {
mp-wp_genesis 744 $r['user-agent'] = $r['headers']['user-agent'];
mp-wp_genesis 745 unset($r['headers']['user-agent']);
mp-wp_genesis 746 }
mp-wp_genesis 747
mp-wp_genesis 748 $arrURL = parse_url($url);
mp-wp_genesis 749
mp-wp_genesis 750 if ( false === $arrURL )
mp-wp_genesis 751 return new WP_Error('http_request_failed', sprintf(__('Malformed URL: %s'), $url));
mp-wp_genesis 752
mp-wp_genesis 753 if ( 'http' != $arrURL['scheme'] || 'https' != $arrURL['scheme'] )
mp-wp_genesis 754 $url = str_replace($arrURL['scheme'], 'http', $url);
mp-wp_genesis 755
mp-wp_genesis 756 // Convert Header array to string.
mp-wp_genesis 757 $strHeaders = '';
mp-wp_genesis 758 if ( is_array( $r['headers'] ) )
mp-wp_genesis 759 foreach( $r['headers'] as $name => $value )
mp-wp_genesis 760 $strHeaders .= "{$name}: $value\r\n";
mp-wp_genesis 761 else if ( is_string( $r['headers'] ) )
mp-wp_genesis 762 $strHeaders = $r['headers'];
mp-wp_genesis 763
mp-wp_genesis 764 $arrContext = array('http' =>
mp-wp_genesis 765 array(
mp-wp_genesis 766 'method' => strtoupper($r['method']),
mp-wp_genesis 767 'user_agent' => $r['user-agent'],
mp-wp_genesis 768 'max_redirects' => $r['redirection'],
mp-wp_genesis 769 'protocol_version' => (float) $r['httpversion'],
mp-wp_genesis 770 'header' => $strHeaders,
mp-wp_genesis 771 'timeout' => $r['timeout']
mp-wp_genesis 772 )
mp-wp_genesis 773 );
mp-wp_genesis 774
mp-wp_genesis 775 if ( ! is_null($r['body']) && ! empty($r['body'] ) )
mp-wp_genesis 776 $arrContext['http']['content'] = $r['body'];
mp-wp_genesis 777
mp-wp_genesis 778 $context = stream_context_create($arrContext);
mp-wp_genesis 779
mp-wp_genesis 780 if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) )
mp-wp_genesis 781 $handle = @fopen($url, 'r', false, $context);
mp-wp_genesis 782 else
mp-wp_genesis 783 $handle = fopen($url, 'r', false, $context);
mp-wp_genesis 784
mp-wp_genesis 785 if ( ! $handle)
mp-wp_genesis 786 return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url));
mp-wp_genesis 787
mp-wp_genesis 788 // WordPress supports PHP 4.3, which has this function. Removed sanity
mp-wp_genesis 789 // checking for performance reasons.
mp-wp_genesis 790 stream_set_timeout($handle, $r['timeout'] );
mp-wp_genesis 791
mp-wp_genesis 792 if ( ! $r['blocking'] ) {
mp-wp_genesis 793 stream_set_blocking($handle, 0);
mp-wp_genesis 794 fclose($handle);
mp-wp_genesis 795 return array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
mp-wp_genesis 796 }
mp-wp_genesis 797
mp-wp_genesis 798 $strResponse = stream_get_contents($handle);
mp-wp_genesis 799 $meta = stream_get_meta_data($handle);
mp-wp_genesis 800
mp-wp_genesis 801 $processedHeaders = array();
mp-wp_genesis 802 if( isset( $meta['wrapper_data']['headers'] ) )
mp-wp_genesis 803 $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']['headers']);
mp-wp_genesis 804 else
mp-wp_genesis 805 $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']);
mp-wp_genesis 806
mp-wp_genesis 807 if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] )
mp-wp_genesis 808 $strResponse = WP_Http::chunkTransferDecode($strResponse);
mp-wp_genesis 809
mp-wp_genesis 810 fclose($handle);
mp-wp_genesis 811
mp-wp_genesis 812 return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response']);
mp-wp_genesis 813 }
mp-wp_genesis 814
mp-wp_genesis 815 /**
mp-wp_genesis 816 * Whether this class can be used for retrieving an URL.
mp-wp_genesis 817 *
mp-wp_genesis 818 * @static
mp-wp_genesis 819 * @access public
mp-wp_genesis 820 * @since 2.7
mp-wp_genesis 821 *
mp-wp_genesis 822 * @return boolean False means this class can not be used, true means it can.
mp-wp_genesis 823 */
mp-wp_genesis 824 function test() {
mp-wp_genesis 825 if ( ! function_exists('fopen') || (function_exists('ini_get') && true != ini_get('allow_url_fopen')) )
mp-wp_genesis 826 return false;
mp-wp_genesis 827
mp-wp_genesis 828 if ( version_compare(PHP_VERSION, '5.0', '<') )
mp-wp_genesis 829 return false;
mp-wp_genesis 830
mp-wp_genesis 831 return true;
mp-wp_genesis 832 }
mp-wp_genesis 833 }
mp-wp_genesis 834
mp-wp_genesis 835 /**
mp-wp_genesis 836 * HTTP request method uses HTTP extension to retrieve the url.
mp-wp_genesis 837 *
mp-wp_genesis 838 * Requires the HTTP extension to be installed. This would be the preferred
mp-wp_genesis 839 * transport since it can handle a lot of the problems that forces the others to
mp-wp_genesis 840 * use the HTTP version 1.0. Even if PHP 5.2+ is being used, it doesn't mean
mp-wp_genesis 841 * that the HTTP extension will be enabled.
mp-wp_genesis 842 *
mp-wp_genesis 843 * @package WordPress
mp-wp_genesis 844 * @subpackage HTTP
mp-wp_genesis 845 * @since 2.7
mp-wp_genesis 846 */
mp-wp_genesis 847 class WP_Http_ExtHTTP {
mp-wp_genesis 848 /**
mp-wp_genesis 849 * Send a HTTP request to a URI using HTTP extension.
mp-wp_genesis 850 *
mp-wp_genesis 851 * Does not support non-blocking.
mp-wp_genesis 852 *
mp-wp_genesis 853 * @access public
mp-wp_genesis 854 * @since 2.7
mp-wp_genesis 855 *
mp-wp_genesis 856 * @param string $url
mp-wp_genesis 857 * @param str|array $args Optional. Override the defaults.
mp-wp_genesis 858 * @return array 'headers', 'body', and 'response' keys.
mp-wp_genesis 859 */
mp-wp_genesis 860 function request($url, $args = array()) {
mp-wp_genesis 861 $defaults = array(
mp-wp_genesis 862 'method' => 'GET', 'timeout' => 5,
mp-wp_genesis 863 'redirection' => 5, 'httpversion' => '1.0',
mp-wp_genesis 864 'blocking' => true,
mp-wp_genesis 865 'headers' => array(), 'body' => null
mp-wp_genesis 866 );
mp-wp_genesis 867
mp-wp_genesis 868 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 869
mp-wp_genesis 870 if ( isset($r['headers']['User-Agent']) ) {
mp-wp_genesis 871 $r['user-agent'] = $r['headers']['User-Agent'];
mp-wp_genesis 872 unset($r['headers']['User-Agent']);
mp-wp_genesis 873 } else if( isset($r['headers']['user-agent']) ) {
mp-wp_genesis 874 $r['user-agent'] = $r['headers']['user-agent'];
mp-wp_genesis 875 unset($r['headers']['user-agent']);
mp-wp_genesis 876 }
mp-wp_genesis 877
mp-wp_genesis 878 switch ( $r['method'] ) {
mp-wp_genesis 879 case 'POST':
mp-wp_genesis 880 $r['method'] = HTTP_METH_POST;
mp-wp_genesis 881 break;
mp-wp_genesis 882 case 'HEAD':
mp-wp_genesis 883 $r['method'] = HTTP_METH_HEAD;
mp-wp_genesis 884 break;
mp-wp_genesis 885 case 'GET':
mp-wp_genesis 886 default:
mp-wp_genesis 887 $r['method'] = HTTP_METH_GET;
mp-wp_genesis 888 }
mp-wp_genesis 889
mp-wp_genesis 890 $arrURL = parse_url($url);
mp-wp_genesis 891
mp-wp_genesis 892 if ( 'http' != $arrURL['scheme'] || 'https' != $arrURL['scheme'] )
mp-wp_genesis 893 $url = str_replace($arrURL['scheme'], 'http', $url);
mp-wp_genesis 894
mp-wp_genesis 895 $options = array(
mp-wp_genesis 896 'timeout' => $r['timeout'],
mp-wp_genesis 897 'connecttimeout' => $r['timeout'],
mp-wp_genesis 898 'redirect' => $r['redirection'],
mp-wp_genesis 899 'useragent' => $r['user-agent'],
mp-wp_genesis 900 'headers' => $r['headers'],
mp-wp_genesis 901 );
mp-wp_genesis 902
mp-wp_genesis 903 if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) ) //Emits warning level notices for max redirects and timeouts
mp-wp_genesis 904 $strResponse = @http_request($r['method'], $url, $r['body'], $options, $info);
mp-wp_genesis 905 else
mp-wp_genesis 906 $strResponse = http_request($r['method'], $url, $r['body'], $options, $info); //Emits warning level notices for max redirects and timeouts
mp-wp_genesis 907
mp-wp_genesis 908 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
mp-wp_genesis 909 return new WP_Error('http_request_failed', $info['response_code'] . ': ' . $info['error']);
mp-wp_genesis 910
mp-wp_genesis 911 if ( ! $r['blocking'] )
mp-wp_genesis 912 return array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
mp-wp_genesis 913
mp-wp_genesis 914 list($theHeaders, $theBody) = explode("\r\n\r\n", $strResponse, 2);
mp-wp_genesis 915 $theHeaders = WP_Http::processHeaders($theHeaders);
mp-wp_genesis 916
mp-wp_genesis 917 if ( ! empty( $theBody ) && isset( $theHeaders['headers']['transfer-encoding'] ) && 'chunked' == $theHeaders['headers']['transfer-encoding'] ) {
mp-wp_genesis 918 if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) )
mp-wp_genesis 919 $theBody = @http_chunked_decode($theBody);
mp-wp_genesis 920 else
mp-wp_genesis 921 $theBody = http_chunked_decode($theBody);
mp-wp_genesis 922 }
mp-wp_genesis 923
mp-wp_genesis 924 $theResponse = array();
mp-wp_genesis 925 $theResponse['code'] = $info['response_code'];
mp-wp_genesis 926 $theResponse['message'] = get_status_header_desc($info['response_code']);
mp-wp_genesis 927
mp-wp_genesis 928 return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $theResponse);
mp-wp_genesis 929 }
mp-wp_genesis 930
mp-wp_genesis 931 /**
mp-wp_genesis 932 * Whether this class can be used for retrieving an URL.
mp-wp_genesis 933 *
mp-wp_genesis 934 * @static
mp-wp_genesis 935 * @since 2.7
mp-wp_genesis 936 *
mp-wp_genesis 937 * @return boolean False means this class can not be used, true means it can.
mp-wp_genesis 938 */
mp-wp_genesis 939 function test() {
mp-wp_genesis 940 if ( function_exists('http_request') )
mp-wp_genesis 941 return true;
mp-wp_genesis 942
mp-wp_genesis 943 return false;
mp-wp_genesis 944 }
mp-wp_genesis 945 }
mp-wp_genesis 946
mp-wp_genesis 947 /**
mp-wp_genesis 948 * HTTP request method uses Curl extension to retrieve the url.
mp-wp_genesis 949 *
mp-wp_genesis 950 * Requires the Curl extension to be installed.
mp-wp_genesis 951 *
mp-wp_genesis 952 * @package WordPress
mp-wp_genesis 953 * @subpackage HTTP
mp-wp_genesis 954 * @since 2.7
mp-wp_genesis 955 */
mp-wp_genesis 956 class WP_Http_Curl {
mp-wp_genesis 957 /**
mp-wp_genesis 958 * Send a HTTP request to a URI using cURL extension.
mp-wp_genesis 959 *
mp-wp_genesis 960 * @access public
mp-wp_genesis 961 * @since 2.7
mp-wp_genesis 962 *
mp-wp_genesis 963 * @param string $url
mp-wp_genesis 964 * @param str|array $args Optional. Override the defaults.
mp-wp_genesis 965 * @return array 'headers', 'body', and 'response' keys.
mp-wp_genesis 966 */
mp-wp_genesis 967 function request($url, $args = array()) {
mp-wp_genesis 968 $defaults = array(
mp-wp_genesis 969 'method' => 'GET', 'timeout' => 5,
mp-wp_genesis 970 'redirection' => 5, 'httpversion' => '1.0',
mp-wp_genesis 971 'blocking' => true,
mp-wp_genesis 972 'headers' => array(), 'body' => null
mp-wp_genesis 973 );
mp-wp_genesis 974
mp-wp_genesis 975 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 976
mp-wp_genesis 977 if ( isset($r['headers']['User-Agent']) ) {
mp-wp_genesis 978 $r['user-agent'] = $r['headers']['User-Agent'];
mp-wp_genesis 979 unset($r['headers']['User-Agent']);
mp-wp_genesis 980 } else if( isset($r['headers']['user-agent']) ) {
mp-wp_genesis 981 $r['user-agent'] = $r['headers']['user-agent'];
mp-wp_genesis 982 unset($r['headers']['user-agent']);
mp-wp_genesis 983 }
mp-wp_genesis 984
mp-wp_genesis 985 // If timeout is a float less than 1, round it up to 1.
mp-wp_genesis 986 if ( $r['timeout'] > 0 && $r['timeout'] < 1 )
mp-wp_genesis 987 $r['timeout'] = 1;
mp-wp_genesis 988
mp-wp_genesis 989 $handle = curl_init();
mp-wp_genesis 990 curl_setopt( $handle, CURLOPT_URL, $url);
mp-wp_genesis 991
mp-wp_genesis 992 if ( 'HEAD' === $r['method'] ) {
mp-wp_genesis 993 curl_setopt( $handle, CURLOPT_NOBODY, true );
mp-wp_genesis 994 }
mp-wp_genesis 995
mp-wp_genesis 996 if ( true === $r['blocking'] ) {
mp-wp_genesis 997 curl_setopt( $handle, CURLOPT_HEADER, true );
mp-wp_genesis 998 curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 1 );
mp-wp_genesis 999 } else {
mp-wp_genesis 1000 curl_setopt( $handle, CURLOPT_HEADER, false );
mp-wp_genesis 1001 curl_setopt( $handle, CURLOPT_NOBODY, true );
mp-wp_genesis 1002 curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 0 );
mp-wp_genesis 1003 }
mp-wp_genesis 1004
mp-wp_genesis 1005 curl_setopt( $handle, CURLOPT_USERAGENT, $r['user-agent'] );
mp-wp_genesis 1006 curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 1 );
mp-wp_genesis 1007 curl_setopt( $handle, CURLOPT_TIMEOUT, $r['timeout'] );
mp-wp_genesis 1008 curl_setopt( $handle, CURLOPT_MAXREDIRS, $r['redirection'] );
mp-wp_genesis 1009
mp-wp_genesis 1010 if ( !ini_get('safe_mode') && !ini_get('open_basedir') )
mp-wp_genesis 1011 curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, true );
mp-wp_genesis 1012
mp-wp_genesis 1013 if( ! is_null($r['headers']) )
mp-wp_genesis 1014 curl_setopt( $handle, CURLOPT_HTTPHEADER, $r['headers'] );
mp-wp_genesis 1015
mp-wp_genesis 1016 if ( $r['httpversion'] == '1.0' )
mp-wp_genesis 1017 curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
mp-wp_genesis 1018 else
mp-wp_genesis 1019 curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
mp-wp_genesis 1020
mp-wp_genesis 1021 if ( ! $r['blocking'] ) {
mp-wp_genesis 1022 curl_exec( $handle );
mp-wp_genesis 1023 curl_close( $handle );
mp-wp_genesis 1024 return array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
mp-wp_genesis 1025 }
mp-wp_genesis 1026
mp-wp_genesis 1027 $theResponse = curl_exec( $handle );
mp-wp_genesis 1028
mp-wp_genesis 1029 if ( !empty($theResponse) ) {
mp-wp_genesis 1030 $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
mp-wp_genesis 1031 $theHeaders = trim( substr($theResponse, 0, $headerLength) );
mp-wp_genesis 1032 $theBody = substr( $theResponse, $headerLength );
mp-wp_genesis 1033 if ( false !== strrpos($theHeaders, "\r\n\r\n") ) {
mp-wp_genesis 1034 $headerParts = explode("\r\n\r\n", $theHeaders);
mp-wp_genesis 1035 $theHeaders = $headerParts[ count($headerParts) -1 ];
mp-wp_genesis 1036 }
mp-wp_genesis 1037 $theHeaders = WP_Http::processHeaders($theHeaders);
mp-wp_genesis 1038 } else {
mp-wp_genesis 1039 if ( $curl_error = curl_error($handle) )
mp-wp_genesis 1040 return new WP_Error('http_request_failed', $curl_error);
mp-wp_genesis 1041 if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array(301, 302) ) )
mp-wp_genesis 1042 return new WP_Error('http_request_failed', __('Too many redirects.'));
mp-wp_genesis 1043
mp-wp_genesis 1044 $theHeaders = array( 'headers' => array() );
mp-wp_genesis 1045 $theBody = '';
mp-wp_genesis 1046 }
mp-wp_genesis 1047 $response = array();
mp-wp_genesis 1048 $response['code'] = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
mp-wp_genesis 1049 $response['message'] = get_status_header_desc($response['code']);
mp-wp_genesis 1050
mp-wp_genesis 1051 curl_close( $handle );
mp-wp_genesis 1052
mp-wp_genesis 1053 return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response);
mp-wp_genesis 1054 }
mp-wp_genesis 1055
mp-wp_genesis 1056 /**
mp-wp_genesis 1057 * Whether this class can be used for retrieving an URL.
mp-wp_genesis 1058 *
mp-wp_genesis 1059 * @static
mp-wp_genesis 1060 * @since 2.7
mp-wp_genesis 1061 *
mp-wp_genesis 1062 * @return boolean False means this class can not be used, true means it can.
mp-wp_genesis 1063 */
mp-wp_genesis 1064 function test() {
mp-wp_genesis 1065 if ( function_exists('curl_init') )
mp-wp_genesis 1066 return true;
mp-wp_genesis 1067
mp-wp_genesis 1068 return false;
mp-wp_genesis 1069 }
mp-wp_genesis 1070 }
mp-wp_genesis 1071
mp-wp_genesis 1072 /**
mp-wp_genesis 1073 * Returns the initialized WP_Http Object
mp-wp_genesis 1074 *
mp-wp_genesis 1075 * @since 2.7
mp-wp_genesis 1076 * @access private
mp-wp_genesis 1077 *
mp-wp_genesis 1078 * @return WP_Http HTTP Transport object.
mp-wp_genesis 1079 */
mp-wp_genesis 1080 function &_wp_http_get_object() {
mp-wp_genesis 1081 static $http;
mp-wp_genesis 1082
mp-wp_genesis 1083 if ( is_null($http) )
mp-wp_genesis 1084 $http = new WP_Http();
mp-wp_genesis 1085
mp-wp_genesis 1086 return $http;
mp-wp_genesis 1087 }
mp-wp_genesis 1088
mp-wp_genesis 1089 /**
mp-wp_genesis 1090 * Retrieve the raw response from the HTTP request.
mp-wp_genesis 1091 *
mp-wp_genesis 1092 * The array structure is a little complex.
mp-wp_genesis 1093 *
mp-wp_genesis 1094 * <code>
mp-wp_genesis 1095 * $res = array( 'headers' => array(), 'response' => array('code', 'message') );
mp-wp_genesis 1096 * </code>
mp-wp_genesis 1097 *
mp-wp_genesis 1098 * All of the headers in $res['headers'] are with the name as the key and the
mp-wp_genesis 1099 * value as the value. So to get the User-Agent, you would do the following.
mp-wp_genesis 1100 *
mp-wp_genesis 1101 * <code>
mp-wp_genesis 1102 * $user_agent = $res['headers']['user-agent'];
mp-wp_genesis 1103 * </code>
mp-wp_genesis 1104 *
mp-wp_genesis 1105 * The body is the raw response content and can be retrieved from $res['body'].
mp-wp_genesis 1106 *
mp-wp_genesis 1107 * This function is called first to make the request and there are other API
mp-wp_genesis 1108 * functions to abstract out the above convoluted setup.
mp-wp_genesis 1109 *
mp-wp_genesis 1110 * @since 2.7.0
mp-wp_genesis 1111 *
mp-wp_genesis 1112 * @param string $url Site URL to retrieve.
mp-wp_genesis 1113 * @param array $args Optional. Override the defaults.
mp-wp_genesis 1114 * @return string The body of the response
mp-wp_genesis 1115 */
mp-wp_genesis 1116 function wp_remote_request($url, $args = array()) {
mp-wp_genesis 1117 $objFetchSite = _wp_http_get_object();
mp-wp_genesis 1118 return $objFetchSite->request($url, $args);
mp-wp_genesis 1119 }
mp-wp_genesis 1120
mp-wp_genesis 1121 /**
mp-wp_genesis 1122 * Retrieve the raw response from the HTTP request using the GET method.
mp-wp_genesis 1123 *
mp-wp_genesis 1124 * @see wp_remote_request() For more information on the response array format.
mp-wp_genesis 1125 *
mp-wp_genesis 1126 * @since 2.7
mp-wp_genesis 1127 *
mp-wp_genesis 1128 * @param string $url Site URL to retrieve.
mp-wp_genesis 1129 * @param array $args Optional. Override the defaults.
mp-wp_genesis 1130 * @return string The body of the response
mp-wp_genesis 1131 */
mp-wp_genesis 1132 function wp_remote_get($url, $args = array()) {
mp-wp_genesis 1133 $objFetchSite = _wp_http_get_object();
mp-wp_genesis 1134
mp-wp_genesis 1135 return $objFetchSite->get($url, $args);
mp-wp_genesis 1136 }
mp-wp_genesis 1137
mp-wp_genesis 1138 /**
mp-wp_genesis 1139 * Retrieve the raw response from the HTTP request using the POST method.
mp-wp_genesis 1140 *
mp-wp_genesis 1141 * @see wp_remote_request() For more information on the response array format.
mp-wp_genesis 1142 *
mp-wp_genesis 1143 * @since 2.7
mp-wp_genesis 1144 *
mp-wp_genesis 1145 * @param string $url Site URL to retrieve.
mp-wp_genesis 1146 * @param array $args Optional. Override the defaults.
mp-wp_genesis 1147 * @return string The body of the response
mp-wp_genesis 1148 */
mp-wp_genesis 1149 function wp_remote_post($url, $args = array()) {
mp-wp_genesis 1150 $objFetchSite = _wp_http_get_object();
mp-wp_genesis 1151 return $objFetchSite->post($url, $args);
mp-wp_genesis 1152 }
mp-wp_genesis 1153
mp-wp_genesis 1154 /**
mp-wp_genesis 1155 * Retrieve the raw response from the HTTP request using the HEAD method.
mp-wp_genesis 1156 *
mp-wp_genesis 1157 * @see wp_remote_request() For more information on the response array format.
mp-wp_genesis 1158 *
mp-wp_genesis 1159 * @since 2.7
mp-wp_genesis 1160 *
mp-wp_genesis 1161 * @param string $url Site URL to retrieve.
mp-wp_genesis 1162 * @param array $args Optional. Override the defaults.
mp-wp_genesis 1163 * @return string The body of the response
mp-wp_genesis 1164 */
mp-wp_genesis 1165 function wp_remote_head($url, $args = array()) {
mp-wp_genesis 1166 $objFetchSite = _wp_http_get_object();
mp-wp_genesis 1167 return $objFetchSite->head($url, $args);
mp-wp_genesis 1168 }
mp-wp_genesis 1169
mp-wp_genesis 1170 /**
mp-wp_genesis 1171 * Retrieve only the headers from the raw response.
mp-wp_genesis 1172 *
mp-wp_genesis 1173 * @since 2.7
mp-wp_genesis 1174 *
mp-wp_genesis 1175 * @param array $response HTTP response.
mp-wp_genesis 1176 * @return array The headers of the response. Empty array if incorrect parameter given.
mp-wp_genesis 1177 */
mp-wp_genesis 1178 function wp_remote_retrieve_headers(&$response) {
mp-wp_genesis 1179 if ( ! isset($response['headers']) || ! is_array($response['headers']))
mp-wp_genesis 1180 return array();
mp-wp_genesis 1181
mp-wp_genesis 1182 return $response['headers'];
mp-wp_genesis 1183 }
mp-wp_genesis 1184
mp-wp_genesis 1185 /**
mp-wp_genesis 1186 * Retrieve a single header by name from the raw response.
mp-wp_genesis 1187 *
mp-wp_genesis 1188 * @since 2.7
mp-wp_genesis 1189 *
mp-wp_genesis 1190 * @param array $response
mp-wp_genesis 1191 * @param string $header Header name to retrieve value from.
mp-wp_genesis 1192 * @return array The header value. Empty string on if incorrect parameter given.
mp-wp_genesis 1193 */
mp-wp_genesis 1194 function wp_remote_retrieve_header(&$response, $header) {
mp-wp_genesis 1195 if ( ! isset($response['headers']) || ! is_array($response['headers']))
mp-wp_genesis 1196 return '';
mp-wp_genesis 1197
mp-wp_genesis 1198 if ( array_key_exists($header, $response['headers']) )
mp-wp_genesis 1199 return $response['headers'][$header];
mp-wp_genesis 1200
mp-wp_genesis 1201 return '';
mp-wp_genesis 1202 }
mp-wp_genesis 1203
mp-wp_genesis 1204 /**
mp-wp_genesis 1205 * Retrieve only the response code from the raw response.
mp-wp_genesis 1206 *
mp-wp_genesis 1207 * Will return an empty array if incorrect parameter value is given.
mp-wp_genesis 1208 *
mp-wp_genesis 1209 * @since 2.7
mp-wp_genesis 1210 *
mp-wp_genesis 1211 * @param array $response HTTP response.
mp-wp_genesis 1212 * @return array The keys 'code' and 'message' give information on the response.
mp-wp_genesis 1213 */
mp-wp_genesis 1214 function wp_remote_retrieve_response_code(&$response) {
mp-wp_genesis 1215 if ( ! isset($response['response']) || ! is_array($response['response']))
mp-wp_genesis 1216 return '';
mp-wp_genesis 1217
mp-wp_genesis 1218 return $response['response']['code'];
mp-wp_genesis 1219 }
mp-wp_genesis 1220
mp-wp_genesis 1221 /**
mp-wp_genesis 1222 * Retrieve only the response message from the raw response.
mp-wp_genesis 1223 *
mp-wp_genesis 1224 * Will return an empty array if incorrect parameter value is given.
mp-wp_genesis 1225 *
mp-wp_genesis 1226 * @since 2.7
mp-wp_genesis 1227 *
mp-wp_genesis 1228 * @param array $response HTTP response.
mp-wp_genesis 1229 * @return array The keys 'code' and 'message' give information on the response.
mp-wp_genesis 1230 */
mp-wp_genesis 1231 function wp_remote_retrieve_response_message(&$response) {
mp-wp_genesis 1232 if ( ! isset($response['response']) || ! is_array($response['response']))
mp-wp_genesis 1233 return '';
mp-wp_genesis 1234
mp-wp_genesis 1235 return $response['response']['message'];
mp-wp_genesis 1236 }
mp-wp_genesis 1237
mp-wp_genesis 1238 /**
mp-wp_genesis 1239 * Retrieve only the body from the raw response.
mp-wp_genesis 1240 *
mp-wp_genesis 1241 * @since 2.7
mp-wp_genesis 1242 *
mp-wp_genesis 1243 * @param array $response HTTP response.
mp-wp_genesis 1244 * @return string The body of the response. Empty string if no body or incorrect parameter given.
mp-wp_genesis 1245 */
mp-wp_genesis 1246 function wp_remote_retrieve_body(&$response) {
mp-wp_genesis 1247 if ( ! isset($response['body']) )
mp-wp_genesis 1248 return '';
mp-wp_genesis 1249
mp-wp_genesis 1250 return $response['body'];
mp-wp_genesis 1251 }
mp-wp_genesis 1252
mp-wp_genesis 1253 ?>