/**
  * Send an HTTP request to a URI.
  *
  * Please note: The only URI that are supported in the HTTP Transport implementation
  * are the HTTP and HTTPS protocols.
  *
  * @access public
  * @since 2.7.0
  *
  * @global string $wp_version
  *
  * @param string       $url  The request URL.
  * @param string|array $args {
  *     Optional. Array or string of HTTP request arguments.
  *
  *     @type string       $method              Request method. Accepts 'GET', 'POST', 'HEAD', or 'PUT'.
  *                                             Some transports technically allow others, but should not be
  *                                             assumed. Default 'GET'.
  *     @type int          $timeout             How long the connection should stay open in seconds. Default 5.
  *     @type int          $redirection         Number of allowed redirects. Not supported by all transports
  *                                             Default 5.
  *     @type string       $httpversion         Version of the HTTP protocol to use. Accepts '1.0' and '1.1'.
  *                                             Default '1.0'.
  *     @type string       $user-agent          User-agent value sent.
  *                                             Default WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ).
  *     @type bool         $reject_unsafe_urls  Whether to pass URLs through {@see wp_http_validate_url()}.
  *                                             Default false.
  *     @type bool         $blocking            Whether the calling code requires the result of the request.
  *                                             If set to false, the request will be sent to the remote server,
  *                                             and processing returned to the calling code immediately, the caller
  *                                             will know if the request succeeded or failed, but will not receive
  *                                             any response from the remote server. Default true.
  *     @type string|array $headers             Array or string of headers to send with the request.
  *                                             Default empty array.
  *     @type array        $cookies             List of cookies to send with the request. Default empty array.
  *     @type string|array $body                Body to send with the request. Default null.
  *     @type bool         $compress            Whether to compress the $body when sending the request.
  *                                             Default false.
  *     @type bool         $decompress          Whether to decompress a compressed response. If set to false and
  *                                             compressed content is returned in the response anyway, it will
  *                                             need to be separately decompressed. Default true.
  *     @type bool         $sslverify           Whether to verify SSL for the request. Default true.
  *     @type string       sslcertificates      Absolute path to an SSL certificate .crt file.
  *                                             Default ABSPATH . WPINC . '/certificates/ca-bundle.crt'.
  *     @type bool         $stream              Whether to stream to a file. If set to true and no filename was
  *                                             given, it will be droped it in the WP temp dir and its name will
  *                                             be set using the basename of the URL. Default false.
  *     @type string       $filename            Filename of the file to write to when streaming. $stream must be
  *                                             set to true. Default null.
  *     @type int          $limit_response_size Size in bytes to limit the response to. Default null.
  *
  * }
  * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
  *                        A WP_Error instance upon error.
  */
 public function request($url, $args = array())
 {
     global $wp_version;
     $defaults = array('method' => 'GET', 'timeout' => apply_filters('http_request_timeout', 5), 'redirection' => apply_filters('http_request_redirection_count', 5), 'httpversion' => apply_filters('http_request_version', '1.0'), 'user-agent' => apply_filters('http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')), 'reject_unsafe_urls' => apply_filters('http_request_reject_unsafe_urls', false), 'blocking' => true, 'headers' => array(), 'cookies' => array(), 'body' => null, 'compress' => false, 'decompress' => true, 'sslverify' => true, 'sslcertificates' => ABSPATH . WPINC . '/certificates/ca-bundle.crt', 'stream' => false, 'filename' => null, 'limit_response_size' => null);
     // Pre-parse for the HEAD checks.
     $args = wp_parse_args($args);
     // By default, Head requests do not cause redirections.
     if (isset($args['method']) && 'HEAD' == $args['method']) {
         $defaults['redirection'] = 0;
     }
     $r = wp_parse_args($args, $defaults);
     /**
      * Filter the arguments used in an HTTP request.
      *
      * @since 2.7.0
      *
      * @param array  $r   An array of HTTP request arguments.
      * @param string $url The request URL.
      */
     $r = apply_filters('http_request_args', $r, $url);
     // The transports decrement this, store a copy of the original value for loop purposes.
     if (!isset($r['_redirection'])) {
         $r['_redirection'] = $r['redirection'];
     }
     /**
      * Filter whether to preempt an HTTP request's return value.
      *
      * Returning a non-false value from the filter will short-circuit the HTTP request and return
      * early with that value. A filter should return either:
      *
      *  - An array containing 'headers', 'body', 'response', 'cookies', and 'filename' elements
      *  - A WP_Error instance
      *  - boolean false (to avoid short-circuiting the response)
      *
      * Returning any other value may result in unexpected behaviour.
      *
      * @since 2.9.0
      *
      * @param false|array|WP_Error $preempt Whether to preempt an HTTP request's return value. Default false.
      * @param array               $r        HTTP request arguments.
      * @param string              $url      The request URL.
      */
     $pre = apply_filters('pre_http_request', false, $r, $url);
     if (false !== $pre) {
         return $pre;
     }
     if (function_exists('wp_kses_bad_protocol')) {
         if ($r['reject_unsafe_urls']) {
             $url = wp_http_validate_url($url);
         }
         if ($url) {
             $url = wp_kses_bad_protocol($url, array('http', 'https', 'ssl'));
         }
     }
     $arrURL = @parse_url($url);
     if (empty($url) || empty($arrURL['scheme'])) {
         return new WP_Error('http_request_failed', __('A valid URL was not provided.'));
     }
     if ($this->block_request($url)) {
         return new WP_Error('http_request_failed', __('User has blocked requests through HTTP.'));
     }
     /*
      * Determine if this is a https call and pass that on to the transport functions
      * so that we can blacklist the transports that do not support ssl verification
      */
     $r['ssl'] = $arrURL['scheme'] == 'https' || $arrURL['scheme'] == 'ssl';
     // Determine if this request is to OUR install of WordPress.
     $homeURL = parse_url(get_bloginfo('url'));
     $r['local'] = 'localhost' == $arrURL['host'] || isset($homeURL['host']) && $homeURL['host'] == $arrURL['host'];
     unset($homeURL);
     /*
      * If we are streaming to a file but no filename was given drop it in the WP temp dir
      * and pick its name using the basename of the $url.
      */
     if ($r['stream'] && empty($r['filename'])) {
         $r['filename'] = get_temp_dir() . wp_unique_filename(get_temp_dir(), basename($url));
     }
     /*
      * Force some settings if we are streaming to a file and check for existence and perms
      * of destination directory.
      */
     if ($r['stream']) {
         $r['blocking'] = true;
         if (!wp_is_writable(dirname($r['filename']))) {
             return new WP_Error('http_request_failed', __('Destination directory for file streaming does not exist or is not writable.'));
         }
     }
     if (is_null($r['headers'])) {
         $r['headers'] = array();
     }
     if (!is_array($r['headers'])) {
         $processedHeaders = self::processHeaders($r['headers'], $url);
         $r['headers'] = $processedHeaders['headers'];
     }
     if (isset($r['headers']['User-Agent'])) {
         $r['user-agent'] = $r['headers']['User-Agent'];
         unset($r['headers']['User-Agent']);
     }
     if (isset($r['headers']['user-agent'])) {
         $r['user-agent'] = $r['headers']['user-agent'];
         unset($r['headers']['user-agent']);
     }
     if ('1.1' == $r['httpversion'] && !isset($r['headers']['connection'])) {
         $r['headers']['connection'] = 'close';
     }
     // Construct Cookie: header if any cookies are set.
     self::buildCookieHeader($r);
     // Avoid issues where mbstring.func_overload is enabled.
     mbstring_binary_safe_encoding();
     if (!isset($r['headers']['Accept-Encoding'])) {
         if ($encoding = WP_Http_Encoding::accept_encoding($url, $r)) {
             $r['headers']['Accept-Encoding'] = $encoding;
         }
     }
     if (!is_null($r['body']) && '' != $r['body'] || 'POST' == $r['method'] || 'PUT' == $r['method']) {
         if (is_array($r['body']) || is_object($r['body'])) {
             $r['body'] = http_build_query($r['body'], null, '&');
             if (!isset($r['headers']['Content-Type'])) {
                 $r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
             }
         }
         if ('' === $r['body']) {
             $r['body'] = null;
         }
         if (!isset($r['headers']['Content-Length']) && !isset($r['headers']['content-length'])) {
             $r['headers']['Content-Length'] = strlen($r['body']);
         }
     }
     $response = $this->_dispatch_request($url, $r);
     reset_mbstring_encoding();
     if (is_wp_error($response)) {
         return $response;
     }
     // Append cookies that were used in this request to the response
     if (!empty($r['cookies'])) {
         $cookies_set = wp_list_pluck($response['cookies'], 'name');
         foreach ($r['cookies'] as $cookie) {
             if (!in_array($cookie->name, $cookies_set) && $cookie->test($url)) {
                 $response['cookies'][] = $cookie;
             }
         }
     }
     return $response;
 }
