Ejemplo n.º 1
0
 /**
  * 初始化
  * @access public
  * @param array $options 参数
  * @return \think\Request
  */
 public static function instance($options = [])
 {
     if (is_null(self::$instance)) {
         self::$instance = new static($options);
     }
     return self::$instance;
 }
Ejemplo n.º 2
0
 /**
  * 创建一个URL请求
  * @access public
  * @param string    $uri URL地址
  * @param string    $method 请求类型
  * @param array     $params 请求参数
  * @param array     $cookie
  * @param array     $files
  * @param array     $server
  * @param string    $content
  * @return \think\Request
  */
 public static function create($uri, $method = 'GET', $params = [], $cookie = [], $files = [], $server = [], $content = null)
 {
     $server['PATH_INFO'] = '';
     $server['REQUEST_METHOD'] = strtoupper($method);
     $info = parse_url($uri);
     if (isset($info['host'])) {
         $server['SERVER_NAME'] = $info['host'];
         $server['HTTP_HOST'] = $info['host'];
     }
     if (isset($info['scheme'])) {
         if ('https' === $info['scheme']) {
             $server['HTTPS'] = 'on';
             $server['SERVER_PORT'] = 443;
         } else {
             unset($server['HTTPS']);
             $server['SERVER_PORT'] = 80;
         }
     }
     if (isset($info['port'])) {
         $server['SERVER_PORT'] = $info['port'];
         $server['HTTP_HOST'] = $server['HTTP_HOST'] . ':' . $info['port'];
     }
     if (isset($info['user'])) {
         $server['PHP_AUTH_USER'] = $info['user'];
     }
     if (isset($info['pass'])) {
         $server['PHP_AUTH_PW'] = $info['pass'];
     }
     if (!isset($info['path'])) {
         $info['path'] = '/';
     }
     $options = [];
     $queryString = '';
     if (isset($info['query'])) {
         parse_str(html_entity_decode($info['query']), $query);
         if (!empty($params)) {
             $params = array_replace($query, $params);
             $queryString = http_build_query($query, '', '&');
         } else {
             $params = $query;
             $queryString = $info['query'];
         }
     } elseif (!empty($params)) {
         $queryString = http_build_query($params, '', '&');
     }
     $server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');
     $server['QUERY_STRING'] = $queryString;
     $options['cookie'] = $cookie;
     $options['param'] = $params;
     $options['file'] = $files;
     $options['server'] = $server;
     $options['url'] = $server['REQUEST_URI'];
     $options['baseUrl'] = $info['path'];
     $options['pathinfo'] = '/' == $info['path'] ? '/' : ltrim($info['path'], '/');
     $options['method'] = $server['REQUEST_METHOD'];
     $options['domain'] = isset($info['scheme']) ? $info['scheme'] . '://' . $server['HTTP_HOST'] : '';
     $options['content'] = $content;
     self::$instance = new self($options);
     return self::$instance;
 }