Пример #1
0
 public function request($url, $args = array())
 {
     xapp_import('xapp.Utils.Strings');
     xapp_import('xapp.Utils.Arrays');
     xapp_import('xapp.Commons.Error');
     xapp_import('xapp.Directory.Utils');
     $defaults = array('method' => 'GET', 'timeout' => 5, 'redirection' => 5, 'httpversion' => '1.0', 'user-agent' => 'no agent', 'reject_unsafe_urls' => false, 'blocking' => true, 'headers' => array(), 'cookies' => array(), 'body' => null, 'compress' => false, 'decompress' => true, 'sslverify' => true, 'sslcertificates' => null, 'stream' => false, 'filename' => null, 'limit_response_size' => null);
     // Pre-parse for the HEAD checks.
     $args = XApp_Utils_Array::parse_args($args);
     // By default, Head requests do not cause redirections.
     if (isset($args['method']) && 'HEAD' == $args['method']) {
         $defaults['redirection'] = 0;
     }
     $r = XApp_Utils_Array::parse_args($args, $defaults);
     // The transports decrement this, store a copy of the original value for loop purposes.
     if (!isset($r['_redirection'])) {
         $r['_redirection'] = $r['redirection'];
     }
     $arrURL = @parse_url($url);
     if (empty($url) || empty($arrURL['scheme'])) {
         return new XApp_Error('http_request_failed', 'A valid URL was not provided.');
     }
     /*
      * 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.
     $r['local'] = 'localhost' == $arrURL['host'] || isset($homeURL['host']) && $homeURL['host'] == $arrURL['host'];
     /*
      * 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'] = XApp_Directory_Utils::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 (!XApp_Directory_Utils::is_writable(dirname($r['filename']))) {
             return new XApp_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 (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.
     //XAppHttp::buildCookieHeader( $r );
     // Avoid issues where mbstring.func_overload is enabled.
     XApp_Utils_Strings::mbstring_binary_safe_encoding();
     if (!isset($r['headers']['Accept-Encoding'])) {
         //if ( $encoding = XAppHttp_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);
     XApp_Utils_Strings::reset_mbstring_encoding();
     return $response;
 }