예제 #2
0
파일: http.php 프로젝트: steveh/wordpress
 /**
  * Send a HTTP request to a URI.
  *
  * The body and headers are part of the arguments. The 'body' argument is for the body and will
  * accept either a string or an array. The 'headers' argument should be an array, but a string
  * is acceptable. If the 'body' argument is an array, then it will automatically be escaped
  * using http_build_query().
  *
  * The only URI that are supported in the HTTP Transport implementation are the HTTP and HTTPS
  * protocols. HTTP and HTTPS are assumed so the server might not know how to handle the send
  * headers. Other protocols are unsupported and most likely will fail.
  *
  * The defaults are 'method', 'timeout', 'redirection', 'httpversion', 'blocking' and
  * 'user-agent'.
  *
  * Accepted 'method' values are 'GET', 'POST', and 'HEAD', some transports technically allow
  * others, but should not be assumed. The 'timeout' is used to sent how long the connection
  * should stay open before failing when no response. 'redirection' is used to track how many
  * redirects were taken and used to sent the amount for other transports, but not all transports
  * accept setting that value.
  *
  * The 'httpversion' option is used to sent the HTTP version and accepted values are '1.0', and
  * '1.1' and should be a string. Version 1.1 is not supported, because of chunk response. The
  * 'user-agent' option is the user-agent and is used to replace the default user-agent, which is
  * 'WordPress/WP_Version', where WP_Version is the value from $wp_version.
  *
  * 'blocking' is the default, which is used to tell the transport, whether it should halt PHP
  * while it performs the request or continue regardless. Actually, that isn't entirely correct.
  * Blocking mode really just means whether the fread should just pull what it can whenever it
  * gets bytes or if it should wait until it has enough in the buffer to read or finishes reading
  * the entire content. It doesn't actually always mean that PHP will continue going after making
  * the request.
  *
  * @access public
  * @since 2.7.0
  * @todo Refactor this code. The code in this method extends the scope of its original purpose
  *		and should be refactored to allow for cleaner abstraction and reduce duplication of the
  *		code. One suggestion is to create a class specifically for the arguments, however
  *		preliminary refactoring to this affect has affect more than just the scope of the
  *		arguments. Something to ponder at least.
  *
  * @param string $url URI resource.
  * @param str|array $args Optional. Override the defaults.
  * @return array containing 'headers', 'body', 'response', 'cookies'
  */
 function request($url, $args = array())
 {
     global $wp_version;
     $defaults = array('method' => 'GET', 'timeout' => apply_filters('http_request_timeout', 5), 'redirection' => apply_filters('http_request_redirection_count', 5), 'httpversion' => apply_filters('http_request_version', '1.0'), 'user-agent' => apply_filters('http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')), 'blocking' => true, 'headers' => array(), 'cookies' => array(), 'body' => null, 'compress' => false, 'decompress' => true, 'sslverify' => true);
     $r = wp_parse_args($args, $defaults);
     $r = apply_filters('http_request_args', $r, $url);
     // Allow plugins to short-circuit the request
     $pre = apply_filters('pre_http_request', false, $r, $url);
     if (false !== $pre) {
         return $pre;
     }
     $arrURL = parse_url($url);
     if ($this->block_request($url)) {
         return new WP_Error('http_request_failed', __('User has blocked requests through HTTP.'));
     }
     // Determine if this is a https call and pass that on to the transport functions
     // so that we can blacklist the transports that do not support ssl verification
     $r['ssl'] = $arrURL['scheme'] == 'https' || $arrURL['scheme'] == 'ssl';
     // Determine if this request is to OUR install of WordPress
     $homeURL = parse_url(get_bloginfo('url'));
     $r['local'] = $homeURL['host'] == $arrURL['host'] || 'localhost' == $arrURL['host'];
     unset($homeURL);
     if (is_null($r['headers'])) {
         $r['headers'] = array();
     }
     if (!is_array($r['headers'])) {
         $processedHeaders = WP_Http::processHeaders($r['headers']);
         $r['headers'] = $processedHeaders['headers'];
     }
     if (isset($r['headers']['User-Agent'])) {
         $r['user-agent'] = $r['headers']['User-Agent'];
         unset($r['headers']['User-Agent']);
     }
     if (isset($r['headers']['user-agent'])) {
         $r['user-agent'] = $r['headers']['user-agent'];
         unset($r['headers']['user-agent']);
     }
     // Construct Cookie: header if any cookies are set
     WP_Http::buildCookieHeader($r);
     if (WP_Http_Encoding::is_available()) {
         $r['headers']['Accept-Encoding'] = WP_Http_Encoding::accept_encoding();
     }
     if (empty($r['body'])) {
         // Some servers fail when sending content without the content-length header being set.
         // Also, to fix another bug, we only send when doing POST and PUT and the content-length
         // header isn't already set.
         if (($r['method'] == 'POST' || $r['method'] == 'PUT') && !isset($r['headers']['Content-Length'])) {
             $r['headers']['Content-Length'] = 0;
         }
         // The method is ambiguous, because we aren't talking about HTTP methods, the "get" in
         // this case is simply that we aren't sending any bodies and to get the transports that
         // don't support sending bodies along with those which do.
         $transports = WP_Http::_getTransport($r);
     } else {
         if (is_array($r['body']) || is_object($r['body'])) {
             if (!version_compare(phpversion(), '5.1.2', '>=')) {
                 $r['body'] = _http_build_query($r['body'], null, '&');
             } else {
                 $r['body'] = http_build_query($r['body'], null, '&');
             }
             $r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
             $r['headers']['Content-Length'] = strlen($r['body']);
         }
         if (!isset($r['headers']['Content-Length']) && !isset($r['headers']['content-length'])) {
             $r['headers']['Content-Length'] = strlen($r['body']);
         }
         // The method is ambiguous, because we aren't talking about HTTP methods, the "post" in
         // this case is simply that we are sending HTTP body and to get the transports that do
         // support sending the body. Not all do, depending on the limitations of the PHP core
         // limitations.
         $transports = WP_Http::_postTransport($r);
     }
     do_action('http_api_debug', $transports, 'transports_list');
     $response = array('headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array());
     foreach ((array) $transports as $transport) {
         $response = $transport->request($url, $r);
         do_action('http_api_debug', $response, 'response', get_class($transport));
         if (!is_wp_error($response)) {
             return apply_filters('http_response', $response, $r, $url);
         }
     }
     return $response;
 }
예제 #3
0
 /**
  * Send a HTTP request to a URI.
  *
  * The body and headers are part of the arguments. The 'body' argument is for the body and will
  * accept either a string or an array. The 'headers' argument should be an array, but a string
  * is acceptable. If the 'body' argument is an array, then it will automatically be escaped
  * using http_build_query().
  *
  * The only URI that are supported in the HTTP Transport implementation are the HTTP and HTTPS
  * protocols. HTTP and HTTPS are assumed so the server might not know how to handle the send
  * headers. Other protocols are unsupported and most likely will fail.
  *
  * The defaults are 'method', 'timeout', 'redirection', 'httpversion', 'blocking' and
  * 'user-agent'.
  *
  * Accepted 'method' values are 'GET', 'POST', and 'HEAD', some transports technically allow
  * others, but should not be assumed. The 'timeout' is used to sent how long the connection
  * should stay open before failing when no response. 'redirection' is used to track how many
  * redirects were taken and used to sent the amount for other transports, but not all transports
  * accept setting that value.
  *
  * The 'httpversion' option is used to sent the HTTP version and accepted values are '1.0', and
  * '1.1' and should be a string. Version 1.1 is not supported, because of chunk response. The
  * 'user-agent' option is the user-agent and is used to replace the default user-agent, which is
  * 'WordPress/WP_Version', where WP_Version is the value from $wp_version.
  *
  * 'blocking' is the default, which is used to tell the transport, whether it should halt PHP
  * while it performs the request or continue regardless. Actually, that isn't entirely correct.
  * Blocking mode really just means whether the fread should just pull what it can whenever it
  * gets bytes or if it should wait until it has enough in the buffer to read or finishes reading
  * the entire content. It doesn't actually always mean that PHP will continue going after making
  * the request.
  *
  * @access public
  * @since 2.7.0
  * @todo Refactor this code. The code in this method extends the scope of its original purpose
  *		and should be refactored to allow for cleaner abstraction and reduce duplication of the
  *		code. One suggestion is to create a class specifically for the arguments, however
  *		preliminary refactoring to this affect has affect more than just the scope of the
  *		arguments. Something to ponder at least.
  *
  * @param string $url URI resource.
  * @param str|array $args Optional. Override the defaults.
  * @return array|object Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
  */
 function request($url, $args = array())
 {
     global $wp_version;
     $defaults = array('method' => 'GET', 'timeout' => apply_filters('http_request_timeout', 5), 'redirection' => apply_filters('http_request_redirection_count', 5), 'httpversion' => apply_filters('http_request_version', '1.0'), 'user-agent' => apply_filters('http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')), 'blocking' => true, 'headers' => array(), 'cookies' => array(), 'body' => null, 'compress' => false, 'decompress' => true, 'sslverify' => true, 'stream' => false, 'filename' => null);
     // Pre-parse for the HEAD checks.
     $args = wp_parse_args($args);
     // By default, Head requests do not cause redirections.
     if (isset($args['method']) && 'HEAD' == $args['method']) {
         $defaults['redirection'] = 0;
     }
     $r = wp_parse_args($args, $defaults);
     $r = apply_filters('http_request_args', $r, $url);
     // Certain classes decrement this, store a copy of the original value for loop purposes.
     $r['_redirection'] = $r['redirection'];
     // Allow plugins to short-circuit the request
     $pre = apply_filters('pre_http_request', false, $r, $url);
     if (false !== $pre) {
         return $pre;
     }
     $arrURL = parse_url($url);
     if (empty($url) || empty($arrURL['scheme'])) {
         return new WP_Error('http_request_failed', __('A valid URL was not provided.'));
     }
     if ($this->block_request($url)) {
         return new WP_Error('http_request_failed', __('User has blocked requests through HTTP.'));
     }
     // Determine if this is a https call and pass that on to the transport functions
     // so that we can blacklist the transports that do not support ssl verification
     $r['ssl'] = $arrURL['scheme'] == 'https' || $arrURL['scheme'] == 'ssl';
     // Determine if this request is to OUR install of WordPress
     $homeURL = parse_url(get_bloginfo('url'));
     $r['local'] = $homeURL['host'] == $arrURL['host'] || 'localhost' == $arrURL['host'];
     unset($homeURL);
     // If we are streaming to a file but no filename was given drop it in the WP temp dir
     // and pick it's name using the basename of the $url
     if ($r['stream'] && empty($r['filename'])) {
         $r['filename'] = get_temp_dir() . basename($url);
     }
     // Force some settings if we are streaming to a file and check for existence and perms of destination directory
     if ($r['stream']) {
         $r['blocking'] = true;
         if (!is_writable(dirname($r['filename']))) {
             return new WP_Error('http_request_failed', __('Destination directory for file streaming does not exist or is not writable.'));
         }
     }
     if (is_null($r['headers'])) {
         $r['headers'] = array();
     }
     if (!is_array($r['headers'])) {
         $processedHeaders = WP_Http::processHeaders($r['headers']);
         $r['headers'] = $processedHeaders['headers'];
     }
     if (isset($r['headers']['User-Agent'])) {
         $r['user-agent'] = $r['headers']['User-Agent'];
         unset($r['headers']['User-Agent']);
     }
     if (isset($r['headers']['user-agent'])) {
         $r['user-agent'] = $r['headers']['user-agent'];
         unset($r['headers']['user-agent']);
     }
     // Construct Cookie: header if any cookies are set
     WP_Http::buildCookieHeader($r);
     if (WP_Http_Encoding::is_available()) {
         $r['headers']['Accept-Encoding'] = WP_Http_Encoding::accept_encoding();
     }
     if (empty($r['body'])) {
         $r['body'] = null;
         // Some servers fail when sending content without the content-length header being set.
         // Also, to fix another bug, we only send when doing POST and PUT and the content-length
         // header isn't already set.
         if (($r['method'] == 'POST' || $r['method'] == 'PUT') && !isset($r['headers']['Content-Length'])) {
             $r['headers']['Content-Length'] = 0;
         }
     } else {
         if (is_array($r['body']) || is_object($r['body'])) {
             $r['body'] = http_build_query($r['body'], null, '&');
             $r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
             $r['headers']['Content-Length'] = strlen($r['body']);
         }
         if (!isset($r['headers']['Content-Length']) && !isset($r['headers']['content-length'])) {
             $r['headers']['Content-Length'] = strlen($r['body']);
         }
     }
     return $this->_dispatch_request($url, $r);
 }
예제 #4
0
파일: http.php 프로젝트: blowery/wordpress
 /**
  * Send a HTTP request to a URI.
  *
  * The body and headers are part of the arguments. The 'body' argument is
  * for the body and will accept either a string or an array. The 'headers'
  * argument should be an array, but a string is acceptable. If the 'body'
  * argument is an array, then it will automatically be escaped using
  * http_build_query().
  *
  * The only URI that are supported in the HTTP Transport implementation are
  * the HTTP and HTTPS protocols. HTTP and HTTPS are assumed so the server
  * might not know how to handle the send headers. Other protocols are
  * unsupported and most likely will fail.
  *
  * The defaults are 'method', 'timeout', 'redirection', 'httpversion',
  * 'blocking' and 'user-agent'.
  *
  * Accepted 'method' values are 'GET', 'POST', and 'HEAD', some transports
  * technically allow others, but should not be assumed. The 'timeout' is
  * used to sent how long the connection should stay open before failing when
  * no response. 'redirection' is used to track how many redirects were taken
  * and used to sent the amount for other transports, but not all transports
  * accept setting that value.
  *
  * The 'httpversion' option is used to sent the HTTP version and accepted
  * values are '1.0', and '1.1' and should be a string. Version 1.1 is not
  * supported, because of chunk response. The 'user-agent' option is the
  * user-agent and is used to replace the default user-agent, which is
  * 'WordPress/WP_Version', where WP_Version is the value from $wp_version.
  *
  * 'blocking' is the default, which is used to tell the transport, whether
  * it should halt PHP while it performs the request or continue regardless.
  * Actually, that isn't entirely correct. Blocking mode really just means
  * whether the fread should just pull what it can whenever it gets bytes or
  * if it should wait until it has enough in the buffer to read or finishes
  * reading the entire content. It doesn't actually always mean that PHP will
  * continue going after making the request.
  *
  * @access public
  * @since 2.7.0
  *
  * @param string $url URI resource.
  * @param str|array $args Optional. Override the defaults.
  * @return boolean
  */
 function request($url, $args = array())
 {
     global $wp_version;
     $defaults = array('method' => 'GET', 'timeout' => apply_filters('http_request_timeout', 5), 'redirection' => apply_filters('http_request_redirection_count', 5), 'httpversion' => apply_filters('http_request_version', '1.0'), 'user-agent' => apply_filters('http_headers_useragent', 'WordPress/' . $wp_version), 'blocking' => true, 'headers' => array(), 'body' => null, 'compress' => false, 'decompress' => true);
     $r = wp_parse_args($args, $defaults);
     $r = apply_filters('http_request_args', $r, $url);
     if (is_null($r['headers'])) {
         $r['headers'] = array();
     }
     if (!is_array($r['headers'])) {
         $processedHeaders = WP_Http::processHeaders($r['headers']);
         $r['headers'] = $processedHeaders['headers'];
     }
     if (isset($r['headers']['User-Agent'])) {
         $r['user-agent'] = $r['headers']['User-Agent'];
         unset($r['headers']['User-Agent']);
     }
     if (isset($r['headers']['user-agent'])) {
         $r['user-agent'] = $r['headers']['user-agent'];
         unset($r['headers']['user-agent']);
     }
     if (WP_Http_Encoding::is_available()) {
         $r['headers']['Accept-Encoding'] = WP_Http_Encoding::accept_encoding();
     }
     if (is_null($r['body'])) {
         // Some servers fail when sending content without the content-length
         // header being set.
         $r['headers']['Content-Length'] = 0;
         $transports = WP_Http::_getTransport($r);
     } else {
         if (is_array($r['body']) || is_object($r['body'])) {
             $r['body'] = http_build_query($r['body'], null, '&');
             $r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
             $r['headers']['Content-Length'] = strlen($r['body']);
         }
         if (!isset($r['headers']['Content-Length']) && !isset($r['headers']['content-length'])) {
             $r['headers']['Content-Length'] = strlen($r['body']);
         }
         $transports = WP_Http::_postTransport($r);
     }
     if (has_action('http_api_debug')) {
         do_action('http_api_debug', $transports, 'transports_list');
     }
     $response = array('headers' => array(), 'body' => '', 'response' => array('code', 'message'));
     foreach ((array) $transports as $transport) {
         $response = $transport->request($url, $r);
         if (has_action('http_api_debug')) {
             do_action('http_api_debug', $response, 'response', get_class($transport));
         }
         if (!is_wp_error($response)) {
             return $response;
         }
     }
     return $response;
 }
예제 #5
0
	/**
	 * Send a HTTP request to a URI.
	 *
	 * The body and headers are part of the arguments. The 'body' argument is for the body and will
	 * accept either a string or an array. The 'headers' argument should be an array, but a string
	 * is acceptable. If the 'body' argument is an array, then it will automatically be escaped
	 * using http_build_query().
	 *
	 * The only URI that are supported in the HTTP Transport implementation are the HTTP and HTTPS
	 * protocols. HTTP and HTTPS are assumed so the server might not know how to handle the send
	 * headers. Other protocols are unsupported and most likely will fail.
	 *
	 * The defaults are 'method', 'timeout', 'redirection', 'httpversion', 'blocking' and
	 * 'user-agent'.
	 *
	 * Accepted 'method' values are 'GET', 'POST', and 'HEAD', some transports technically allow
	 * others, but should not be assumed. The 'timeout' is used to sent how long the connection
	 * should stay open before failing when no response. 'redirection' is used to track how many
	 * redirects were taken and used to sent the amount for other transports, but not all transports
	 * accept setting that value.
	 *
	 * The 'httpversion' option is used to sent the HTTP version and accepted values are '1.0', and
	 * '1.1' and should be a string. Version 1.1 is not supported, because of chunk response. The
	 * 'user-agent' option is the user-agent and is used to replace the default user-agent, which is
	 * 'WordPress/WP_Version', where WP_Version is the value from $wp_version.
	 *
	 * 'blocking' is the default, which is used to tell the transport, whether it should halt PHP
	 * while it performs the request or continue regardless. Actually, that isn't entirely correct.
	 * Blocking mode really just means whether the fread should just pull what it can whenever it
	 * gets bytes or if it should wait until it has enough in the buffer to read or finishes reading
	 * the entire content. It doesn't actually always mean that PHP will continue going after making
	 * the request.
	 *
	 * @access public
	 * @since 2.7.0
	 *
	 * @param string $url URI resource.
	 * @param str|array $args Optional. Override the defaults.
	 * @return array containing 'headers', 'body', 'response', 'cookies'
	 */
	function request( $url, $args = array() ) {
		global $wp_version;

		$defaults = array(
			'method' => 'GET',
			'timeout' => apply_filters( 'http_request_timeout', 5),
			'redirection' => apply_filters( 'http_request_redirection_count', 5),
			'httpversion' => apply_filters( 'http_request_version', '1.0'),
			'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )  ),
			'blocking' => true,
			'headers' => array(),
			'cookies' => array(),
			'body' => null,
			'compress' => false,
			'decompress' => true,
			'sslverify' => true
		);

		$r = wp_parse_args( $args, $defaults );
		$r = apply_filters( 'http_request_args', $r, $url );

		$arrURL = parse_url($url);

		if ( $this->block_request( $url ) )
			return new WP_Error('http_request_failed', 'User has blocked requests through HTTP.');

		// Determine if this is a https call and pass that on to the transport functions
		// so that we can blacklist the transports that do not support ssl verification
		$r['ssl'] = $arrURL['scheme'] == 'https' || $arrURL['scheme'] == 'ssl';

		// Determine if this request is to OUR install of WordPress
		$homeURL = parse_url(get_bloginfo('url'));
		$r['local'] = $homeURL['host'] == $arrURL['host'] || 'localhost' == $arrURL['host'];
		unset($homeURL);

		if ( is_null( $r['headers'] ) )
			$r['headers'] = array();

		if ( ! is_array($r['headers']) ) {
			$processedHeaders = WP_Http::processHeaders($r['headers']);
			$r['headers'] = $processedHeaders['headers'];
		}

		if ( isset($r['headers']['User-Agent']) ) {
			$r['user-agent'] = $r['headers']['User-Agent'];
			unset($r['headers']['User-Agent']);
		}

		if ( isset($r['headers']['user-agent']) ) {
			$r['user-agent'] = $r['headers']['user-agent'];
			unset($r['headers']['user-agent']);
		}

		// Construct Cookie: header if any cookies are set
		WP_Http::buildCookieHeader( $r );

		if ( WP_Http_Encoding::is_available() )
			$r['headers']['Accept-Encoding'] = WP_Http_Encoding::accept_encoding();

		if ( is_null($r['body']) ) {
			// Some servers fail when sending content without the content-length
			// header being set.
			$r['headers']['Content-Length'] = 0;
			$transports = WP_Http::_getTransport($r);
		} else {
			if ( is_array( $r['body'] ) || is_object( $r['body'] ) ) {
				if ( ! version_compare(phpversion(), '5.1.2', '>=') )
					$r['body'] = _http_build_query($r['body'], null, '&');
				else
					$r['body'] = http_build_query($r['body'], null, '&');
				$r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
				$r['headers']['Content-Length'] = strlen($r['body']);
			}

			if ( ! isset( $r['headers']['Content-Length'] ) && ! isset( $r['headers']['content-length'] ) )
				$r['headers']['Content-Length'] = strlen($r['body']);

			$transports = WP_Http::_postTransport($r);
		}

		if ( has_action('http_api_debug') )
			do_action('http_api_debug', $transports, 'transports_list');

		$response = array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() );
		foreach ( (array) $transports as $transport ) {
			$response = $transport->request($url, $r);

			if ( has_action('http_api_debug') )
				do_action( 'http_api_debug', $response, 'response', get_class($transport) );

			if ( ! is_wp_error($response) )
				return $response;
		}

		return $response;
	}